Enhance and secure your Bash shells
Bash Tricks
The Bash shell is powerful and infinitely expressive. In this article we describe some tricky techniques you can use to enhance and customize your Bash environment.
Ever stopped and thought about what you're typing several times a day at the command prompt? Many experts prefer the Bash command prompt for its versatility, speed, and convenience. However, users often stick with a small collection of commands without realizing the full range of possibilities available through the very expressive Bash shell. In this article, I will describe a few of my favorite tricks. You might already be using some of these techniques, and some might be new to you; in either case, I hope this article encourages you to explore the advanced capabilities available through Bash.
Tailoring ls
At the end of a long day's work, I recently found myself staring at the command line and wondering where a certain command originated, namely the ls
command. I wouldn't want to hazard a guess at how many times I run the ls
command everyday.
With those bleary eyes, I realized that I'd been typing this command for more than 20 years with little knowledge about it, other than it essentially meant list
. For something so succinct, the all pervasive ls
command is not only greatly understated but also commonly taken for granted.
According to folklore, the ls
command originates from a project that began in 1964 in Cambridge, Massachusetts called Multics (Multiplexed Information and Computing Service), which, among other things, influenced the architecture of the undisputed heavyweight champion of operating systems, Unix. ls
was included in the original version of Unix from AT&T Bell Labs. On the premise that Unix and its ever-popular relative Linux are based around listing files and working within a current working directory structure, it's clear why the ls
command was developed.
Even the freshest faces among us will know that if you just run the ls
command on its own, it will simply show lists of files and directories. A simple command might be:
# ls /usr/local
As compact and bijou as the ls
command is, it can do much if you become familiar with its many useful command-line options.
A common way of disguising a file, for legitimate reasons, from normal directory listings is to add a dot to the start of the filename. Good old ls
makes light work of revealing those hidden files with the -a
switch:
# ls -a
To glean the extra information found in the long
format output, the following switch works well. Incidentally, this switch is often written simply as ll
within a Bash alias in some Linux distributions to save a little typing time, because it is used so ubiquitously. Long output kindly offers information on users and group permissions; you can try it with:
# ls -l
If you've ever lost a file in a subdirectory and would rather not type a lengthy find
command, the ever-faithful recursive switch usually outputs a screed of information, as it usefully includes subdirectories, too:
# ls -R
My favorite is a combination of showing all files, including those hidden, adding long format for permissions and user details, and also converting file sizes to human-readable format (Figure 1). If you ever forget the switches required, you might think of this command as a tribute to Kubrick's 2001: A Space Odyssey:
# ls -hal
Colorful Language
Some, especially older, Linux distributions lack color when outputting information, and with the amount of information and detail that most sys admins need to wade through on a daily basis, color-coding is all but essential in order to work effectively.
One powerful feature of the ls
command, and Bash in general, is that it lets you use simple colors to clarify output. (It is worth mentioning that certain terminals simply aren't designed to display colors, but those with limited functionality tend to be older or part of the hardware's purposely minimal, embedded operating system.)
Among other uses, color schemes let you easily see if you're logged in as the superuser root
or a less privileged user. You can also bring color, at least on some versions, to commands such as the popular grep
command by configuring the appropriate settings in your Bash config file:
export GREP_OPTIONS='--color=auto'
As you might already be aware, the Bash config file resides in each user's home directory as the hidden file .bashrc
. If your symlinks, files, and directories are not displaying in different colors, try adding the config file:
alias ls='ls -F --color=auto'
After you've added that line with your favorite editor, again making sure you're inside your home directory (by typing cd
or cd ~
– or even cd /home/$USER
), you then can update your shell with the altered .bashrc
file as follows:
# cd # . .bashrc
Your file and directory listings should now be far easier to read.
Step Away from the Delete Key
Now that you are staring firmly into the powerful but somewhat abyss-like .bashrc
file, I would be remiss not to mention the following lifesavers.
I've come across certain flavors of systems with these options installed by default (usually using the global user config file /etc/bash.bashrc
, which affects all users, but you can usually override the changes to the config from within your .bashrc
file). If you haven't seen these command switches before, try to guess what they force you to do if you use the delete, copy, or move commands (in Unix-like operating systems move
or mv
is also rename
, for all intents and purposes):
alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
Yes, you're quite right that the preceding .bashrc
entries ensure you'll be prompted before overwriting or manipulating a file. This precaution can save your life; especially if you're typing at a breakneck speed. Bash will force you to pause for reflection before deleting that all-important single copy of a file.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more and more Linux systems are getting targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.
-
Fedora 41 Beta Available with Some Interesting Additions
If you're a Fedora fan, you'll be excited to hear the beta version of the latest release is now available for testing and includes plenty of updates.
-
AlmaLinux Unveils New Hardware Certification Process
The AlmaLinux Hardware Certification Program run by the Certification Special Interest Group (SIG) aims to ensure seamless compatibility between AlmaLinux and a wide range of hardware configurations.
-
Wind River Introduces eLxr Pro Linux Solution
eLxr Pro offers an end-to-end Linux solution backed by expert commercial support.
-
Juno Tab 3 Launches with Ubuntu 24.04
Anyone looking for a full-blown Linux tablet need look no further. Juno has released the Tab 3.
-
New KDE Slimbook Plasma Available for Preorder
Powered by an AMD Ryzen CPU, the latest KDE Slimbook laptop is powerful enough for local AI tasks.
-
Rhino Linux Announces Latest "Quick Update"
If you prefer your Linux distribution to be of the rolling type, Rhino Linux delivers a beautiful and reliable experience.