Standard I/O & Error RedirectionThe numbers 0, 1, and 2 refer to the standard input, standard output, and standard error, respectively.

I/O Redirection

  • > Redirect the standard output to a file or device, it creates the file, if it does not exists and overwrites if does exists
  • >! Forces the overwriting of a file, if already exists, thus overriding noclubber option.
  • >> Appending the output to the end of the file. It can be used to unify multiple files
cat file1 file2 file3> new-file
  • < Redirect the standard input from a file or device to a program or command

Error Redirection

When a command fails to execute, the system issues an error message which gets displayed on a screen by default. To keep the record of the error messages, they are redirected to a file using special redirection operators.

  • 2> Redirect the standard error to a file or device
  • 2>> Redirect and append the standard error to a standard output
  • 2>&1 Redirect the standard error to standard output
  • >& Redirect the standard error to a file or device

Pipes

  • Command | Command Pipe the standard output of one command as input for another command
  • Command | &Command Pipe the standard error as input to another command in the C-shell

The Pipe | operator placed between two commands forms a connection between them. They make the CLI incredibly powerful, efficient and versatile. It creates a unidirectional data channel used for interprocess communication.

  • Using pipe with less
    dig ananova.com | less
  • Count the number of lines of output after command execution
    grep “Linux Hosting” *.php | wc -l
  • With sort
    du -khs * | sort -h

Named Pipe 

mkfifo <pipe-name>

mknod p <pipe-name>

Its special file, which follows FIFO (First-in, First-out character) mechanism used even over multiple shell sessions. The size of the named pipe is zero and has a designation of “p.” They resided in memory rather than being written to disk.

$ mkfifo hosting

$ ls -l hosting