Linux history Command Tutorial

Linux command-line interface or bash is one of the most strong and popular parts of it. A regular system administrator spends a lot of his time on the terminal and types a lot of commands. Daily operations generally repeat where previously executed commands are generally executed again and again. The history command is used to list previously executed commands in a bash shell.

history Command Syntax

The history command has the following syntax.

history OPTIONS
  • OPTIONS are set to display specified number of history commands. OPTIONS is optional.

List Last 1000 Commands

The history command lists all previously executed commands. The default size for the history is 1000 which means no more than 1000 commands can be stored in history. When the history command is executed like below it will list all the last 1000 commands.

List Last 1000 Commands

From the output we can see that the commands and numbers are printed. These numbers are used to execute or remove them from the history.

List Last Specified Number of Commands

Even last 1000 commands are listed with the history command we can set a limit for the displayed command count. We will just provide the number of the commands we want to display from the history.

history 10
List Last Specified Number of Commands

Execute Specified Command Again

One of the most useful feature of the history command is the ability to execute previously executed command. The command can be executed with its history number which is listed previously. The ! sign is used to specify execute historical command with the specified ID. In the followig example we will execute the command numbered as 333.


The output will be like below where the command is printed to the terminal.

sudo lsof -i TCP:1-2048
 [sudo] password for ismail: 
 systemd-r  712 systemd-resolve   13u  IPv4  35311      0t0  TCP localhost:domain (LISTEN)
 sshd       916            root    3u  IPv4  39812      0t0  TCP *:ssh (LISTEN)
 sshd       916            root    4u  IPv6  39814      0t0  TCP *:ssh (LISTEN)
 apache2    971            root    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 apache2   5619        www-data    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 apache2   5620        www-data    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 apache2   5621        www-data    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 apache2   5622        www-data    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 apache2   5623        www-data    4u  IPv6  42125      0t0  TCP *:http (LISTEN)
 cupsd     5625            root    6u  IPv6 134296      0t0  TCP ip6-localhost:ipp (LISTEN)
 cupsd     5625            root    7u  IPv4 134297      0t0  TCP localhost:ipp (LISTEN)

Only Print Old Command

By using the ! sign the historical command is executed. We can also add :p which will not execute the command and only print to the screen. We can copy and paste the printed command into the terminal for execution. This is a more controlled way to execute historical commands.


Run Most Recent Command

By using the double exclamation mark !! the most recent command can be executed directly. This is equal to the !1.


Filter Command History

Historic commands can be filtered by using the grep command. During daily usage, we run a lot of different or similar commands which are very hard to remember or identify. By providing a specific term the historic commands can be filtered and searched. In the following example, we will list commands those contain “login”.

history | grep login

The output is like below.

337  cat /etc/login.defs 
338  gedit /etc/login.defs 
342  history | grep login

Search In Command History

History can be searched in an interactive manner. Just press the CTRL+R keys which will start the interactive search mode where we can type some characters where the matched command is displayed. In the following example, we type “login” which will look below.

(reverse-i-search)`login': history | grep login

Press Enter to executed listed command or Press ESC just put the command into the terminal where you can change some part of the command.

READ  How To Install Wireshark In Linux (Ubuntu, Debian, Mint, CentOS)?

Remove Specific Lines From History

Even historic commands are very useful we may want to remove some of them for different reasons. For example, if we have typed a command which contains sensitive information like username, password, or token we should remove this line. The -d option is used to remove the specified line number from the history.

history -d 233

Remove All History

Deleting history commands line by line is a trivial task. All history can be deleted by using the -c option. This clears the history.

history -c

Leave a Comment