There are several ways to improve how you troubleshoot a system. One of the things I consider to be important is the BASH history. Here you can see what commands were executed. Though as default you can’t see when, or ignore certain things, like duplicate commands. Here’s a little guide to show you how.
HISTTIMEFORMAT
A simple yet effective little trick is to change the format of the output the command history spits out. In my case it comes to good use when troubleshooting a system, wanting to find out if errors in the system log match up with commands executed at that time. To enable the feature in your current terminal, run:
1
|
export HISTTIMEFORMAT=“%h %d %H:%M:%S> “
|
So the output from history that was looking like this:
1
2
3
4
5
|
[jorge@ashitaka ~]$ history
1 ls
2 echo “Hello”
3 pwd
...
|
Now looks like this:
1
2
3
4
5
|
[jorge@ashitaka ~]$ history
1 Mar 24 12:53:35> ls
2 Mar 24 12:53:36> echo “Hello”
3 Mar 24 12:53:37> pwd
...
|
HISTCONTROL
The variable HISTCONTROL can do a few things, like ignoring duplicates if they are in a row and erasing duplicates. The difference here is that by ignoring duplicates, it will remove duplicates only if they are in a row, while erasing duplicates, it will remove previous commands from the history file. Let’s give an example:
ignoredups
First run:
1
|
export HISTCONTROL=ignoredups
|
The output that looked like this:
1
2
3
4
5
6
7
8
|
[jorge@ashitaka ~]$ history
1 pwd
2 echo “Hello”
3 ls –l
4 ls –h
5 pwd
6 pwd
...
|
Will now look like this:
1
2
3
4
5
6
7
|
[jorge@ashitaka ~]$ history
1 pwd
2 echo “Hello”
3 ls –l
4 ls –h
5 pwd
...
|
erasedups
erasedups means that if you executed the same command previously, that entry will be erased, substituted by the newly run command. Generally not recommended though. If you still wish to do so, it can be done by running the following line:
1
|
export HISTCONTROL=erasedups
|
So a history that looked like this:
1
2
3
4
5
6
7
|
[jorge@ashitaka ~]$ history
1 pwd
2 echo “Hello”
3 ls –l
4 ls –h
5 pwd
...
|
After the export, will now look like this when we run the command pwd again:
1
2
3
4
5
6
|
[jorge@ashitaka ~]$ history
1 echo “Hello”
2 ls –l
3 ls –h
4 pwd
...
|
ignorespace
If you want full control over which commands will be stored by history and which will not be stored, set HISTCONTROL to ignorespace:
1
|
export HISTCONTROL=ignorespace
|
So out of these commands:
1
2
3
|
ls –la
du –h
ps
|
Only these will show:
1
2
3
4
|
[jorge@ashitaka ~]$ history
1 ls –la
2 ps
...
|
ignoredups and ignorespace
To use both of these features, simply use:
1
|
export HISTCONTROL=ignoreboth
|
HISTIGNORE
The variable HISTIGNORE will help you tell history which commands to ignore, meaning that they shouldn’t be picked up by the BASH history. For instance, let’s say that I want the commands df and free to be ignored, including optional flags:
1
|
export HISTIGNORE=“df*:free*”
|
HISTSIZE
Setting how many lines of history to store is always a nice feature:
1
|
export HISTSIZE=10000
|
Thanks to pkhamre for suggesting to include HISTCONTROL and HISTIGNORE.