Monday, October 14, 2013

Linux history command(change HISTSIZE variable)

History is a very handy command to see what was typed and executed in a shell for a period of time.
For example, you are troubleshooting Linux server and you want to see the last 20 commands executed.
$ history 20
Or you want to find all commands with sudo word in them.
$ history | grep sudo
You can combine with less, tail and so on, it all depends of your needs.
After search you’ve found a command with number 234 and you want to execute it again.
$ !234
For more details about history command -> man history.
So how this works, how long is history and how to change its size?
Shell command history is stored in a .bash_history file in your /home folder ( cat .bash_history to see all the entries there ).
Number of commands is defined by histsize variable.
Type
$ echo $HISTSIZE
or
$ env | grep HISTSIZE
to see the number of commands that can be stored in this file.
My default number on Oracle Linux is 1000.
But what if you want to store 3000 commands for example, how can you change this variable?
Well quite simply, just use gedit or vi editor.
Change .bash_profile, add HISTSIZE=3000, export this variable to override one in /etc/profile, save file and load the changes with
$ source .bash_profile
Your .bash_profile should look like this
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
………
HISTSIZE=3000
export PATH HISTSIZE
So now you can store up to 3000 command in your history.
echo 'export HISTTIMEFORMAT="%d/%m/%y %T "' >> ~/.bashrc
source ~/.bashrc