Shell: Redirect/Pipe

We can redirect standard output and error to files instead of the screen. We can also redirect standard input from a file instead of the keyboard.


Redirecting Standard Output

COMMAND > FILE

Redirect COMMAND's output to FILE. If COMMAND outputs nothing, an empty file called FILE will be made. If FILE exists, its content will be replaced by the output. To write to an existing file without clobbering its content, use ">>" as below:

COMMAND >> FILE

">" equals to "1>", ">>" equals to "1>>", as standard output's file descriptor is 1.


Redirecting Standard Error

COMMAND 2> FILE

Redirect COMMAND's error messages to FILE. 2 is standard error's file descriptor.

If COMMAND outputs nothing, an empty file called FILE will be made. If FILE exists, its content will be replaced by the output. To write to an existing file without clobbering its content, use "2>>" as below:

COMMAND 2>> FILE


Redirecting Standard Input

COMMAND < FILE

Use FILE's content as COMMAND's input, only useful for commands that read standard input.


Redirecting All of them

COMMAND < FILE1 > FILE2 2> FILE3

To redirect standard output and standard error to the same file, the method below is recommended:

COMMAND >> FILE 2>> FILE
This method is superior to others as it works in any shell and easy to understand and remember. In this method, the sequence of ">>" and "2>>" doesn't matter. If COMMAND only output either normal output or error messages, then ">>" can ,of course, be replaced by ">" in this method.


Piping

Piping is to use a shell's standard output as another's standard input.

COMMAND1 | COMMAND2

COMMAND2 gets COMMAND1's standard output as its standard input instead of from the keyboard.

For example, the command below reads 2 files and sorts all the lines and prints the 10th to the 20th of them.

cat file1 file2 | sort | head -20 | tail -10
  1. "cat file1 file2" reads both "file1" and "file2" and concatenate them to its standard output.
  2. "sort" reads that and write the sorted version to its standard output.
  3. "head -20" reads that and trim the first 20 lines to its standard output.
  4. "tail -10" reads that and trim the last 10 lines to its standard output, which is connected to the screen.


Related

Comments

Popular

Intel 7th and 8th generation processor graphics driver for Windows 7/8/8.1

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part II - Fidelity

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part IV - Decode

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part I - Lossless

OBS Studio settings for best quality (loseless) recording

AV1 vs VP9 vs AVC (h.264) vs HEVC (h.265): Part III - Quality

Install VMAF on Fedora, CentOS/Red Hat (RHEL)