Useful History Command Examples in Linux

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 list of commands which you are executed previously.

Here, we learn some most useful history commands examples and also the configuration of history command.

1.Display timestamp using HISTTIMEFORMAT :

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 purpose, it would be beneficial to display command with a timestamp. HISTTIMEFORMAT will help to display commands along with timestamp as shown below,

 $ export HISTTIMEFORMAT='%F %T '

$ history | less

If you want to display commands with a timestamp on permanent basis then you should add “export HISTTIMEFORMAT=’%F %T ‘” into the end of the “~/.bashrc” file.

$ vim ~/.bashrc
##End of file
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 value of how much commands to be displayed/store in history command “HISTSIZE

If you want to store more than 500 commands in history then append below two lines to the .bashrc file. 500 is the default value to store commands in history.

Relogin to the bash shell again to see the change.

vim .bashrc

HISTSIZE=1000
HISTFILESIZE=1000

4. Run the previous command with the specific word

Type ! with first 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. Change default name of history file using HISTFILE

By default, history is stored in .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 using HISTCONTROL

This is 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 using HISTCONTROL

Its 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 previous history using option -c

This option will remove all the previous history but will keep the history for moving forward.

$ history -c

9. Disable history using HISTSIZE

If you want to disable history then set the HISTSIZE to 0 as shown below,

$ export HISTSIZE=0

$ history

10. Not to store particular command in history using HISTCONTROL

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

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 techthings.org for more such valuable articles.

Leave a Reply

Your email address will not be published. Required fields are marked *