How to save the output of a Linux command to a file

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.

  • kendon

    The one thing I noticed was that logsave has a much cleaner output

    and what do you mean by that? output is output, so either logsave is dismissing parts of the output or it should be the same?

  • By cleaner output I mean that the output is organized in a better fashion using whitespace and new lines. And the fact that logsave marks the beginning and end of a session of output makes it a lot easier to return to the log file to debug at a later time.

  • Yes true I agree with Sukrit

  • Pingback: Come salvare l’output di un comando Linux in un file | 3 sul Blog - Tech, Fun e Web 2.0()

  • Sharmistha

    Hi Guys, I am using the “logsave” command and unable to get the output. Do you actually create a txt file and then run the respective command or as the command runs an output is created by itself? if you can let me know, shall remain grateful.

    Actually on runing the command line, with “logsave”, I am getting an error saying the No such file or directory 2. The .txt died with exit status 1. What is actually means I am unable to get it.
    if some one can explain.

  • Simplehelp

    Logsave/ >> does not work for me.
    I do not understand the reason. I have an error saying 1. no .txt /directoy
    2. the .txt died with exit status1.

    Please if someone can guide me.

  • Anand

    actually the difference between logsave & greater than symbol is :

    logsave will save the output and display the result of the command.

    greater than just save the output won’t display the result