How to view the `.bash_history` file via command line?
Sebastian Wright
I want to view the contents of my .bash_history file but don't know how to get there via the command line.
4 Answers
If you want to access the actual file itself, just use your favorite text editor (I use emacs but you can use pluma of gedit or vimor whatever):
emacs ~/.bash_historyThat is the default location if your history file. If you don't find anything there, you may have changed the history file's name. This is stored in the $HISTFILE variable, so print it out to check its current value:
echo $HISTFILEIf, instead of the file, you just want to see your history, you can run history as @minerz029 suggested. The history command with no options just prints the contents of your $HISTFILE followed by the commands executed in the current shell that have not yet been written to that file, with line numbers.
If you actually need the output of the .bash_history file, replace history withcat ~/.bash_history in all of the commands below.
If you actually want the commands without numbers in front, use this command instead of history:
history | cut -d' ' -f 4-otherwise, there will be no difference (except if you're using a different shell).
Last 15 commands
You can use
history | tail -n 15to get only the last 15 lines of your history with the last executed printed last (at the bottom).
Searching for a command
Alternatively, use
history | grep "apt-get" | tail -n 15to get the last 15 commands which contained apt-get with the last executed printed last (at the bottom). You can replace apt-get with any command (or command argument) you want to search for (it can be a regular expression).
Scrolling through history
You can use
history | tac | lessto scroll through all the commands executed starting with the most recent at the top. Press q to exit.
Similarly, use
history | grep "apt-get" | tac | lessto scroll through all the commands executed with "apt-get" in them (including arguments) starting with the most recent at the top. Press q to exit.
In addition to minerz029's excellent answer.
To reiterate - the 'history' command prints the history along with a number next to it.
You can pipe the output of history into grep, less etc.
The ouput of history also shows a number on the left next to the output. e.g.
469 free 470 ps -fA 471 ps -fA | grep xend 472 free 473 sudo vi /etc/xen/xend-config.sxp 474 cat /etc/default/grub With this number you can re-run the command.
e.g. to re-run 473 I would type into the terminal
admin@xen1~$ !473followed by the enter key to repeat the command next to 473 in the history output.
You can also search interactively backwards in the command history by typing ctrl + r keys then start typing some of the contents of the command and it will search and fill it out. When you've found it you can type enter to repeat it, or press the [tab] key to copy it to the command line to edit the command first.
Of course, those last two options work in the bash shell. I'm not sure if these features work in other shells. But since bash is the Ubuntu default shell you should find them there.
You may want to try "suggest box"-like history - it reads Bash history and allows for quick navigation.