Using Command Output for Complex Tasks


One of the greatest advantages of the Linux command line is that it enables you to tie multiple commands and their output together in a wide variety of ways. By tying commands, you can perform complex tasks that involve multiple steps; the output can be further refined at each step, to fulfill your needs in the most unusual of situations.

The two most important techniques for tying multiple commands are the use of pipeswhich enable the output of one command to act as the input for another commandand command substitutionwhich enables the output of one command to alter the behavior of another command.

Using Pipes to Link Commands

Sometimes it is convenient to use the output of one command as the input of another. You can use pipes, which you create with the vertical bar (|), to cause the shell to do this.

For example, you have seen the list of JPEG and GIF images of space on your system; you created this list by redirecting the output of two locate commands to a file called myjpegs.txt and then searching this file with grep for the word space.

Suppose you now want to learn whether any images of space in your Linux file system are stored in the .png or .tif graphics formats. Wouldn't it be helpful to send the output of the locate command directly to grep so that grep could search the data on the fly, without having to keep saving data to the file myjpegs.txt?

You can do this with the help of the pipe (|) in a command like this one:

 locate '*.gif' '*.jpg' '*.tif' '*.png' | grep space 

This command generates a list of all the image pathnames that contain the word space across your entire Linux file system. The output of the locate command (to which you supplied four arguments) is sent directly to grep, which searches the data it received for the word space.

A more mundane, but no less useful, application of pipes is the paging of command output. For example, try entering the following command:

 ls -l /usr/bin 

The listing that this command generates is very long; the vast majority of it will scroll past the top of your console before you get a chance to see it. However, by sending the output of this command through a pipe to another command called less, you can remedy the situation. As you've already learned, the less command is a pager: It displays a file or input data one page at a time, pausing and requiring you to press the spacebar to continue between each screen of information. Try piping the output of ls through less now with this command:

 ls -l /usr/bin | less 

The listing still contains a great deal of information, but you are now able to see all of it at your leisure. Don't worry if the range of applications for pipes seems murky to you now; you'll grow more accustomed to pipe use as we continue to work with the shell.

Using One Command's Answers as Another's Arguments

Another essential shell tool is known as command substitution. Command substitution allows the output of one command to be used as a set of command-line arguments for another command. This process enables the results of a first command to alter the behavior of a second command, thereby affecting its output and customizing the second command for the situation at hand.

Suppose you want to create a directory called spacejpegs and gather into it every JPEG image on your system whose pathname contains the word space, for easy indexing and access to space images. You already know how to use locate to find all the pathnames that contain the word space and end in .jpg in the Linux file system. With this output, you can use command substitution to gather these files into one place:

 [you@workstation20 ~]$ mkdir spacejpegs [you@workstation20 ~]$ ln -s $(locate '*space*.jpg') spacejpegs [you@workstation20 ~]$ 

Enclosing the command locate '*space*.jpg' in parentheses preceded by a dollar sign ($) caused the output filenames from the locate command to be treated as though you had typed them in one by one on the command line as arguments to ln -s, which is the command to create symbolic links. The net effect was as if you had typed a command something like this:

 ln -s /path/to/file1.jpg /path/to/file2.jpg /path/to/file3.jpg [...] jpegfiles 

What Are Symbolic Links Again?

Symbolic links and the ln -s command are discussed in Chapter 7.


Use the ls command to get a long listing of the spacejpegs directory. You'll find that a symbolic link has been created to every file containing the word space in its pathname and ending in .jpg on the entire system; all the space-oriented JPEG images have been collected in one place for easy access.

Two Ways to Substitute Commands

Command substitution in the bash shell can occur in two ways; the one shown here is to enclose the substituted command in parentheses preceded by a dollar sign:

 $(locate myfile) 

An alternate, more traditional way of using command substitution is to enclose the substituted command in backquotes (also known as backticks or simply ticks):

 `locate myfile` 

The less traditional method is used in this book because backquotes are often difficult to differentiate from forward single quotation marks and can thus lead to confusion.


As was the case with pipes, the wide range of applications for command substitution is not likely to be immediately obvious to the beginner. Don't worry, though; you'll see them again as we continue to work with the shell.



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net