When using the Linux command line often, making good use of history can be an important performance boost. In fact, once you have mastered the 15 examples we have provided here, you will find using the command line more enjoyable and fun.
1. Timestamp display using HISTTIMEFORMAT
Typically when you enter history from the command line, it will output command # and command. For auditing purposes, it can be useful to display the timepstamp along with the command as shown below.
# export HISTTIMEFORMAT='%F %T ' # history | more 1 2017-03-19 11:14:15 service network restart 2 2017-03-19 11:14:15 exit 3 2017-03-19 11:14:15 id 4 2017-03-19 11:14:15 cat /etc/redhat-release
2. Select a story with Control + R
We strongly believe this may be the most commonly used history function. When you have already executed a very long command, you can simply look at the search history using a keyword and rerun the same command without having to enter it all over again. Press Control + R and enter a keyword… In the following example, we were looking for redwhich was displayed in the previous command “cat / etc / redhat-release”In the story that contained the word red.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search)`red': cat /etc/redhat-release [Note: Press enter when you see your command, which will execute the command from the history] # cat /etc/redhat-release Fedora release 9 (Sulphur)
Sometimes you want to edit a command from history before executing it. For example, you can search httpdwhich will display service httpd stop from command history, select that command and change stop to start and restart it again as shown below.
# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search)`httpd': service httpd stop [Note: Press either left arrow or right arrow key when you see your command, which will display the command for you to edit, before executing it] # service httpd start
3. Repeat the previous command quickly using 4 different methods
You may end up repeating previous commands someday for a variety of reasons. Below are 4 different ways to repeat the last executed command.
- Use up arrowto view the previous command and press Enter to run it.
- Enter !! and press enter from command line
- Enter !-1 and press enter from the command line.
- Click on Ctrl + P, it will display the previous command, press the ENTER key to execute it.
4. Execute a special command from history
In the following example, if you want to repeat command # 4, you can do this: !4as shown below.
# history | more 1 service network restart 2 exit 3 id 4 cat /etc/redhat-release # !4 cat /etc/redhat-release Fedora release 23
5. Run the previous command that starts with a specific word
A type ! followed by a few letters of the command that you would like to rerun. In the following example, typing! Ps followed by the enter key after executing the previous command starting with ‘ps aux | grep yp ‘.
# !ps ps aux | grep yp root 16127 0.0 0.1 36236 1264 ? Sl 13:10 0:00 ypbind root 17563 0.0 0.0 4454 740 pts/0 S+ 19:19 0:00 grep yp
6. Controlling the total number of lines in history using HISTSIZE
Add the following two lines to .bash_profile and log in again to the bash shell to see the changes. In this example, only 450 commands will be saved in the Bash history.
# vi ~/.bash_profile HISTSIZE=450 HISTFILESIZE=450
7. Change the file name using the HISTFILE history
By default, history is stored in a file ~ / .bash_history… Add the following line to your .bash_profile file and re-enter the Bash shell to save the command in the .commandline_destroyer history file instead of the .bash_history file. We are still figuring out a practical application for this. We can see this to get used to if you want to keep track of commands executed from different terminals using a different history file name.
# vi ~/.bash_profile HISTFILE=/root/.commandline_destroyer
If you have a good reason to change the name of the history file, please share with us, as we are interested to know how you use this feature.
8. Eliminate continuous re-recording from history with HISTCONTROL
In the following example pwd was printed three times, when you make history, you can see all 3 continuous occurrences of it. To eliminate duplicates, set HISTCONTROL to ignoredups as shown below.
# pwd # pwd # pwd # history | tail -4 44 pwd 45 pwd 46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above] 47 history | tail -4 # export HISTCONTROL=ignoredups # pwd # pwd # pwd # history | tail -3 56 export HISTCONTROL=ignoredups 57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above] 58 history | tail -4
9. Erase duplicates across the entire history using HISTCONTROL
The ignoredups shown above removes duplicates only if they are sequential commands. To eliminate duplicates across the entire history, set HISTCONTROL to erasedups as shown below.
# export HISTCONTROL=erasedups # pwd # service httpd stop # history | tail -3 38 pwd 39 service httpd stop 40 history | tail -3 # ls -ltr # service httpd stop # history | tail -6 35 export HISTCONTROL=erasedups 36 pwd 37 history | tail -3 38 ls -ltr 39 service httpd stop [Note that the previous service httpd stop after pwd got erased] 40 history | tail -6
10. Unable to recall a specific command using HISTCONTROL
When executing a command, you can specify the history of ignored commands by setting HISTCONTROL to ignorespace and entering a space before the command, as shown below. I see a lot of novice sysadmins worried about this because they might hide the command from history. It’s good to understand how ignorespace works. But, as a best practice, they do not purposefully hide anything from history.
# export HISTCONTROL=ignorespace # ls -ltr # pwd # service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history] # history | tail -3 67 ls -ltr 68 pwd 69 history | tail -3
11. Clear all previous histories with the -c option
Someday you can clear all previous history but want to keep history.
# history -c
12. Substitution of a word from command history
When searching through history, you can run a different command, but use the same parameter from the command you were just looking for.
In the example below !!: $ next to the vi command gets the argument from the previous command to the current command.
# ls andreyex-ks.cfg andreyex-ks.cfg # vi !!:$ vi andreyex-ks.cfg
In the example below ! ^ next to the vi command we get the first argument of the previous command (i.e. the cp command) to the current command (i.e. the vi command).
# cp andreyex-ks.cfg andreyex-ks.cfg.bak andreyex-ks.cfg # vi !^ vi andreyex-ks.cfg
13. Substitute a specific argument for a specific command.
In the example below, ! cp: 2 searches the history for the previous command that starts with cp and takes the second argument cp and replaces it with the -l of the ls command, as shown below.
# cp ~/longname.txt /really/a/very/long/path/long-filename.txt # ls -l !cp:2 ls -l /really/a/very/long/path/long-filename.txt
In the example below, ! cp: $ looks for the previous command in history that starts with cp and takes the last argument (in this case, which is also the second argument, as shown above) cp and replaces it in the Ls -l command, as shown below.
# ls -l !cp:$ ls -l /really/a/very/long/path/long-filename.txt
14. Disable history usage using HISTSIZE
If you want to turn off history all at once and don’t want the Bash shell to remember the commands you type, set HISTSIZE to 0 as shown below.
# export HISTSIZE=0 # history # [Note that history did not display anything]
15. Ignore specific commands from history using HISTIGNORE
Sometimes, you can’t clutter your history with basic commands like pwd and ls. Use HISTIGNORE to list all commands you want to ignore from history. Note that adding ls to HISTIGNORE only ignores ls and not LS -l. Thus, you must provide the exact command that you would like to ignore from history.
# export HISTIGNORE="pwd:ls:ls -ltr:" # pwd # ls # ls -ltr # service httpd stop # history | tail -3 79 export HISTIGNORE="pwd:ls:ls -ltr:" 80 service httpd stop 81 history [Note that history did not record pwd, ls and ls -ltr]