We've seen how to redirect input from a file and output to a file. You can also connect two programs together so the output from one program becomes the input of the next. Two or more programs connected in this way form a pipe. To make a pipe, place a vertical bar (
|) on the command line between the two commands.
When a pipe is set up between two commands, the standard output of the command to the left of the pipe symbol becomes the standard input of the command to the right of the pipe symbol. Any two commands can form a pipe as long as the first program writes to standard output and the second program reads from standard input. For example:
ls -la | colrm 1 48. .. .CFUserTextEncoding .DS_Store .Trash Desktop Documents Library Movies Music Pictures Public Sites $
This example combines ls -la (list all), and then uses the colrm (column remove) command to give you just a list of the contents of a directory. This shows the contents of a user's Home directory, including the hidden dot files.
You could take this example one step further and redirect its output to a file; for example:
ls -la | colrm 1 48 > homedirlist.txt
That command line starts by listing the files, uses colrm to strip out everything that ls -la returns, and then redirects that information as the contents of a new file, named homedirlist.txt. Add another pipe, and you can open the file with TextEdit:
ls -la | colrm 1 48 > homedirlist.txt | open homedirlist.txt
You just can't do that in the Finder. ...