script: Save Your Session Log

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!)

8 comments ↓

#1 tee unix command | UNIX Tutorial: Learn UNIX on 12.21.07 at 7:59 am

[...] script: Save Your Session Log Please share: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

#2 chandra shaker on 07.28.08 at 5:55 am

thanks for ur command page

#3 nbkz on 02.22.10 at 9:53 pm

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!

#4 alby on 06.11.10 at 11:10 am

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 ?

#5 Midlands88 on 03.06.11 at 2:26 am

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?

#6 Zeppo on 04.06.11 at 9:13 am

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

#7 Omar on 10.20.11 at 9:11 am

Very useful, Thank you very much

#8 anupam on 11.29.11 at 11:04 am

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 ?

Leave a Comment