If you ever need to save the history of your Unix shell session, one of the easiest ways to do it is to use the script command, found in most Unix systems.
Simply provide the file name for your log as a command line parameter:
$ script /tmp/unix-session.log
After starting the command, you'll be able to do whatever you want, and once you're done you can use the standard Ctrl+D to end the session recording. The generated log file will have both the commands you have typed during your session and the output generated by these commands.
Your session log with also include the timestamps marking the start and the finish of your recording.
Here's a sample session showing you how you use the script command:
$ script /tmp/unix-session.log Script started, file is /tmp/unix-session.log $ uname -a Linux simplyunix.com 2.6.16.29-xen #3 SMP Sun Oct 15 13:15:34 BST 2006 x86_64 GNU/Linux $ exit Script done, file is /tmp/unix-session.log
Now, if we were to cat the log file generated in this example, we'd see the following:
$ cat /tmp/unix-session.log Script started on Tue Nov 20 16:48:08 2007 $ uname -a Linux simplyunix.com 2.6.16.29-xen #3 SMP Sun Oct 15 13:15:34 BST 2006 x86_64 GNU/Linux $ exit
Script done on Tue Nov 20 16:48:12 2007
Have you got a better way to save your sessions? Let me know! (saving logs from gnome-terminal and Putty doesn't count, mind you!)
chandra shaker says
thanks for ur command page
nbkz says
Hi, I'm wondering if you can help.
So I made a log-file with the script command to record a sessions, say I saved it as foo.log. I tried to open it with text editors (pico, vim, kate, gedit) that I could find, and it opens up with gibberish. Is there a good way to convert these session logs into a clean/readable text file?
Thanks!
alby says
i've insert in .profile :
DATA_ORA=`date +"%Y-%m-%d-%H:%M:%S"`
SESSION=$DIR$LOGNAME"_"$DATA_ORA
script -a $SESSION
but when a user log in see :
Script started, file is /BEA3_QUEUE_WLI/bea_2010-06-11-12:09:05
Is possibile to hide that , also when a user exit ?
Midlands88 says
You can hide this by adding '-q' (for quite, see man script), as in
script -a -q $SESSION
… but the user has to type exit twice, for ending 'script' and for exiting the shell … anyone has an answer to work around this?
Zeppo says
Just add an exit after the script session
My challenge is that this generates lots of data. I want a compress feature on all the logs
Omar says
Very useful, Thank you very much
anupam says
How we can set the the below script to be run automatically when some one login to system:
've insert in .profile :
DATA_ORA=`date +"%Y-%m-%d-%H:%M:%S"`
SESSION=$DIR$LOGNAME"_"$DATA_ORA
script -a $SESSION
but when a user log in see :
Script started, file is /BEA3_QUEUE_WLI/bea_2010-06-11-12:09:05
Is possibile to hide that , also when a user exit ?