Useful helpers for the shell

Terminal Tuning

Article from Issue 208/2018
Author(s):

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.

Figure 1: Hstr sorts the Bash history by frequency of execution, not chronologically.

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

Figure 2: As an alternative to ls, Exa provides more information and presents the output in a clearly arranged format with colors.

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.

Figure 3: The Exa tree view allows you to keep track of even the most branched directory structures.

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

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

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Bash History on Steroids

    You can always browse the Bash history using your arrow keys, but Bash's search capabilities are very limited. Enter the clever Bash History Suggest Box.

  • Command Line – Tilix

    A terminal emulator with innovative features makes working from the command line easier and more efficient.

  • McFly

    When it comes to working at the command line, using Bash history effectively can save you time. McFly extends the Bash history's features and helps you find past commands more quickly.

  • New Commands for Old Purposes

    As the standard core Linux commands have become more complex due to revisions, new commands have risen to take their place. Bruce looks at seven of these modern commands, plus a terminal emulator.

  • Bash Alternatives

    Don't let your familiarity with the Bash shell stop you from exploring other options. We take a look at a pair of alternatives that are easy to install and easy to use: Zsh and fish.

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

News