Logging shell commands into a file

This post is a really short one, but I thought it important enough to make a note! While working on a coding project, having a log of all your commands and outputs is extremely important. You never know when you need to look back at an earlier error you had encountered or what output a certain command had given. Trust me, this happens ever so often that I decided to keep a log of all my activity once I begin working for the day.

And it’s insanely easy to create log files! All you have to do is use the script command. This command makes it easy to dump all your logs into a file name of your choice. Invoke the following command:

$ script
Script started, file is typescript

This is actually another shell that is a child of the login shell. The log file name is called ‘typescript’ by default. You can specify a file name of your choice by entering script <filename> instead.

Now to end the logging, just type exit and you’re done.

Script done, file is typescript

You can now view the file contents using cat <filename> or cat typescript, whatever is applicable.

Note that you can also use a previous log file using script -a <filename>.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: