Piping and Redirection

Basically redirection and piping are a few ways among many to achieve Inter Process communication in Unix.

  1. Redirection: Data is written to and read from a typical UNIX file. Any number of processes can interoperate. this must be used when sharing large data sets.

ls > FileName

  1. Piping : Piping is a process where the output of one process is made the input of another. They were evolved in the most primitive forms of the Unix operating system. They provide unidirectional flow of communication between processes within the same system. A pipe is created by invoking the pipe system call, which creates a pair of file descriptors. [ For file descriptors read http://www.bottomupcs.com/file_descriptors.html ]

ls | grep $myName

It works on simple data sharing, such as producer and consumer.

Property Comparison: Piping is always uni-directional while redirection could be used to redirecting input as well as output.

ls > grep myFileName [ Redirecting output of first command to later one ] sort < fileName.txt [ Redirecting fileName.txt file as an input to command sort ]

One can also write below to use bi-directional redirect in single statement.

sort < fileName.txt > sortNewFile.txt

While Piping, it is always output of first command supplied to the later one and that to simulanoeously.

ls | grep myName | awk '{ print $NF }' [ multiple piping in a single statement ]

Note 1: command > fileName . If there is a command named fileName, that would make using redirection a lot harder and more error prone. One must check first, whether there's a command named like destination file.

Other ways to achieve IPC in Unix system are:

  1. Named pipe
  2. Signal
  3. Shared memory
  4. Socket

Piping directs the output of a program to another program.

For example:

ls * | grep "name"

Pipes the names of all files in the current directory to grep. Re-direction directs or appends output to a file.

ls * > file  # writes all file names in current directory to the "file"
ls * >> file # appends all files names in current directory to the "file"

Piping saves you the hassle of having to write to a file, then read from a file to execute a program on the output of another program.

ls * > file
grep "name" file

is equivalent to

ls * | grep "name"

As for how it they work internally, I am only learning that my self now. But I found this link which offers some discussion on it.

How Does Piping Work in Linux?

You should use piping if you want to pass outputs between programs; use redirection if you want to write to a file.


Redirection is (mostly) for files (you redirect streams to/from files).

Piping is for processes: you pipe (redirect) streams from one process to another.

Essentially what you really do is "connect" one standard stream (usually stdout) of one process to standard stream of another process (usually stdin) via pipe.

Pipes have also the synchronization "side effect" : they block one process (on reading) when the other has nothing to write (yet) or when reading process cannot read fast enough (when the pipe's buffer is full).