Quick Tip: Track Your Bash History


Sometimes, you want to look back in time and see what commands you ran in the past. Thanks to the history command built into the Linux shell, you can do that with ease.

$ history
396  git pull --all
397  git branch -a
398  git push origin :header-revamp
399  git branch -a
400  git push origin :nav-dropdown
401  git branch -d nav-dropdown
402  git fetch -p origin
403  git branch -a
404  git checkout front-page
405  git branch --unset-upstream
406  git checkout develop

You can easily search through the output by piping to the grep command.

$ history | grep ssh
482  ssh
492  ssh -p xxxx [email protected]
702  ssh -p xxxx [email protected]
770  ssh -p xxxx [email protected]
781  ssh -p xxxx [email protected]
889  ssh [email protected]

If you want to enable a timestamp, you can use the export command to set the variable and run the command again:

492  4/9/2017 13:42  ssh -p xxxx [email protected]222.222.22
702  4/11/2017 19:29  ssh -p xxxx [email protected]222.222.22
770  4/12/2017 09:56  ssh -p xxxx [email protected]44.44.44
781  4/12/2017 21:41  ssh -p xxxx [email protected]44.44.44
889  4/13/2017 09:47  ssh [email protected]45.67.89

HISTTIMEFORMAT can be added to your .bashrc or .zshrc file, according to your needs, to permanently set the variable. Same goes for how many items you want to save in the history via the HISTSIZE variable.

Finally, you can delete your history easily as well:

$ history -c