Pipes and Redirects

The UNIX philosophy is that programs should do one job and do it well. To do more complicated tasks programs can be chained together using pipes but before we go on we have to discuss input output streams. There are three standard streams in Linux; stdin, stdout and stderr. stdin provides input to a program, for example the text you type into the console. stdout provides the usual output from a program for example the feedback from the console. Finally stderr is the output stream for error messages.

A redirect takes the output from a file descriptor and sends it to a file. To send output from a device overwriting or creating a file use the > character in the form:  device > file. Most of the time we want to take the output from stdout or stderr  represented with the numbers 1 and 2. To pipe the output from ls you can use:
ls 1> listOfFiles.txt
or since defaults to stout we can just use
ls > listOfFiles.txt
 If you look at listOfFiles.txt:
user@pc~$ cat listOfFiles.txtmyDocument.docmySpreadsheet.xlsmyPDF.pdf
If we want to grab the error output we can use 2> like this:
user@pc~$ ls thisFileDoesNotExist.txt 2> errorMsg.txtuser@pc~$ cat errorMsg.txtls: cannot access thisFileDoesNotExit.txt: No such file or directory
What if if we don't want to overwrite the file. We simply use >> to append the file instead. You can even combine them:
ls thisFileMightExist > list.txt 2>>errors.txt
 or combine them with &> or &>>. for example:
ls ./ &>> listAndErrors.txt
Redirecting Input

To use a file as stdin (input) for a program use the < charictor. The program 'rev' simply reverses the text it is given as input. So if we want to reverse the text in listOfFiles.txt created earlier we might do:
user@pc~$ rev < ./listOfFiles.txt cod.tnemucoDymslx.teehsdaerpSymfdp.FDPym

Pipes connect stdout of one program to the stdin of another using the | character. Using | you can chain simple programs together to do complex tasks. We'll start by cating listOfFile.txt:
user@pc~$ cat ./listOfFiles.txt
Now we can add grep to find just the word documents:
user@pc~$ cat ./listOfFiles.txt | grep .docmyDocument.doc
And now we tack on rev to reverse the output:
user@pc~$ cat ./listOfFiles.txt | grep .doc | revcod.tnemucoDym