Redirecting Output

Redirecting output to a file

Most programs, when executed, display lots of text on the screen. You can save this text into a file if you want to retain it, or process it further. To do this, you use the redirection operator, ">".

For example, suppose that you want to save a copy of a directory listing into a file. You would type in the complete "ls" command, followed by ">" and the name of the new file to be created.

In the following example, we will list the contents of the /usr directory in long format, and write this listing into a file in our home directory (since we cannot create files in the /usr directory).

sandbox@laptop:~ > cd /usr
sandbox@laptop:/usr > ls -l > ~/usr-listing.txt
sandbox@laptop:/usr > cd
sandbox@laptop:~ > ls
KDesktop  public_html  snapshot1.png  usr-listing.txt
sandbox@laptop:~ >

Note the second line. First we have the command to list the directory "ls -l", then we have the redirection symbol ">" telling bash to dump all the results into a file, and finally we have the name of the new file to create (~/usr-listing.txt). As you may recall, the "~" symbol stands for the user’s home directory.

The symbol we gave so far (>) creates a new file with the specified name. If that file already exists it is first emptied before it receives the program’s output. If you give a double-angle-bracket (>>), the output is appended to the end of the file if it already exists. This can be used to redirect the output of several commands into one file.

Piping output to a program

When we used redirection, above, the output of a command was sent into a new file. With pipes, this output of one program is instead sent to the input of another program. This second program processes the output of the first program, and may produce its own output based on it.

Let us try this out. The "cat" command displays the contents of a file. The "/etc/services" file contains a list of recognised TCP/IP services. Try displaying this file now - it should show you many screenfuls of text.

Note that in the following example only a small number of lines are shown: your system will have much more.

sandbox@laptop:~ >cat /etc/services
#                 0/tcp    Reserved
#                 0/udp    Reserved
tcpmux            1/tcp                 # TCP Port Service Multiplexer
tcpmux            1/udp                 # TCP Port Service Multiplexer
compressnet       2/tcp                 # Management Utility
compressnet       2/udp                 # Management Utility
......lots more lines.....
nimhub          48002/tcp               # Nimbus Hub
nimhub          48002/udp               # Nimbus Hub
nimgtw          48003/tcp               # Nimbus Gateway
nimgtw          48003/udp               # Nimbus Gateway

Now, the "sort" command (as its name implies) sorts its input in alphabetical order, and sends it to the output. So, in order to sort the output of the "cat" command, we must pipe it to the sort command. To do this, we use the pipe symbol (|).

sandbox@laptop:~ >cat /etc/services | sort
3Com-nsd        1742/tcp                # 3Com-nsd
3Com-nsd        1742/udp                # 3Com-nsd
3com-amp3       629/tcp                 # 3Com AMP3
3com-amp3       629/udp                 # 3Com AMP3
......lots more lines.....
zip             6/ddp                   # Zone Information Protocol
zserv           346/tcp                 # Zebra server
zserv           346/udp                 # Zebra server

One very popular use of the pipe is to send the output of such long commands to the "less" command. This command allows you to scroll up and down, or even sideways, to view the complete results.

ps -Hefw | less

The above command line displays the list of processes in the system and uses "less" to allow you to scroll around (using the arrow keys and PgUp/PgDown), and Q to quit.

It is possible to string together several commands separated by a pipe. For example we could use the "cat" command to display the contents of the services file above, pipe that to the "sort" command to sort it, then pipe the results of the sort command into the "tail" command to pick out only the last 50 lines, and finally pipe those 50 lines to the "less" command to scroll around in the results.

cat /etc/services | sort | tail -n 50 | less

Comments

Looking for this article and

Looking for this article and found it at this great site...
xenical uk