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
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.
-
DebConf24 to be Held in South Korea
Busan will be the location of the latest DebConf running July 28 through August 4