command line fun – logs and logging

Before writing this blog entry, I never considered the actual similarities between the Linux shell and the dos shell. I shouldn’t have been too surprised as the similarities have less to do with the operating systems than with the general theory.

The shell is all about text. Command line programs accept text as input or generate text as output.  When things go well the text goes to standard output, but when things don’t go well then the messages go to standard error.

Output on the command prompt isn’t entirely obvious when it is printed.  The content may give an indication if it says something like “syntax error” but there is a better way to determine the real problems.  The output streams are separate and can be redirected either separately or together to a log file.

All the output is displayed together in the terminal window but can be split into its composite parts.  In Unix each program inherits three standard streams, which is how the program can then split the output into either standard output or standard error.

Standard IO Streams

Fileno Name Description
0 STDIN Standard input
1 STDOUT Standard output
2 STDERR Standard error

For example, the directory listing output of the ls program is sent to standard output but the error is sent to standard error.

sam@asus:/tmp/testarea$ ls -ltrR
.: total 388
-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg
-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg
-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg
d--------- 2 sam sam 4096 Nov 7 23:55 private
ls: cannot open directory ./private: Permission denied 
sam@asus:/tmp/testarea$

The output can be redirected to a file by using the greater than “>” symbol.

sam@asus:/tmp/testarea$ ls -ltrR > listing.txt
ls: cannot open directory ./private: Permission denied 
sam@asus:/tmp/testarea$

Yet, because the error output is not redirected, it is still displayed in the terminal window.

It is possible to only redirect the error messages to the error file.

sam@asus:/tmp/testarea$ ls -ltrR 2> error.txt
.: total 388
-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg
-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg
-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg
d--------- 2 sam sam 4096 Nov 7 23:55 private

Yet, it is easy to combine the output of both streams into a single output file by adding “2>&1” to the end of the command.  This will then redirect all standard error output into the standard output.

sam@asus:/tmp/testarea$ ls -ltrR 2>&1 output.log
.: total 388
-rw-r--r-- 1 sam sam 146962 Nov 7 23:54 Steigenberger Airport_Kinder_009.jpg
-rw-r--r-- 1 sam sam 92444 Nov 7 23:54 Steigenberger Airport_Kinder_010.jpg
-rw-r--r-- 1 sam sam 148526 Nov 7 23:54 Steigenberger Airport_Kinder_031.jpg
d--------- 2 sam sam 4096 Nov 7 23:55 private

Special output locations

It is possible to decide which output is important.  In some situations it may be the normal (standard) output while in perhaps for production situations it is only any errors that occur that are interesting.

On both Linux and dos it is possible to easily throw away the unnecessary output.  The virtual garbage can is called the null device.  So, to eliminate unneeded output simply redirect the output to the null file.

The null file is called /dev/nul on linux and NUL on windows.

Ignore standard output example

Linux
   ls -l file.xxx > /dev/null

Windows
   dir file.xxx > nul

Ignore error example

Linux
   ls -l file.xxx 2> /dev/null

Windows
   dir file.xxx 2> nul

Other output methods – tee

One of the really neat things about the Unix world is the wealth of command line programs which simplify daily operations.  One of my favorite command line programs is tee.  Simply run some program and pipe the output through the tee command.  The output will be both displayed both on the screen but also logged to the file given.

ie.ls -ltr | tee mylogfile.log

This is ideal when you wish to run some program or script on the command line and see what is being printed but still wish to save the standard output to a log file.  But if the error output should also be saved, then simply redirect the standard error output to the standard output stream.

There is no standard program or utility that is similar to the Linux tee command in the windows world but there is a similar utility that can be downloaded for windows to supplement windows scripts.

Other output methods – script

It is all well and good to redirect output.  It is possible to see what the output was or to get a small extract of the errors that occurred, but there is a better way to see not only the run of a single command but everything that occurred at the command line for a certain period of time.

The Linux command “script” will save every key pressed and every character or message that was output to a output file.

If the command is run from the command prompt without any parameters then all output will be saved into a file called “typescript”.  If a filename is given as a parameter then all output will be saved to that file.  All key presses or output will be stored into the output file until you terminate the script command.  This is done by either typing “exit” or with a control-d.

This entry was posted in Command line and tagged , , . Bookmark the permalink.