Pipeline "|" versus output redirection ">"


The uses of output redirection, >, and pipelining, |, seem similar. What is the distinction in their use?


The distinction here is between storage and use as input. Consider the following example

cat volcanoes.txt | wc

from this lesson. Here we’re using the result of the left side, cat volcanoes.txt, as input to the right side, wc. If we instead wrote

cat volcanoes.txt > wc

then we would use the result of the left side and store it into a file called wc. This is a very different behavior than what we did with | above.


I was confused by this lesson:
$ cat volcanoes.txt | wc | cat > islands.txt

if we want to “pipe” the standard output of volcanoes.txt to islands.txt, could we write
$ cat volcanoes.txt | wc > islands.txt ? cat islands.txt turns out the same result as the lesson.

And please explain to me what the difference between storage and use as input?


I’m confused about this as well. Could someone help plz?


wc fav-cities.txt > cities.txt appears to do the same thing. Is the piping just to create a use case for it? Is there a better example of piping that does not contain a command such as wc, since wc gives output already?


I am joining the earlier question. Also, confused about the role of wc in this setting. It was written in the initial explanation " the wc command outputs the number of lines, words, and characters in volcanoes.txt , respectively", but still I am not really getting it. @zjedwin I would appreciate more information on this redirection command.


So… Is it safe to say that …

$ cat boats.txt | sort > sorted-boats.txt

Just means …

Display contents of “boats.txt”

WHILE - organizing the content inside “boats.txt”

THEN - Create a new .txt called “sorted-boats.txt” with the contents that we organized in “boats.txt” inside



Drawing on (reading) the content from boats.txt, sorting it and storing it in sorted-boats.txt. The original file will remain untouched.


The lines
cat volcanoes.txt | wc | cat > islands.txt
cat volcanoes.txt | wc > islands.txt
do the same thing. There will often be many ways to accomplish any task. No need to be confused.

For the difference between “storage” and “use as input”, simply note that there is a difference between saving something for later and using it immediately and then throwing it away.
With x | y, you’re saying: take the output of x and use it as the input for y. You don’t save the output of x; you’re just passing it along to y. So you’re using it as input rather than storing it someplace.
On the other hand, x > y says: save, i.e. store, the output of x into the location defined by y.


wc is not a redirection command. It simply provides some statistics on an arbitrary file that is passed to it. In particular
number_of_lines number_of_words number_of_characters file_name
Simply test what the output of this function shows for an arbitrary file on your machine. Like this:
wc my_file
Redirection comes from operators like | and > which simply allow you to chain commands together.


I think I understand the use of the pipeline command well but what I don’t understand is why some of the exercises add unneccesary steps like doing
$ cat text_file.txt | sort ETC.
instead of just starting with
$ sort text_file.txt ETC.
I was able to complete these challenges without the extra useless steps and wasn’t sure what the point of those steps were; was it just to make it easier for beginners to understand by showing the whole flow of steps?, or to get us used to a certain conventional way of using the command line?, or just an accident?


can I also write this then?

$ sort boats.txt > sorted-boats.txt

Yes I have the same question too!
Would this work too?:
$ sort boats.txt > sorted-boats.txt

Can we consider ‘wc’ as a temporary variable where we save our result of cat volcanoes.txt?

wc is a command literally ‘word count’, you need specify input for it to do the count on. in our example cat volcanoes.txt | wc | cat > islands.txt

  1. output content of volcanoes.txt file to the terminal
  2. count words what was outputted in step 1
  3. output to terminal the result of word count from step 3 (actually redundant step as was outlined before)
  4. save output to file islands.txt

I get that the course was trying to show the chaining of pipes in that example. But if it does exactly the same thing as the other one, it’s not a good example imo. Could you show a situation where it clearly shows the point of chaining?

1 Like

What is the difference then between redirecting stdin and piping?

Piping ‘|’ allows command to command redirection without storing the data and executing the chained command.
The redirecting operator ‘>’ redirects an output as input for storage.

Lets look at an example,

cat volcanoes.txt | wc > word_count_volcanoes.txt

The above command will execute cat cmd outputting the data from volcanoes.txt which is then redirected into the wc cmd using ‘|’ and processed, producing new data followed by the final redirect which takes the output of the wc and uses as input to store in a file.

What would happen if we replace the pipeline in the command with a redirection ?

cat volcanoes.txt > wc > word_count_volcanoes.txt

In this case the output data from cat cmd is redirected as input to wc but wc isn’t treated as a cmd instead as a storage point (file). The final redirect would pass the output data of wc storage point to word_count file.

In the word count file you would find the same data as volcanoes.txt

Use pipeline when you want to chain commands and process the input data.
Use redirection operators when you need to store into a variable or file.

I know this is hella after your post, but bravo @zjedwin

A pipe is to CMD as a HOF is to function().