Saturday, May 11, 2024
14
rated 0 times [  14] [ 0]  / answers: 1 / hits: 1762  / 2 Years ago, mon, june 6, 2022, 2:54:22

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?


More From » command-line

 Answers
5

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.


[#20598] Tuesday, June 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
repend

Total Points: 195
Total Questions: 113
Total Answers: 107

Location: Nicaragua
Member since Tue, Dec 8, 2020
4 Years ago
;