OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Redirecting Standard Output

I/O redirection allows us to redefine where standard output goes. To redirect standard output to another file instead of the screen, we use the “>” redirection operator followed by the name of the file. Why would we want to do this? It's often useful to store the out- put of a command in a file. For example, we could tell the shell to send the output of the ls command to the file ls-output.txt instead of the screen:


[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt

[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt


Here, we created a long listing of the /usr/bin directory and sent the results to the file

ls-output.txt. Let's examine the redirected output of the command:


[me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 167878 2016-02-01 15:07 ls-output.txt

[me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 167878 2016-02-01 15:07 ls-output.txt


Good; a nice, large, text file. If we look at the file with less, we will see that the file

ls-output.txt does indeed contain the results from our ls command:


[me@linuxbox ~]$ less ls-output.txt

[me@linuxbox ~]$ less ls-output.txt


Now, let's repeat our redirection test, but this time with a twist. We'll change the name of the directory to one that does not exist:



[me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt

ls: cannot access /bin/usr: No such file or directory

[me@linuxbox ~]$ ls -l /bin/usr > ls-output.txt

ls: cannot access /bin/usr: No such file or directory


We received an error message. This makes sense since we specified the non-existent di- rectory /bin/usr, but why was the error message displayed on the screen rather than being redirected to the file ls-output.txt? The answer is that the ls program does not send its error messages to standard output. Instead, like most well-written Unix pro- grams, it sends its error messages to standard error. Since we only redirected standard output and not standard error, the error message was still sent to the screen. We'll see how

Redirecting Standard Output


to redirect standard error in just a minute, but first, let's look at what happened to our out- put file:



[me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 0 2016-02-01 15:08 ls-output.txt

[me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 0 2016-02-01 15:08 ls-output.txt


The file now has zero length! This is because, when we redirect output with the “>” redi- rection operator, the destination file is always rewritten from the beginning. Since our ls command generated no results and only an error message, the redirection operation started to rewrite the file and then stopped because of the error, resulting in its truncation. In fact, if we ever need to actually truncate a file (or create a new, empty file) we can use a trick like this:



[me@linuxbox ~]$ > ls-output.txt

[me@linuxbox ~]$ > ls-output.txt


Simply using the redirection operator with no command preceding it will truncate an ex- isting file or create a new, empty file.

So, how can we append redirected output to a file instead of overwriting the file from the beginning? For that, we use the “>>” redirection operator, like so:



[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt

[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt


Using the “>>” operator will result in the output being appended to the file. If the file does not already exist, it is created just as though the “>” operator had been used. Let's put it to the test:



[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 503634 2016-02-01 15:45 ls-output.txt

[me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l /usr/bin >> ls-output.txt [me@linuxbox ~]$ ls -l ls-output.txt

-rw-rw-r-- 1 me me 503634 2016-02-01 15:45 ls-output.txt


We repeated the command three times resulting in an output file three times as large.


Top OS Cloud Computing at OnWorks: