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.
|clear the history list by deleting all of the entries
|delete the history entry at position OFFSET
|append history lines from this session to the history file
|read all history lines not already read from the history file
|read the history file and append the contents to the history list
|write the current history to the history file
|perform history expansion on each ARG and display the result without storing it in the history list
|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.
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.
history | grep ufw
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
Linux command with examples
date | dd | df | diff | dir | dmidecode | du
echo | eject | env | exit | expr
grep | groups | gunzip | gzip
kill | kmod
man | mdsum | mkdir | more| mv
passwd | pidof | ping | pr | ps | pwd | pstree
rdiff-backup | reboot | rename | rm | rmdir
tac | tail | talk | tar | tee | time | tree | top | touch | tr
unalias | uname | uniq | unzip | uptime | users
vim | vi
yes | youtube-dl
zcmp | zdiff | zip | zz