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

Save all of your Terminal activity to a file UNIX
I use Terminal everyday, and I find it a good idea to log everything I you. It makes it much easier to undo your mistakes when you know what those mistakes were. Here's how I do this:

Open Terminal's preferences.

Go to Settings, then Shell. You can choose here to run a command at startup. You could create a simple log of your session using the following:
/usr/bin/script ~/Desktop/Terminal.log
This will log everything you do and append it to the log file.

I like to keep my history so instead I wrote this small script that archives previous sessions by renaming the file with a date/time string. I then set to run at startup the following command:
Here's the script:
# Log everything you do in Terminal.

#* Formatted date & time string.
FORMATTED_DATE=`/bin/date "+%Y-%m-%d%H%M%S"`

#* Archive the previous file
/bin/cp -f ~/Desktop/Terminal.log{,.$FORMATTED_DATE.txt}

#* Begin a new one
/usr/bin/script ~/Desktop/Terminal.log
[kirkmc adds: This is a good idea. I don't use Terminal a lot, but I find that I sometimes need to remember a command I ran in the past which is no longer in my history.

Also, this is similar to something I do with texts I write. I do most of my writing in BBEdit, and I have a number of "scratch" files which I change every year. For example, I have one file for most of what I write, another just for Mac OS X Hints, and another for a specific client I write for. I archive these files at the end of the year, and create new ones. If I ever need to go back to these files to look for something I can do so. I don't bother to enter dates - which I could do easily enough with a TypeIt4Me shortcut - but if I'm looking for something I'll generally know what text to search for.]
  • Currently 3.44 / 5
  You rated: 1 / 5 (9 votes cast)

Save all of your Terminal activity to a file | 9 comments | Create New Account
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.
This is unnecessary.
Authored by: Chas on May 11, '12 08:03:23AM

Apparently you don't know that bash already has a history file ".bash_history" in your home directory. Just hit the up arrow to scroll through your previous commands. I don't know how big the default history is, but it can be expanded easily. RTFM.

Even if you wanted to do this log file (which you shouldn't) you're setting it up wrong. You should be doing this with environment variables and .bashrc

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

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 | # ]
Save all of your Terminal activity to a file
Authored by: excarnate on May 11, '12 11:51:23AM
Note that the script has a race condition. If you open several terminals at once, the script will clobber the same file and have multiple terminals writing to it at once; which is fun, but not what is intended.

[ Reply to This | # ]
Save all of your Terminal activity to a file
Authored by: mark hunte on May 11, '12 11:43:59PM
Thank you for this, I wish I had known about it before.
One think to note that I found: The output does not seem to be instant, it may take about 10-20 seconds to log the text.

Also a thought occurred to me, you do not need to have a separate script file to archive the logs.
Just put this in the Run command.
FORMATTED_DATE=`/bin/date "+%Y-%m-%d%H%M%S"`;/usr/bin/script  ~/Library/Logs/Terminal/terminal_$FORMATTED_DATE.log
If you want to use just one file but append to the contents then use the -a option
/usr/bin/script -a ~/Library/Logs/Terminal/Terminal.log
I have also pointed it to a folder called "Terminal" in the users logs. This way I can go into Console and find them quickly.
Edited on May 12, '12 12:05:30AM by mark hunte

[ Reply to This | # ]