28.09.2018 Views

Linux Dummies 9th

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Redirection and Pipes<br />

Chapter 17: Working without the GUI<br />

Redirection and pipes facilitate the flow of information. A pipe is exactly what<br />

it sounds like: It directs the output of one program to the input of another<br />

program. A pipeline may consist of several utilities plumbed together by<br />

pipes. At either end of this pipeline is, optionally, a redirection.<br />

327<br />

Almost all <strong>Linux</strong> utilities that require input and output have been plumbed<br />

with the following common interfaces: stdin (standard input), stdout<br />

(standard output), and stderr (standard error). By having a common<br />

method to feed input to a program or read data from the output of a program,<br />

you can glue utilities together into sophisticated solutions.<br />

Redirecting command output<br />

We discuss redirecting command output here because it’s by far the most<br />

common form of information detouring. One example of output redirection<br />

involves telling a command to send its results to a file rather than to the<br />

screen, as you’ve probably been used to seeing. Start in some familiar territory<br />

by typing ls -l and then pressing Enter, to produce something like the<br />

following:<br />

rich@testbox:~$ ls -l<br />

total 36<br />

drwxr-xr-x 2 rich rich 4096 2009-05-20 18:56 Desktop<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Documents<br />

drwxr-xr-x 4 rich rich 4096 2009-05-17 17:42 GNUstep<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Music<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Pictures<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Public<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Templates<br />

drwxr-xr-x 2 rich rich 4096 2009-05-17 14:09 Videos<br />

rich@testbox:~$<br />

Want to send this information to a file instead? You can use the > redirection<br />

operator to tell bash to send the data into a file rather than onto your screen.<br />

Enter the following command to send the information to a file named listing:<br />

ls -l > listing<br />

Notice that nothing shows up on-screen, where normally you’d expect to see<br />

the output from the ls command. That’s because the shell has rerouted the<br />

output to a file named listing. To verify that the directory listing is there,<br />

enter the following command:<br />

cat listing

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!