Useful helpers for the shell
Terminal Tuning
Some users have been working at the command line for years and wouldn't change a thing about it, but others ride a wave of constant innovation. We explore some helpful tools for extending and expanding your shell experience.
The Bash shell is one of the central building blocks of any Linux system. Beginners are often a little lost, but they know they'll never become experts without some basic knowledge of the mystical command line. Advanced users love the convenience of navigating their Linux life using simple and practical text commands. For many users, the Bash shell is like everything else in Linux: Sure it's great, but it could always get better.
With a few tricks and some useful helper tools, you can add new capabilities and features to the venerable Bash shell. This article explores some favorite terminal tuning tricks.
Better History
Bash stores each command in the ~/.bash_history
file. You can display the command history by typing history
. Bash searches the history if you press Ctrl+R in the terminal and then enter a search term. If you repeat the Ctrl+R key combination, the shell jumps to the next hit; Ctrl+Shift+R takes it in the other direction.
Searching helps you dig up old commands so you don't have to type them again. The search function always sorts the hits from new to old. The last entered commands always appear first when searching with Ctrl+R.
Artful use of the Bash search feature can lead to some time-saving tricks. For instance, if you enter a long command that you don't want to have to type again, add a short comment string to the end of it using a hash tag. For example, instead of long command
, enter long command #quick
. The shell interprets the text after the hash tag as a comment and does not execute it. However, if you want to access the command later, just type Ctrl+R and quick
, and Bash will jump to the desired command.
Hstr
Perhaps you think it would be more convenient if the most frequently used commands were to appear first instead of last. Exactly this and much more is possible with Hstr [1], also known as the Bash and Zsh Shell History Suggest Box, a special tool designed to let you "easily view, navigate, search, and manage your command history."
Not all distributions include Hstr directly in the package archives; for example, Ubuntu requires you to integrate a PPA. Installation instructions for several Linux distros are available on the Hstr GitHub project page [2].
After you set up Hstr, transfer its default configuration to the configuration file for the Bash shell and reload it so that the changes takes effect without a new logon (Listing 1). Hstr uses the hh
command.
Listing 1
Hstr Configuration
$ hh --show-configuration >> ~/.bashrc $ source ~/.bashrc
Now when you launch a command search in the shell using Ctrl+R, Hstr will start automatically: Instead of just one command, you will see an Ncurses interface with the most frequently entered commands. If you type in a search term, Hstr filters out the corresponding hits and sorts them according to their frequency (Figure 1). Use the arrow keys to scroll through the list; pressing the Enter key executes the currently selected command. Use a tabulator to transfer the command to the shell so that you can still make changes.
If a command with security-critical data slips into your history (e.g., the password when transferring data with ftp
), you can remove this command from the Bash history by pressing Del. Frequently used commands can be marked as favorites with Ctrl+F and brought back to life by changing the Hstr sort order with Ctrl+Shift+7.
Better Lists
The ls
command, which lists the content of a directory, is one of the most common Bash commands. If you're looking for more information, try extending the simple ls
command with ls -alh
. The -alh
options turn the simple list into a complete overview, including file permissions, ownership, file size, and more. The command also outputs hidden files and directories.
The ls
command also supports colors, but usually only color highlighting for directories and links. The alternative Exa [3], which is written in Rust, offers significantly more convenience. Only a few distributions offer Exa in their package sources, although you will find it in the Arch User Repository (AUR). If your distro doesn't provide a package for Exa, check out the project homepage, where the developer provides a zipped binary for 64-bit systems, which you can simply copy as exa
to /usr/local/bin/
or ~/bin/
.
The output of the exa
command corresponds to the output of ls
, except that Exa adds more colors to the overview (Figure 2). For example, it highlights archives in red, images in light purple, videos in dark purple, backup copies in gray, and directories in light blue. Exa gets even more interesting when you enable further options (see Table 1). For example, with exa -bghHlHlS
, Exa displays the file permissions, including file sizes, affiliations, and the last modification data, in a neatly sorted list.
Table 1
Exa Important Options
Switch | Long Form | Comment |
---|---|---|
Display Options |
||
-1 |
--oneline |
Shows each file and folder in a new line |
-l |
--long |
Displays metadata, such as file permissions and size |
-R |
--recurse |
Lists the contents of subdirectories recursively (depth option: --level=<level_number>) |
-T |
--tree |
Displays a tree structure (depth option: --level=<level_number>) |
|
--color <when> |
Color output is never, always, or automatic |
|
--color-scale |
Highlights the display of the file size in color for large files |
Filtering and Sorting Options |
||
-a |
--all |
Shows hidden files and folders |
-d |
--list-dirs |
Treats directories like files |
-r |
--reverse |
Reverses the sort order |
-s <option> |
--sort=<option> |
Sorts by name, size, created, newest, or oldest, among other options |
|
--group-directories-first |
Lists directories before files |
Other colors help you keep track of things such as file permissions. If you decide on a version of the command you like best, create an alias for it with
alias ll='exa -bghHlS'
and place the alias in the ~/.bashrc
file. The preceding alias would let you call the command by simply typing ll
.
Exa's ability to display entire folder structures recursively in a tree view is also practical (Figure 3). To display a tree, supplement the call with exa --tree
or exa -T
. You can limit the tree depth with the --level=<n>
option, where <n>
is the number of levels you want to display. Alternatively, switch off the tree view and let Exa list just the subfolders with the -R
or --recurse
switch. Again, you can restrict the depth with the --level
option.
The shortcuts for sorting the output also make everyday life in the shell easier. For example, to find the largest files in a folder quickly, call Exa with:
exa -lr --sort=size
The -r
switch reverses the order of the output so that the largest files are at the beginning of the list.
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
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.