Enhance and secure your Bash shells
Bash Tricks
![© Lead Image © Ksenia Raykova, 123RF.com © Lead Image © Ksenia Raykova, 123RF.com](/var/linux_magazin/storage/images/issues/2014/167/bash-tricks/123rf_15427408_dog-tricks-frisbee_kseniaraykova_resized.png/624065-1-eng-US/123rf_15427408_dog-tricks-frisbee_KseniaRaykova_resized.png_medium.png)
© 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:
![](/var/linux_magazin/storage/images/issues/2014/167/bash-tricks/figure-1/624068-1-eng-US/Figure-1_large.png)
# 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.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.