Clear Linux / UNIX BASH Shell Command Line Cache / History

last updated in Categories , , , , , , ,

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

....
..
  4  cd /tmp/
    5  wget http://downloads.wordpress.org/plugin/cleaner-dashboard.1.1.zip
    6  unzip cleaner-dashboard.1.1.zip 
    7  su - 
    8  vi /etc/httpd/testing.lan.cyberciti.biz.conf
    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 ‘cf.purge.sh’ in history by typing the following grep command:
$ history | grep "commmand-to-search"
$ history | grep "cf.purge.sh'

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
    	list
      -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 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use "SPLASH15" coupon code. Offer expires on August 27, 2018
training.linuxfoundation.org

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14 comment

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

  2. Hi, Thank you very much.

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

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

  4. 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

  5. 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

    Have a question? Post it on our forum!