How to use history command in Linux

History Command in Linux Explained in Depth

When you are using Linux command line frequently, using the history command effectively can be a major productivity boost.

In Linux, there is a very useful command to show you all of the last commands that have been recently used. The command is simply called history.

Display Timestamp

Typically when you type history in command line, by default you see a number followed by the commands you’ve used recently. For auditing purpose, it may be beneficial to display the timepstamp along with the command as shown below.

To enable timestamp in history command output, you must configure the HISTTIMEFORMAT Bash variable.

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

Searching Command History

This may be your most frequently used feature of the history command. 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.

  1. Press Control+R and type the keyword.
  2. 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 example, 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.

Then re-execute it again by just hitting Enter.

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

Repeat the Last Executed Command

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

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

Limit the Number of History Items

This is controlled by the HISTSIZE and HISTFILESIZE built-in Bash shell variables. Append the following two lines to your ~/.bashrc file and log out and then log back in.

HISTSIZE=1500
HISTFILESIZE=1500

As a result, now history is set to store 1500 commands.

Change the History File Name

By default, history is stored in ~/.bash_history file.

Add the following line to the .bash_profile file and relogin to the Bash shell, to store the history command in .my_commandline file instead of .bash_history file.

HISTFILE=/root/.my_commandline

This getting used when you want to track commands executed from different terminals using different history file name.

Eliminate the Continuous Repeated Entry from History

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

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

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

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

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

Ignore Specific Commands from the History

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]

For more about the history command in Linux, consult its manual page.

Bobby Borisov
Bobby Borisov

Bobby is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *