Top 10 examples of history command for Linux distros
by Prashant · Published · Updated

Examples of History Command
In this post, we are going to understand some useful History command examples. When you are using Linux often, it’s very difficult to remember all commands all the time. History command will provide you a list of commands which you are executed previously.
Here, we learn some most useful history commands examples and also the configuration of history command.
Let’s first enable the most important thing in history command configuration i.e. “Timestamp in History Command”.
1. Display Timestamp in History Command :
Typically when you use history command, it will only display the commands which are executed. But it will not provide timestamp when the command was executed.
sometimes for investigation purposes, it would be beneficial to display command with a timestamp. HISTTIMEFORMAT will help to display commands along with timestamp as shown below,
Temporary Add Timestamp
Run the export command to temporary add timestamp setting,
$ export HISTTIMEFORMAT='%F %T '
$ history | less
Permanent Add Timestamp
If you want to display commands with a timestamp on a permanent basis then you should add “export HISTTIMEFORMAT=’%F %T ‘” into the end of the “~/.bashrc” file.
$ vim ~/.bashrc
export HISTTIMEFORMAT='%F %T '
$ history | less
2. Repeat previously used command quickly
Most of the time we end up using the same command for various reasons. Here, you can use 4 different ways to execute previous commands.
$ UP Arrow to View previous command and press enter to execute.
$ Type !! and press enter. it will execute last enter command.
$ Type !-1 and press enter. it will execute last enter command.
$ Cntrl+P will show previously used commands and press enter to execute the command.
3. Set a limit to store commands in the history
By Default, history command shows up to 1000 old commands in a list. If you want to increase the limit then make below changes in “.bashrc file”.
vim ~/.bashrc
HISTSIZE=2000
HISTFILESIZE=2000
4. Run the previous command with the initial word
Type ! with the initial few letters of command which you would like to execute.
$ !ps
ps aux | grep apache
root 9568 0.0 0.0 14224 928 pts/4 S+ 19:25 0:00 grep --color=auto apache
$ !pi
ping google.com
5. The change default name of History File
By default, history is stored in the .bash_history file.
HISTFILE will help to store history to another file instead of .bash_history. I am not able to figure out the practical use for this.
If you have a good reason to change the name of the history file, please share it with me, as I’m interested in finding out how you are using this feature.
$ vim ~/.bash_profile
HISTFILE=/root/.oldcommands
6. Remove repetitive commands from history command
This is a very useful option to remove repetitive or same commands from history.
Below example, you will see I have use ls command 3 times. To remove duplicates use HISTCONTROL to erase duplicates.
$ ls
$ ls
$ ls
$ history | tail -4
44 ls
45 ls
46 ls [Note that there are three ls commands in history, after
executing ls 3 times as shown above]
47 history | tail -4
$ export HISTCONTROL=erasedups
$ ls
$ ls
$ ls
$ history | tail -3
56 export HISTCONTROL=erasedups
57 ls [Note that there is only one ls command in the history, even after
executing ls 3 times as shown above]
58 history | tail -4
7. Exclude repetitive commands from history command
It’s the same as erasing the duplicates but here you need to use HISTCONTROL=ignoredups instead of erasedups.
$ ls
$ ls
$ ls
$ history | tail -4
44 ls
45 ls
46 ls [Note that there are three ls commands in history, after
executing ls 3 times as shown above]
47 history | tail -4
$ export HISTCONTROL=ignoredups
$ ls
$ ls
$ ls
$ history | tail -3
56 export HISTCONTROL=ignoredups
57 ls [Note that there is only one ls command in the history, even after
executing ls 3 times as shown above]
58 history | tail -4
8. Remove all the history
This option will remove all the previous history but will keep the history for moving forward.
$ history -c
9. Disable history
If you want to disable history then set the HISTSIZE to 0 as shown below,
$ export HISTSIZE=0
$ history
10. Don’t store the specific command in History
You can instruct history not to remember or ignore the command by using HISTCONTROL to ignorespace.
It will not store commands which are executed with space in front of that command.
$ export HISTCONTROL=ignorespace
$ ls
$ cd /opt
$ /etc/init.d/apache2 start [Note that there is a space at the beginning of service,to ignore this command from history]
$ history | tail -3
$ ls
$ cd /opt
$ history | tail -3
You have seen some interesting settings about the history command which can be used to reduce your repetitive work on Linux distro with the help of history command.
I hope you like the article if you find any difficulties using History command then please do comment your queries or problem via the comment section, till then stay tuned to Linuxgrow.com for more such valuable articles.
Read: Sysadmin daily useful ps aux command examples in Linux