Enhance and secure your Bash shells

Bash Tricks

© Lead Image © Ksenia Raykova, 123RF.com

© Lead Image © Ksenia Raykova, 123RF.com

Article from Issue 167/2014

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:

Figure 1: Add -hal to the ls to display all files with long-format permissions and file sizes in human-readable form.
# 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

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
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.

Learn More