AI Learning Hub › Learn AI › Self-Paced Courses › Terminal on Mac: Reading Commands Before You Run Them › Putting Commands Together
Module 7 ยท Beginner
Putting Commands Together
A pipe sends one command's output into another. A redirect sends it to a file, and one of the two forms empties that file first.
By the end
- Send one command’s output into another with a pipe
- Write output to a file, and know which form empties it first
- Read
&&in a command somebody else wrote - Recognize the four operators on sight
Until now every command has stood alone: you type one thing, it prints something, the prompt returns. This module is about the characters that join commands together, and it is the step that makes AI-generated command blocks readable rather than cryptic.
There are four, and the course teaches no others.
The four
- A vertical bar, called a pipe:
| - Send the output of the command on the left into the command on the right, instead of to the screen.
- A single angle bracket:
> - Send the output into a file. If the file already has contents, they are replaced.
- Two angle brackets:
>> - Send the output into a file, added to the end. Existing contents stay.
- Two ampersands:
&& - Run the command on the right only if the command on the left succeeded.
Three of those move output somewhere. The fourth is about sequencing. Only one of the four destroys anything.
Pipes
A command normally prints to the screen. A pipe intercepts that and feeds it to another command as input. The classic pairing is a search with a counter:
Try it
grep -R "Jackson" . grep -R "Jackson" . | wc -l
Run both. The first prints the matching lines; the second prints 3. wc counts things and -l asks for lines, so the pipeline means: find every mention, then tell me how many there were rather than showing me each one.
That is the whole idea. Neither command knows about the other; the shell connects them. Run the first on its own before the pipeline and the second makes immediate sense — which is a reasonable way to read any pipeline you meet.
Redirects
Instead of another command, you can send output to a file.
Try it — writes into the practice folder
echo "Research complete" > output/status.txt cat output/status.txt echo "Reviewed by me" >> output/status.txt cat output/status.txt
Two lines in the file. Now watch the difference:
Read this — it empties the file first
echo "Starting over" > output/status.txt
Run that and the file holds one line. The two you had are gone, without a prompt and without any output saying so.
> is a destructive operator that does not look like one. It empties the file before writing, every time, whether or not you intended to. The difference between > and >> is one character and the difference in consequence is everything in the file. When you read a command somebody else wrote, this is one of the things to look for.
Chaining with &&
You will see this constantly in instructions and in AI output, usually joining a cd to whatever comes next:
Try it
cd cases && ls
Move into cases, and if that worked, list it. The && matters because of what happens when the first part fails. Try cd nowhere && ls and you get the error from cd and nothing else — the ls never runs.
That is a safety property worth recognizing. A command that begins cd somewhere && rm ... will not perform the removal if the cd fails, which is precisely why it was written that way.
One thing to notice before moving on: you are now standing in cases. The cd was part of the command, but its effect outlasts it — every command you type next is measured from there until you move again. Go back before the exercise, which assumes the practice folder itself.
Try it
cd .. pwd
Reading a joined command
Put the four together and you can take apart a line you did not write. For example:
Read this — what would it do?
cd ~/terminal-practice && grep -R "Jackson" . | wc -l > output/count.txt
Taken left to right: move into the practice folder; if that worked, search everything below it for Jackson; send those results into wc -l to be counted instead of displayed; and write the number into output/count.txt, replacing whatever was in that file.
Four operators, one line, and nothing in it is mysterious once you read the pieces in order. That is the skill Module 9 asks you to use on commands you have more reason to be careful about.
Exercise: build one, then read one
- Count how many
.txtfiles the practice folder contains, by pipingfindintowc -l. - Write that count into
output/filecount.txt. - Add a second line to the same file with
>>, then check it has two lines. - Now overwrite it with
>and confirm it has one. - Without running it, say what this would do:
cd notes && grep -R "docketing" . > found.txt. Where wouldfound.txtbe created, and what would it contain?
Check your answers
The count in step 1 is probably not five. Module 6 found five .txt files, and you may well see six or seven here. Nothing is broken: output/status.txt is a .txt file you created a few minutes ago, and output/filecount.txt counts itself, because the shell creates the file for the > before find starts walking the folder. Scope is measured when the command runs, not when you read it — which is the whole of the capstone.
found.txt would be created inside notes, because the cd ran first and the path is relative. If you expected it in the practice folder root, that is the single most common misreading of a chained command, and it is the reason Module 2 spent its time on relative paths.
It would be empty. docketing appears in cases/opinion-a.txt, and the cd notes means the search never looks there — the . is notes now. The > still creates found.txt, inside notes, because redirection creates the file whether or not the command produces any output. An empty result file is a real result worth recognizing: it tells you the search ran, not that the term is absent from your material.