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.
|-c||clear the history list by deleting all of the entries|
|-d offset||delete the history entry at position OFFSET|
|-a||append history lines from this session to the history file|
|-n||read all history lines not already read from the history file|
|-r||read the history file and append the contents to the history list|
|-w||write the current history to the history file|
|-p||perform history expansion on each ARG and display the result without storing it in the history list|
|-s||append 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.
2. 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):
Here I am using
history 5. It will display the previous five commands executed.
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):
Here I am using
!2, It will run the second command
ufw status (display during using history command) again.
4. Adding a (–) dash after exclamation points or before the command number will repeat the nth command from the last.
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
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.
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.
9. history command can also be piped with some other Linux commands. Here it will list all commands that contain ufw.
history | grep ufw
10. To remove all data from the history library, use
history command with
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