Linux History Command Tips

March 5, 2014

I use the Linux command “history” for various reasons like remembering previous commands, to see what was done on a server, to repeat commands and more. This article covers some useful tips I have accumulated to make the command more useful.

When examining the history command output I find it useful to have the date and time of when the command was executed so in my ~/.bashrc file I have:

export HISTTIMEFORMAT='%F %R '

Which provides output like:

$ history
 
1  2014-03-05 21:38 cd /etc
2  2014-03-05 21:38 ls
3  2014-03-05 21:38 ps -aux
4  2014-03-05 21:38 history

The default history file output as well as cache output is 500 lines I tend to increase this by double since I don’t like to lose my early history. Add this to your ~/.bashrc file:

export HISTSIZE=1000
export HISTFILESIZE=1000

*Note HISTSIZE controls the amount during the active session and HISTFILESIZE controls the size of the saved history file

Another annoying thing in your history file output can be duplications of commands in succession, to prevent these unnecessary duplicates I add the following to my ~/.bashrc file:

export HISTCONTROL=ignoredups

Sometimes you accidently get ahead of yourself and mean to type sudo su – but instead enter your password at the prompt which then gets stored in your history cache. When this happens you can do the following command to remove that particular line in the history output.

$ history -d #

* Note where the # is the line number correlating to what you want to delete.

Other times you want to clear all your history for a particular shell session which you can do with the command:

$ history -c

If you want to get rid of all history permanently even from past sessions you can do the following:

$ :> ~/.bash_history
$ history -c

Hope this is as useful to you as it is to me.

Comments are closed.