Lecture # 22 - Redirection and Pipes

Redirection:

Redirection is used to save the output of a command in a file instead of showing the output on the terminal. By default the output is shown on the terminal but when we use redirection, we can save the output of an executed command into a file.

Streams:

  • 0 - Standard Input (stdin)

  • 1 - Standard Output (stdout)

  • 2 - Standard Error (stderr)

here 0, 1, and 2 are file descriptors.

Overwrite:

This deletes last data and overwrite it with new data.

  • Standard Output ( > )

  • Standard Error ( 2> )

  • Standard Input ( < )

  • If error: overwrite in another file, if no error: owerwrite in another file

  • If error or no error overwrite in the same file

Append:

This adds new data after the last data.

  • Standard Output ( >> )

  • Standard Error ( 2>> )

  • Standard Input ( << )

  • If error: append in another file, if no error: append in another file

  • If error or no error append in the same file

Pipes:

Pipes are used to redirect a stream from one program to another program. Only the data returned by the second program will be displayed. Pipe is represented by a vertical bar ( | ). Its syntax is [command-1] | [command-2]

Difference between Pipes and Redirects:

  1. Redirection is used to connect a command with a file.

  2. Pipes are used to connect output of one command with the input of second command.

0
Subscribe to my newsletter

Read articles from Abdullah Bin Altaf directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abdullah Bin Altaf
Abdullah Bin Altaf