8

I know that Ctrl+R let's you search command history, but it's a little primitive. Is there a way to export all my command history (not just the current terminal session, but the full history) to a text file? I could then use a text editor to search it comfortably. Or if the history file already exists, where is it?

  • I'm going to assume that you meant your shell's command history, and have edited to use the default shell, bash. – muru Apr 9 '15 at 14:11
13

From man bash:

HISTFILE
    The name of the file in which command history is saved.
    The default value  is  ~/.bash_history.
    If unset, the command history is not saved when a shell exits.

So, the variable HISTFILE will contain the filename where the history will be saved.

$ echo "$HISTFILE"
/home/user/.bash_history

You can now search for the pattern:

$ grep "vim" "$HISTFILE"
vim foo.text
vim bar.text
vim file.txt

As @Dennis has pointed out, if you want you can run history -a to append the command history of the running session to the $HISTFILE file. Basically the commands will be automatically appended once you close a session, history -a will do the same thing right at that instant.

Run help history to get more idea on the history builtin itself.

  • 1
    Note that the history file will usually not contain lines entered since the beginning of the current bash session, which can be fixed by executing history -a. – Dennis Apr 9 '15 at 21:19
  • @Dennis: Good point..added.. – heemayl Apr 9 '15 at 21:24
4

Try this:

history > output.txt
less output.txt

Then search by typing / + searchterm

3

The bash history is saved in your home directory ~/.bash_history.

Basically Ctrl + R searches from this file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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