history command in Linux is a built-in shell tool used to display the previously executed command in terminal.

  • By default, it will display the 500 most recent commands, beginning with the oldest.
  • It is possible for users to reuse any listed command without having to type it again.

In this tutorial, you will learn about history command in Linux and the uses of history command with examples.

history --help

history --help

OptionsDescription
-cclear the history list by deleting all of the entries
-d offsetdelete the history entry at position OFFSET
-aappend history lines from this session to the history file
-nread all history lines not already read from the history file
-rread the history file and append the contents to the history list
-wwrite the current history to the history file
-pperform history expansion on each ARG and display the result without storing it in the history list
-sappend the ARGs to the history list as a single entry

1 Simply using the history command displays the whole list of the commands used since the start of the terminal session.  It lists up to the last 1000 commands and each command has a number associated with it.

history

history command in Linux2. Display the limited number of previously used commands

By default history command without any argument or options displays up to the last 1000 commands. You can limit the display of the number of commands that were executed previously. The syntax for displaying the limited number of previously used commands is (It will display the last n commands):

history n

Here I am using history 5. It will display the previous five commands executed.

history 5

history 5

 

3. Repeat/Reuse the command

history command allows users to reuse any listed command (displayed using the history command) without having to type it again. Syntax for repeating the command is (Here n is the number associated with the command while using history command):

!n

Here I am using !2, It will run the second command ufw status (display during using history command) again.

!2

!2

 

4. Adding a () dash after exclamation points or before the command number will repeat the nth command from the last.

 

!-85
!-85

5. Repeat/Reuse the last command

Using double exclamation points repeat and display the most recent command.

!!

!!

6. Repeat command by specifying its string

You can search, display and repeat the command by specifying its name/string. Use an exclamation point and add a string just after that without any space. It will display and repeat the most recently used command that starts with that string.

Here I am using !sudo. It will display the more recently used command starting with sudo and also run that command

 

!sudo

!sudo
7. You can print the command before executing by specifying its name/string. It will only display the most recently used command that starts with that string and will not run that command. Add :p argument after an exclamation point and a string just after exclamation point without any space.

  • It helps us to avoid executing a wrong command.
  • It displays the command without running it.
!sudo:p

!sudop
8. You can search, display and repeat the command by specifying its name/string. It will display and repeat the most recently used command that contains those name/string but may not start with it.

!?ufw

!ufw
9. history command can also be piped with some other Linux commands.  Here it will list all commands that contain ufw.

history | grep ufw

history grep pipe
10. To remove all data from the history library, use history command with -c option.

history -c

history -c
history -c history
11. Instead of deleting all data from history library, you can also remove a particular command. Use history command with -d option followed by command number in history. The syntax is:

history -d command_number

12. By default output of history command displays the previously executed command in terminal along with command number. But, you can also include date and timestamps. For this, you need to edit .bashrc file and include the following line in the .bashrc file:

export HISTTIMEFORMAT="%c "

For editing the .bashrc file, you can use the following command.

sudo nano .bashrc

history nano editor export histtimeformat=%c

history histtime format

 

Sharing is Caring
Scroll to Top