FAQ: Redirection - |

This community-built FAQ covers the “|” exercise from the lesson “Redirection”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn the Command Line

FAQs on the exercise |

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I have a question concerning my comprehension of the | effect here…
cat volcanos.txt | wc outputs the number of lines, words and characters in volcanoes.txt (I understand that). cat volcanoes.txt | wc | cat > islands.txt (from what I understand, cat islands.txt output became volcanoes.txt | wc output).
Is that correct, and especially is it a good way to say it? - sorry I try to understand by verbelazing the concept.
Thank you for your answer.


I think you are right.

How I would phrase it is:

  • cat volcanoes.txt | wc : the content of volcanoes.txt is provided to the command wc by the pipe, which provides the number of lines, words and characters of the provided content. Other way more complicated to say it:
    • “stdin” of cat is the file volcanoes.txt.
    • “stdout” of cat volcanoes.txt is the content of the file volcanoes.txt
    • “stdin” of wc is the “stdout” of cat volcanoes.txt (because of the pipe).
    • “stdout” of cat volcanos.txt | wc is the number of lines, words and characters of the content of the file volcanoes.txt.
  • cat volcanoes.txt | wc | cat > islands.txt : the “stdout” of cat volcanos.txt | wc is the new input of the second cat wich is then redirected to the file “island.txt” through the > command (i.e. the content of “island.txt” is replaced by the output of the second cat, here the number of lines, words and characters of the content of the file volcanoes.txt)

Why to use the second cat in cat volcanoes.txt | wc | cat > islands.txt?

Why not simply put cat volcanoes.txt | wc > islands.txt?



Thank you for your answer. I am not familiar yet with all that but I think you answered my question.
I understand but it is still not “natural” for me, like a language that I am learning (that is what I am doing actually) but do not master at all yet, I am far from fluent.
And concerning your “Why” in your second message it is simply because the exercice explained the redirections this way.
Thank you again :slight_smile:

I am questioning the same thing. Anyone?

I have tested it on my system and you are right. It would have the exact same effect in a more efficient way. I believe they just presented it this way to illustrate that double piping is possible. As the command for piping | could not be directly followed by the command to redirect > it is necessary to open the output of ‘cat volcanoes.txt | wc’ again so it could then be redirected to islands.txt, therefore definitely a redundancy. That’s my understanding but I’m no expert, so take it with a grain of salt!!

1 Like

Why is the man command disabled (or excluded) in the interactive shell? I would hazard to say that this is a pretty basic program that should be included for students learning unix based systems.

It is especially helpful to use when looking up the format of the wc command.

I was confused about the differences between piping and redirection. This article helped a lot

1 Like

Not sure if anyone is still confused, but i realised that the command

cat > island.txt

creates a file called island.txt in your current directory.
If you try this line of code, your usual $ sign goes missing on the left side, in order for the user to type in the content of the new txt file manually, until they press the CTRL+D keys, to exit. you can try this and cat island.txt to see the new content of your created file.

So to break it down:

cat volcanoes.txt | wc → this has an output of the wordcount of the content of volcanoes.txt,
cat > islands.txt → this creates a new file and waits for User input for the content of said new file

cat volcanoes.txt | wc | cat > islands.txt → by combining the two, you input the content of the New file, as the wordcount of the volcanoes.txt

Hope this helps!

user [codedcolt0961340047]'s link helped! check it out if still unsure.

Im a noob when it comes to this, so if im wrong, lmk!

edit: not sure why, but seems like

cat volcanoes.txt | wc > islands.txt → also creates the new file. Then im not sure about the functionality of the last “cat” too

It’s totally unnecessary, I don’t think there’s a good reason for it other than maybe the author felt it more explicitly showed what was going on.

This article is worth reading if you are questioning why this would be used.