Here are a few ways how you can work with the command history in Linux and save time by not having to type the same long commands over and over.
Of course the most basic way to work with the history is to use the arrows up and down to get the previous/next commands (you can also use ctrl-P and ctrl-N instead of the arrow keys).
If you press ctrl-R, you’ll get in a kind of history search mode. It’s an incremental search, whenever you start typing something, it will show you the last command which matches the part you’ve written:
(reverse-i-search)`erv': service sshd restart
Here I have typed serv and it shows me that the last command with erv I’ve used was service sshd restart.
If you now press return, the command will be executed.
If you press the escape key, the command will be selected and you can modify it before executing it.
If you press ctrl-R again, it will go back to the previous matching command and will loop back to the latest command once you’ve cycled through all matching commands.
In order to rerun the last command starting with let’s say "serv", you can also type the following and press return:
# !serv
To search for a pattern in the command (so not only command starting with the pattern):
# !?at cat info*
!?at will search for the last command contain "at" which in my case was "cat info*".
To repeat the last command:
# !!
Alternatively you can also type:
# !-1
So of course to repeat the second to last command:
# !-2
And so on…
To list the complete history:
# history 1 ifup eth0 2 mtr 8.8.8.8 3 ping 8.8.8.8 4 cd /etc/udev/rules.d/ 5 ls 6 vi 70-persistent-cd.rules 7 vi /etc/i*tab 8 poweroff 9 ifconfig
You can of course also pipe it to a grep to only see the history entries relevant for what you have in mind.
If you now want to repeat ifconfig, you can type the following:
# !9
If you want to see how often you use which command (no matter which arguments you used):
# history | awk ' { print $2; } ' | sort | uniq -c 2 3 alias 11 apt-get 2 aptitude 1 bi 3 cat 6 cd 2 df 1 dpkg-reconfigure 2 /etc/bash.bashrc 1 /etc/init.d/sshd 1 /etc/rc.d/sshd 3 exit 1 halt 4 history 4 history|awk 5 ifconfig 1 ifup 1 iptables 10 l 2 less 5 ls 1 mkdir 1 mtr 1 netstat 1 passwd 5 ping 1 poweroff 1 ps 2 pwd 1 rcsshd 1 ser 3 service 1 setupcon 2 su 11 vi
If you want to do it also considering the arguments:
# history | awk ' { $1=""; print $0; } ' | sort | uniq -c
For both previous command you can also pipe it to "sort -r -n" to get a list sorted by the number of occurrences of the command instead of sorting it alphabetically by command.
You can also limit the number of entry displayed by the history command:
# history 5 139 ls -a 140 less /etc/profile 141 l /etc/profile.d/ 142 less /etc/profile.d/bash_completion.sh 143 history 5
You can clear the history using:
# history -c
The history is stored in a file per user: ~/.bash_history
Where the history is stored and how long it is defined by the following variables:
# echo $HISTFILE /root/.bash_history # echo $HISTFILESIZE 500 # echo $HISTSIZE 500
If you want to use another file or have a longer history you can use EXPORT to modify them or modify your .bash_profile, .bashrc or .profile files to make it more permanent.
Another good trick is to use the variable HISTIGNORE to make sure that some commands are not stored in the history:
# export HISTIGNORE="ls:pwd:dir" # cd /tmp # ls # cd /opt # dir # cd /usr # pwd # history 4 145 cd /tmp 146 cd /opt 147 cd /usr 148 history 4
As you can see it didn’t record ls, dir and pwd.