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: mchagers on May 11, '12 08:54:38AM

Are you aware that everything you type into the terminal is already logged to the file .bash_history in your home directory? That is if you're using bash as your shell of course, but since it's the default, this will be the shell for most users.
I'm not sure how long the contents of this file are kept, apparently the oldest lines are purged from it after some time, but you could make backups every month or so to make sure nothing is lost.



[ Reply to This | # ]
Save all of your Terminal activity to a file
Authored by: lurch99 on May 11, '12 09:33:48AM
Actually, .bash_history just records the commands you've entered, not everything you've typed in Terminal, which is what 'script' does. However, it's also good to use the following in your .bash_profile (or .bashrc) to record a longer amount of bash history:
export HISTFILESIZE=1000


[ Reply to This | # ]
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:

HISTSIZE=2222
HISTFILESIZE=999999
HISTTIMEFORMAT="%Y%m%d-%T "
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 | # ]
Save all of your Terminal activity to a file
Authored by: amusingfool on May 15, '12 07:12:56AM

Two other important points, when using this feature. Close your shells using 'exit', not just allowing them to end. This forces a flush to the .bash_history. And you can also do
export HISTCONTROL=erasedups

This will keep duplicates from being written to the file. It does some odd things to the history substitutions (when at history entry 1024, !-2 and !1022 might not do the same thing), but does prevent 500 instances of 'cd' from taking over the history.



[ Reply to This | # ]