Saving or Appending Linux Command’s Ouput to a File

Recently I issued a command on one of my linux servers (using putty) for which the output returned could not fit on the putty’s available screen. This has happened to me quite a few times before but this time the data was critical and I wanted to check the complete output of the command executed. So I saved the output to a file, so that I could read it completely.

This guide will teach you to extract the output of a linux command to a log file or simply a file. Now before we proceed with extracting outputs, let us understand what things will be required.

  • A linux command to execute.
  • Path to the file where you want the output to be saved.

Now, notice the 2nd point above. When you provide a path to the file, there are again 2 possibilities, either the file already exists or it does not. The reason for mentioning this is, just incase you mistakenly give the wrong path, the data from the existing file might be deleted or changed. Again, while saving the output you may, at times, want to “append” the output to the existing log file. Linux provides this feature as well. Let us now see the two ways to save the output of a Linux command to a file.

Logsave command in Linux

The general command for logsave is as follows :

# logsave /path/to/file command

For demonstration, I extracted few logs to /tmp folder, created a folder in /tmp folder  and  then executed a command to recursively list the files in the /tmp directory.

Following is the example for logsave command :

# logsave /tmp/mylog1.log ls -LR /tmp

Note that “ls -LR /tmp” is the command to recursively list the names of files and folders in /tmp directory.

The output was as follows :


Log of ls -LR /tmp
Sat Apr 12 00:01:33 2014

/tmp:
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4

Sat Apr 12 00:01:33 2014
----------------

Note : This command will create a new file if it does not already exist. If the file already existed, it will clear its contents and paste the output of the command in it.

Using Logsave Command with -a Argument

If you wish to append the output of the command you can use the -a argument with logsave command as follows :

#logsave -a /tmp/mylog1.log ls -LR /tmp

After executing this command the contents of the file mylog1.log were as follows :


Log of ls -LR /tmp
Sat Apr 12 00:01:33 2014

/tmp:
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4

Sat Apr 12 00:01:33 2014
----------------
Log of ls -LR /tmp
Sat Apr 12 00:10:18 2014

/tmp:
mylog1.log
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4

Sat Apr 12 00:10:18 2014
----------------

As you can see the logsave command saves the output as well as date and time. If you do not want this to happen, you can use the Greater than operator for saving the output.

Using greater than operator for saving or appending output of Linux command to a file

Case 1 : When you wish to create a new file or flush the contents of existing file and then save the output of the command to that file, you can use the following command :

# ls -LR /tmp > /tmp/mylog1.log

greater than sign to save linux command output to a file

After executing the above command, my “/tmp/mylog1.log” file contained :


/tmp:
mylog1.log
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4

Case 2 : When you wish to append the output to the existing contents of the file, use the following command :

# ls -LR /tmp >> /tmp/mylog1.log

double greater than sign to append linux command output to a file

After executing the append command the contents of /tmp/mylog1.log file were as follows :


/tmp:
mylog1.log
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4
/tmp:
mylog1.log
ns.log
testfolder
topstats.log

/tmp/testfolder:
1
2
3
4

Note that the ouput is twice since I first executed the command with single greater than operator as mentioned for case 1 (which created the new file) and then I appended the same output again to this file by executing the command mentioned under case 2.

This was a simple example but the possibilities of using this technique are endless. You can create a script to save the output of certain commands to a file. Then set a cron job to execute this script at specific intervals. You can then use another script which checks the contents of previously generated log files and sends mails to users or whatever. Depending on your requirement this technique can be used for many constructive purposes.

Hope this tutorial was useful. Incase of doubts feels free to comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.