Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Save all of your Terminal activity to a file' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Save all of your Terminal activity to a file
Authored by: aubreyapple on May 11, '12 10:01:32AM

The problem with the history files is that the only thing saved there is what was the last terminal window. If you use multiple terminal windows, the history there can be somewhat mixed up.

More: When you start a window, the contents of the history file are available to you. When you type commands, they will be saved to the history file, sometimes immediately and sometimes when you close the window. Each window maintains it's own history, so unless you never use more than one terminal window at a time, it is a crap shoot as to what gets saved there. (I am sure there is an algorithm, but I never use history myself, I set the save history to 0 for security reasons).

So, the history file is useful. So is this hint. Combined, yet more useful... :-)

[ Reply to This | # ]
Save all of your Terminal activity to a file
Authored by: excarnate on May 11, '12 10:44:39AM

Actually you can save your history from multiple windows to a single .bash_history file. Here are some things related to history in my .bash_profile:

HISTIGNORE="&:pwd:ls:[bf]g:exit:[ \t]*"
shopt -s cmdhist
shopt -s histappend
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
function hs {
grep $1 $HISTFILE

In order, this:
Sets the history kept for a current iteration of the shell to larger than the default of 500. If you have a lot of terminal sessions or keep them open for weeks/months you may want to up this value.
Keeps a large history file (which I like, this may be a bit much, though).
Records and displays the date and time of a command.
Ignores repeated and uninteresting commands, also allows you to keep a command out of history by starting it with a space or a tab.
Displays multiple commands entered on a single line on a single line (think: for loop). This is just my preference.
Appends history from all windows.
Everytime there is a new prompt, the history is appended, cleared, read (back) in so history from all windows is available and displays correctly in all windows.
Finally, a command to search the history (e.g. an easier way than history|grep foo).

This doesn't record, as 'script' does, the output of commands, but it does much of what is useful (I use the script command in more limited circumstances). If you don't use (the default) bash shell, you'll have to look up how to do things in your chosen shell.

Note that the script command records control characters like backspaces, and capturing a vi session is a bit messy. If just want a history of what you typed, use the built in history functionality of bash.

Edited on May 11, '12 12:13:37PM by excarnate

[ Reply to This | # ]
Save all of your Terminal activity to a file
Authored by: excarnate on May 11, '12 11:58:58AM

<deleted, redundant>

Edited on May 11, '12 12:14:04PM by excarnate

[ Reply to This | # ]