Now and then, I meet symbols such as
| 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
COMD > file overrides the file,
COMD >> file append texts to the end of the file.
For the error output
2>> correspondingly. To redirect all screen output, use
<< 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/stderr. The default input file is
/dev/stdin. Redirection is just let shell write to the specified file instead of the
- 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
COMD1 || COMD2 only runs
COMD1 has error.
COMD1 && COMD2 only runs
| 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.