img

Clear Linux / UNIX BASH Shell Command Line Cache / History

September 8, 2019

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.sxi.io.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

-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

Posted by: SXI ADMIN

The author is the creator of SXI LLC 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.

Article Tags:
Article Categories:
How To

Comments are closed.