Enhance and secure your Bash shells
Bash Tricks

© Lead Image © Ksenia Raykova, 123RF.com
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
Direct Download
Read full article as PDF:
Price $2.95
News
-
System76 Teams up with HP to Create the Dev One Laptop
HP and System76 have come together to develop a new laptop, powered by Pop!_OS and aimed toward developers.
-
Titan Linux is a New KDE Linux Based on Debian Stable
Titan Linux is a new Debian-based Linux distribution that features the KDE Plasma desktop with a focus on usability and performance.
-
Danielle Foré Has an Update for elementary OS 7
Now that Ubuntu 22.04 has been released, the team behind elementary OS is preparing for the upcoming 7.0 release.
-
Linux New Media Launches Open Source JobHub
New job website focuses on connecting technical and non-technical professionals with organizations in open source.
-
Ubuntu Cinnamon 22.04 Now Available
Ubuntu Cinnamon 22.04 has been released with all the additions from upstream as well as other features and improvements.
-
Pop!_OS 22.04 Has Officially Been Released
From the makers of some of the finest Linux-powered desktop and laptop computers on the market comes the latest version of their Ubuntu-based distribution, Pop!_OS 22.04.
-
Star Labs Unveils a New Small Format Linux PC
The Byte Mk I is an AMD-powered mini Linux PC with Coreboot support and plenty of power.
-
MX Linux Verison 21.1 “Wildflower” Now Available
The latest release of the systemd-less MX Linux is now ready for public consumption.
-
Microsoft Expands Their Windows Subsystem for Linux Offerings With AlmaLinux
Anyone who works with Windows Subsystem for Linux (WSL) will now find a new addition to the available distributions, one that’s become the front-runner replacement for CentOS.
-
Debian 11.3 Released wIth Numerous Bug and Security Fixes
The latest point release for Debian Bullseye is now available with some very important updates.