Piping and redirecting output from one command or file to another

From Docswiki
Jump to navigation Jump to search

Piping entails sending standard output to the standard input of a linux command. Pipes are the primary power of using the command line. They can be used with sed, awk, perl, and python amongst other tools to produce very specific behavior. To pipe the output to a new command place a '|' character between them.

Examples

command line pipe

wc -l * | sort -rn

pipes in for loops

 for f in `ls -al | awk '{print$9}' | sort`; 
 do 
    echo $f; 
    cp $f $f.bak; 
 done

named pipes

Another way of piping is to set up a pipe that can persist outside of a specific command line. This can also be used to monitor calculations that are in progress.

For example (from wikipedia), one can create a pipe and set up gzip to compress things piped to it:

mkfifo my_pipe
gzip -9 -c < my_pipe > out.gz
rm my_pipe

In a separate process shell, independently, one could send the data to be compressed:

cat file > my_pipe