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.
- How to disable bash shell history in Linux
- How to delete a single command from history on Linux/Unix Bash shell
- Clear Shell History In Ubuntu Linux
- Clear Linux / UNIX BASH Shell Command Line Cache / History
- Bash History: Display Date And Time For Each Command
🐧 14 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
I have found this site very useful which comes with straight forward explanations, thanks a lot. morgen musonza
Hi, Thank you very much.
I also found in your site very useful, I am newcomer to linux operating system.
Perfect!!!
Thanks.
Thanks this is just what I was looking for.
There’s also the ~/.bash_history file that still needs to be deleted.
Really. Good.
Its good n helpful
Also you can make alias like
alias clearhist=’rm ~/.bash_history;history -c;exit’
This clear everything without leaving any sort of trail :)
Thank u,
nice command
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
Thanks for the command reference. It worked great.
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
how to system arrive the free space percentage for FS