Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This question may sound a bit stupid, but I can not really see the difference between redirection and pipes.

Redirection is used to redirect the stdout/stdin/stderr, e.g. ls > log.txt.

Pipes are used to give the output of a command as input to another command, e.g. ls | grep file.txt.

But why are there two operators for the same thing ?

Why not just write ls > grep to pass the output through, isn't this just a kind of redirection also ? What I am missing ?

share|improve this question
add comment

4 Answers 4

up vote 25 down vote accepted

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. saved 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 renamed 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).

share|improve this answer
    
Thank you. You mentioned thing1 > temp_file && thing2 < temp_file to do more easier with pipes. But why not re-use the > operator to do this, e.g. thing1 > thing2 for commands thing1 and thing2 ? Why an extra operator | ? –  John Threepwood Aug 7 '12 at 13:57
    
"Take the output and write it to a file" is a different action than "Take the output and pass it to a different program". I'll edit more thoughts into my answer... –  David Oneill Aug 7 '12 at 14:01
    
@JohnThreepwood They have different meanings. What if I wanted to redirect something to a file named less, for example? thing | less and thing > less are perfectly different, as they do different things. What you propose would create an ambiguity. –  Darkhogg May 25 at 9:55
    
Is it accurate to say that "thing1 > temp_file" is merely syntactic sugar for "thing1 | tee temp_file" ? Since finding out about tee I almost never use redirects. –  Sridhar-Sarnobat Jun 5 at 5:09
    
@Sridhar-Sarnobat no, the tee command does something different. tee writes output to both the screen (stdout) and the file. Redirect does only the file. –  David Oneill Jun 5 at 9:16
add comment

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.

share|improve this answer
    
Thank you. This is a good reason to have two operators. –  John Threepwood Aug 7 '12 at 13:57
add comment

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 Pipe is used to send the output to other command whereas to redirect is used to redirect the output to some file.

Hope this is helpful.

share|improve this answer
add comment

Redirection means change the direction of input and output. Piping means change the flow of one command output to another command as input.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.