What is the difference between "Redirection" and "Pipe"?

Pipe is used to pass output to another program or utility.

Redirect is used to pass output to either a file or stream.

Example: thing1 > thing2 vs thing1 | thing2

thing1 > thing2

  1. Your shell will run the program named thing1
  2. Everything that thing1 outputs will be placed in a file called thing2. (Note - if thing2 exists, it will be overwritten)

If you want to pass the output from program thing1 to a program called thing2, you could do the following:

thing1 > temp_file && thing2 < temp_file

which would

  1. run program named thing1
  2. save the output into a file named temp_file
  3. run program named thing2, pretending that the person at the keyboard typed the contents of temp_file as the input.

However, that's clunky, so they made pipes as a simpler way to do that. thing1 | thing2 does the same thing as thing1 > temp_file && thing2 < temp_file

EDIT to provide more details to question in comment:

If > tried to be both "pass to program" and "write to file", it could cause problems in both directions.

First example: You are trying to write to a file. There already exists a file with that name that you wish to overwrite. However, the file is executable. Presumably, it would try to execute this file, passing the input. You'd have to do something like write the output to a new filename, then rename the file.

Second example: As Florian Diesch pointed out, what if there's another command elsewhere in the system with the same name (that is in the execute path). If you intended to make a file with that name in your current folder, you'd be stuck.

Thirdly: if you mis-type a command, it wouldn't warn you that the command doesn't exist. Right now, if you type ls | gerp log.txt it will tell you bash: gerp: command not found. If > meant both, it would simply create a new file for you (then warn it doesn't know what to do with log.txt).


From the Unix and Linux System Administration Handbook:

Redirection

The shell interprets the symbols <,>, and >> as instructions to reroute a command's input or output to or from a file.

Pipes

To connect the STDOUT of one command to the STDIN of another use the | symbol, commonly known as a pipe.

So my interpretation is: If it's command to command, use a pipe. If you are outputting to or from a file use the redirect.


If the meaning of foo > bar would depend on whether there is a command named bar that would make using redirection a lot harder and more error prone: Every time I want to redirect to a file I first had to check whether there's a command named like my destination file.


There's a vital difference between the two operators:

  1. ls > log.txt --> This command sends the output to the log.txt file.

  2. ls | grep file.txt --> This command sends the output of the ls to grep command through the use of pipe (|), and the grep command searches for file.txt in the in the input provided to it by the previous command.

If you had to perform the same task using the first scenario, then it would be:

ls > log.txt; grep 'file.txt' log.txt

So a pipe (with |) is used to send the output to other command, whereas redirection (with >) is used to redirect the output to some file.