O'Reilly logo

Linux® For Dummies®, 8th Edition by Richard Blum, Dee-Ann LeBlanc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Redirection and Pipes

Redirection and pipes facilitate the flow of information. A pipe is exactly what it sounds like: It directs the output of one program to the input of another program. A pipeline may consist of several utilities plumbed together by pipes. At either end of this pipeline is, optionally, a redirection.

Almost all Linux utilities that require input and output have been plumbed with the following common interfaces: stdin (standard input), stdout (standard output), and stderr (standard error). By having a common method to feed input to a program or read data from the output of a program, you can glue utilities together into sophisticated solutions.

Redirecting command output

I discuss redirecting command output here because it’s by far the most common form of information detouring. One example of output redirection involves telling a command to send its results to a file rather than to the screen, as you probably have been used to seeing. Start in some familiar territory by typing ls -la ~ and then pressing Enter, to produce something like the following:

 total 20                                                            
							 drwx------ 2 sue  users 4096 Oct 30 07:48 .                         
							 drwxr-xr-x 5 root root  4096 Oct 30 11:57 ..                        
							 -rw-r----- 1 sue  users  24 Oct 30 06:50 .bash_logout               
							 -rw-r----- 1 sue  users 230 Oct 30 06:50 .bash_profile              
							 -rw-r----- 1 sue  users 124 Oct 30 06:50 .bashrc                    
							 -rw-rw-r-- 1 sue  users   0 Jan 2 07:48 wishlist                    

Want to send this information to a file instead? You can use the > redirection operator to tell bash ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required