If you administer a Linux machine it is quite likely that you see a lot of messages fly by on your screen as you run commands. Some of these messages are trivial, while others may be critical. Although Linux and UNIX have a fine logging engine in syslogd and most of the system’s messages are logged in a proper log format in various files under he /var/log directory, you might find it useful sometimes to log the output of a command or script that you have run. Linux provides a number of ways to log the output of your commands.
The simplest, and probably the most common way to do this is to put a greater than sign after a command, followed by the location of the output file.
# ls /var/log > /tmp/varlog.log
The command shown above logs the output of the command ls /var/log into the file /tmp/varlog.log. One thing to note about this command is that if you use a single greater than sign to log the output the output of a command to a file it will create a new file if not already present, or wipe clean, if one is found. So, if you want to append the output of the command to a file you need to use two greater than signs instead of one. The command shown above needs to be modified to look like this:
# ls /var/log >> /tmp/varlog.log
Linux has a tool aptly named logsave which does a similar task as greater than sign. You can attain the same result as the first example using the following command:
# logsave /tmp/varlog.log ls /var/log
This command will create a new or overwrite an existing one with the output of the command ls /var/log. If you want logsave to append the out to a file instead of writing over it, using it with the -a option:
# logsave -a /tmp/varlog.log ls /var/log
There isn’t much of a difference between the greater than and the logsave method. The one thing I noticed was that logsave has a much cleaner output and it also adds the date to the output file.