Be the Master of Your (Bash) History

How to use history command in Linux

When you are using Linux command line frequently, using the bash history effectively can be a major productivity boost. In fact, once you have mastered the examples bellow, you’ll find using command line more enjoyable and fun.

Display timestamp using HISTTIMEFORMAT

Typically when you type history from command line, it displays the command# and the command. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.

export HISTTIMEFORMAT='%F %T '
history | more
1  2020-06-25 19:02:39 systemctl restart network
2  2020-06-25 19:02:43 exit
3  2020-06-25 19:02:47 id
4  2020-06-25 19:02:56 cat /etc/hosts

Search the history using Control+R

This may be your most frequently used feature of history. When you’ve already executed a very long command, you can simply search history using a keyword and re-execute the same command without having to type it fully.

  • Press Control+R and type the keyword.
  • Press enter when you see your command, which will execute the command from the history.

In the following example, I searched for host, which displayed the previous command cat /etc/hosts in the history that contained the word host.

(reverse-i-search)`host': cat /etc/hosts
cat /etc/hosts
#<ip-address>	<hostname.domain.org>	<hostname>
127.0.0.1	localhost.localdomain	localhost	server1
::1		localhost.localdomain	localhost	server1

Sometimes you want to edit a command from history before executing it. For e.g. you can search for systemctl, which will display systemctl restart network from the command history, select this command (pressing ESC key) and change the restart to stop and re-execute it again as shown below.

(reverse-i-search)`systemctl': systemctl stop network
systemctl stop network

Repeat previous command quickly using 4 different methods

Sometime you may end up repeating the previous commands for various reasons. Following are the 4 different ways to repeat the last executed command.

  1. Use the up arrow to view the previous command and press enter to execute it.
  2. Type !! and press enter from the command line
  3. Type !-1 and press enter from the command line.
  4. Press Control+P will display the previous command, press enter to execute it.

Execute a specific command from history

In the following example, If you want to repeat the command #4, you can do !4 as shown below.

history | more
systemctl restart network
exit
id
cat /etc/hosts
!4
cat /etc/hosts
#<ip-address>	<hostname.domain.org>	<hostname>
127.0.0.1	localhost.localdomain	localhost	server1
::1		localhost.localdomain	localhost	server1

Execute previous command that starts with a specific word

Type ! followed by the starting few letters of the command that you would like to re-execute. In the following example, typing !ps and enter, executed the previous command starting with ps, which is ps aux | grep yp.

!ps
ps aux | grep yp
root     16947  0.0  0.1  36516  1264 ?        Sl   13:10   0:00 ypbind
root     17503  0.0  0.0   4124   740 pts/0    S+   19:19   0:00 grep yp

Control the total number of lines in the history using HISTSIZE

Append the following two lines to the .bash_profile and relogin to the bash shell again to see the change. In this example, 1500 command will be stored in the bash history.

vi ~/.bash_profile
HISTSIZE=1500
HISTFILESIZE=1500

Change the history file name using HISTFILE

By default, history is stored in ~/.bash_history file. Add the following line to the .bash_profile and relogin to the bash shell, to store the history command in .my_commandline file instead of .bash_history file. This getting used when you want to track commands executed from different terminals using different history file name.

vi ~/.bash_profile
HISTFILE=/root/.my_commandline

Eliminate the continuous repeated entry from history using HISTCONTROL

In the following example pwd was typed three times, when you do history, you can see all the 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.

history | tail -4
44  pwd
45  pwd
46  pwd
47  history | tail -4
export HISTCONTROL=ignoredups
history | tail -3
56  export HISTCONTROL=ignoredups
57  pwd
58  history | tail -4

Erase duplicates across the whole history using HISTCONTROL

The ignoredups shown above removes duplicates only if they are consecutive commands. To eliminate duplicates across the whole history, set the HISTCONTROL to erasedups as shown below.

export HISTCONTROL=erasedups
pwd
systemctl restart network
history | tail -3
38  pwd
39  systemctl restart network
40  history | tail -3
ls -l
systemctl restart network
history | tail -6
35  export HISTCONTROL=erasedups
36  pwd
37  history | tail -3
38  ls -l
39  systemctl restart network
40  history | tail -6

Force history not to remember a particular command using HISTCONTROL

When you execute a command, you can instruct history to ignore the command by setting HISTCONTROL to ignorespace AND typing a space in front of the command as shown below. I can see lot of junior sysadmins getting excited about this, as they can hide a command from the history. It is good to understand how ignorespace works. But, as a best practice, don’t hide purposefully anything from history.

export HISTCONTROL=ignorespace
ls -ltr
pwd
 systemctl restart network #Note that there is a space at the beginning of service, to ignore this command from history
history | tail -3
67  ls -l
68  pwd
69  history | tail -3

Clear all the previous history using option -c

Sometime you may want to clear all the previous history, but want to keep the history moving forward.

history -c

Substitute words from history commands

When you are searching through history, you may want to execute a different command but use the same parameter from the command that you’ve just searched.

In the example below, the !!:$ next to the vi command gets the argument from the previous command to the current command.

ls nginx.conf
nginx.conf
vi !!:$
vi nginx.conf

In the example below, the !^ next to the vi command gets the first argument from the previous command (i.e cp command) to the current command (i.e vi command).

cp nginx.conf nginx.conf.bak
vi  !^
vi nginx.conf

Substitute a specific argument for a specific command

In the example below, !cp:2 searches for the previous command in history that starts with cp and takes the second argument of cp and substitutes it for the ls -l command as shown below.

cp ~/longname.txt /really/a/very/long/path/long-filename.txt
ls -l !cp:2
ls -l /really/a/very/long/path/long-filename.txt

In the example below, !cp:$ searches for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument as shown above) of cp and substitutes it for the ls -l command as shown below.

ls -l !cp:$
ls -l /really/a/very/long/path/long-filename.txt

Disable the usage of history using HISTSIZE

If you want to disable history all together and don’t want bash shell to remember the commands you’ve typed, set the HISTSIZE to 0 as shown below.

export HISTSIZE=0
history
# Note that history did not display anything

Ignore specific commands from the history using HISTIGNORE

Sometimes you may not want to clutter your history with basic commands such as pwd and ls. Use HISTIGNORE to specify all the commands that you want to ignore from the history. Please note that adding ls to the HISTIGNORE ignores only ls and not ls -l. So, you have to provide the exact command that you would like to ignore from the history.

export HISTIGNORE="pwd:ls:ls -ltr:"
pwd
ls
ls -l
systemctl restart network
history | tail -3
79  export HISTIGNORE="pwd:ls:ls -l:"
80  systemctl restart network
81  history
[Note that history did not record pwd, ls and ls -l]

Leave a Reply

Your email address will not be published.

Latest from Tips & Tricks