Difference between Redirection and Pipe in Linux

      Comments Off on Difference between Redirection and Pipe in Linux

Now and then, I meet symbols such as >, >>, &&, ||, and  | between Linux commands in CLI. Every time I had a hard time recalling the concepts and difference between Redirection and Pipe, I wanted to write a short but to-the-point summary of it. Simple but astonishing idea is easier to remember.

 

  • Redirection – screen to file redirection

Only used to redirect what should have been printed on the screen to a file.

For the standard output stdout,   COMD > file overrides the file,  COMD >> file append texts to the end of the file.

For the error output stderr, use 2> and 2>> correspondingly. To redirect all screen output, use &> and &>>.

< and << are input to file redirection which redirects what you typed, stdin, to a file.

The essence of redirection is everything in Linux is a file. The default output file is the screen file – /dev/stdout and /dev/stderr. The default input file is /dev/stdin. Redirection is just let shell write to the specified file instead of the stdin or stdout

 

  • Pipe – concatenate commands by feeding output to input

COMD1 | COMD2 is the basic pipe symbol which feed the stdout to the input of next command. The pipeline stops when stderr appears.

COMD1 || COMD2 only runs COMD2 when COMD1 has error.

COMD1 && COMD2 only runs COMD2 when COMD1 succeeds.

I see | used with grep most of the time. It is very worthy to be familiar with this kind of command: ps -ef|grep rsync which looks for string “string” from a full list of all processes.