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

-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 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):


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

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:


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

sudo nano .bashrc

history nano editor export histtimeformat=%c

history histtime format

Linux command with examples


adduser  |  addgroup  |  alias  |  anacron  |  apt  |  aptitude  |  arp  |  at atq  |  atrm  |  awk


basename banner | batch |  bc  |  bg  |  bzip


cat cal  |  cd  |  chgrp  |  chown  |  cksum  |  chmod  |  clear  |  cmp  |  comm  |  cp


date  |  dd  |  df  |  diff  |  dir  |  dmidecode  |  du


echo  |  eject  |  env  |  exit  |  expr


factor  |  find  |  free


grep  |  groups  |  gunzip  |  gzip


head  |  history  |  hostname  |  hostnamectl  |  htop  |  hwclock  |  hwinfo


id  |  ifconfig  |  ionice  |  iostat  |  ip  |  iptables  |  iw  |  iwlist



kill  |  kmod


last  |  less  |  ln  |  locate  |  login  |  lp  |  ls  |  lshw  |  lscpu  |  lsof  |  lsusb


man  |  mdsum  |  mkdir  |  more|  mv


nano  |  nc  |  neofetch  |  netcat  |  netstat  |  nice  |  nmap  |  nproc




passwd  |  pidof  |  ping  |  pr  |  ps  |  pwd  |  pstree



rdiff-backup  |  reboot  |  rename  |  rm  |  rmdir


scp  |  shred  |  shutdown  |  sleep  |  sort  |  split  |  ssh  |  stat  |  su  |  sudo  |  sum


tac  |  tail  |  talk  |  tar  |  tee  |  time  |  tree  |  top  |  touch  |  tr


unalias  |  uname  |  uniq  |  unzip  |  uptime  |  users


vim  |  vi


w  |  wall  |  watch  |  wc  |  wget  |  whatis  |  whereis  |  which  |  who  |  whoami




yes  |  youtube-dl


zcmp  |  zdiff  |  zip  |  zz

Sharing is Caring
Scroll to Top