Clear Linux / UNIX BASH Shell Command Line Cache / History

I just noticed tutorial about clearing DNS cache, and there are lots of commands stored in my bash shell cache. Can you tell me the command to clear out my command history or cache from a shell prompt?

Many programs read input from the user a line at a time. The GNU history library can keep track of those lines, arbitrary associate data with each line, and utilize information from previous lines in composing new ones. Bash and other shells may use this history library. The default file is ~/.history or ~/.bash_history to store your bash command history. History is one of the significant features of bash/ksh and POSIX shell.

To display the history list with line numbers, enter

$ history

  4  cd /tmp/
    5  wget
    6  unzip 
    7  su - 
    8  vi /etc/httpd/
    9  service httpd restart
 997  vnstat -m -i eth1
  998  date
  999  yum update
 1000  w
 1001  ~/scripts/clean.cache rss squid web
 1002  history

To see last 10 commands from history, run:
$ history 10
Sample outputs:

 1998  df -H
 1999  dmesg 
 2000  cbz_wp_admin on 
 2001  cbz_wp_admin off
 2002  file_replication --check --status
 2003  ~/bin/purge.cache --key=XXXXXX
 2004  echo $HISTFILE
 2005  echo $HISTFILE 
 2006  cbz_wp_admin --uploads on
 2007  history 10

You can search for the lines that have ‘’ in history by typing the following grep command:
$ history | grep "commmand-to-search"
$ history | grep "'

Another option is to type CTRL+r for searching searching backwards in history.

How to run commands from my bash history

Now you know how to display history list along with line numbers. To execute command # 2002 ( file_replication --check --status), run:
$ !2002
I just recalled previous history command by its number preceded by an exclamation point. To call previous command, type:
$ !!
For example, the following will fail as I forget to add sudo:
$ systemctl restart httpd
Just enter sudo !! and command will re-execute with sudo in front of it:
$ sudo !!

How to scroll through bash history

Simply press UP or DOWN arrow key to Scroll backwards and forwards in history respectively.

How to clear the history, enter:

$ history -c
To delete the history entry at offset i.e. command number 100 from history file:
$ history -d 100
The -c option causes the history list to be cleared by deleting all of the entries. For more information, enter:
$ help history
Sample outputs:

      -c	clear the history list by deleting all of the entries
      -d offset	delete the history entry at offset 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
      -w	write the current history to the history file
    	and append them to the history list
      -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

Explains how to use history bash command to get a history of all commands with date and time listed as well.

This entry is 4 of 5 in the Bash HISTORY Tutorial series. Keep reading the rest of the series:
  1. How to disable bash shell history in Linux
  2. How to delete a single command from history on Linux/Unix Bash shell
  3. Clear Shell History In Ubuntu Linux
  4. Clear Linux / UNIX BASH Shell Command Line Cache / History
  5. Bash History: Display Date And Time For Each Command

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 14 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Morgenster Feb 5, 2008 @ 6:44

    I have found this site very useful which comes with straight forward explanations, thanks a lot. morgen musonza

  • sontn Jun 5, 2009 @ 19:05

    Hi, Thank you very much.

    I also found in your site very useful, I am newcomer to linux operating system.

  • Hardik Mar 23, 2010 @ 17:33



  • Chad May 9, 2011 @ 23:35

    Thanks this is just what I was looking for.

  • Amit Aug 24, 2011 @ 3:58

    There’s also the ~/.bash_history file that still needs to be deleted.

  • A.S.Qureshi Sep 9, 2011 @ 8:35

    Really. Good.

  • Anita singh Oct 10, 2011 @ 10:01

    Its good n helpful

  • Saevar Einarsson Nov 23, 2011 @ 18:30

    Also you can make alias like
    alias clearhist=’rm ~/.bash_history;history -c;exit’
    This clear everything without leaving any sort of trail :)

  • sriram Mar 24, 2012 @ 7:27

    Thank u,

  • venkatesh Jul 11, 2012 @ 6:57

    nice command

  • Sara Sep 20, 2012 @ 18:25

    I’m running konsole on a linux system. Evidently, when I did ‘Settings->Save as Default’ in the konsole shell, there were 30 lines of commands in the history at that time. Now, when I start a new shell and issue first command: history, those 30 lines are displayed. I have tried removing different files in the ~/.kde/share/config/ dir, and this will clear the saved schema but not the history. I have tried ’rm ~/.bash_history;history -c;exit’ as stated above and still no change.
    Does anyone know where that old history is stored and how on earth I can remove it?
    Many thanks

  • Rajandran R Feb 8, 2013 @ 4:33

    Thanks for the command reference. It worked great.

  • Faisal Ali May 21, 2013 @ 21:09

    hi all,
    i realy very very happy of this web site
    because i need some thing related linux
    commands so i just type and get all details
    best regards
    and God Blease you

  • bala Jan 6, 2015 @ 6:18

    how to system arrive the free space percentage for FS

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum