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