Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to view the contents of my .bash_history file but don't know how to get there via the command line.

share|improve this question
1  
How about history | less –  MadMike Oct 14 '13 at 9:51
1  
Have you checked /home/username/.bash_history ? –  Mitch Oct 14 '13 at 9:52

3 Answers 3

up vote 10 down vote accepted

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_history

That is the default location if your history file. If you don't find anything there, you may have changed he history file name. This is stored in the $HISTFILE variable, so print it out to check it's current value:

echo $HISTFILE

If, 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.

share|improve this answer

If you actually need the output of the .bash_history file, replace history with
cat ~/.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 15

to 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 15

to 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 | less

to 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 | less

to 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.

share|improve this answer
2  
Nice!! I wish I could give more thumbs up!! :) –  Saurav Kumar Oct 15 '13 at 0:21

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~$ !473

followed 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.