AI Learning Hub › Learn AI › Self-Paced Courses › Terminal on Mac: Reading Commands Before You Run Them › Removing Things
Module 5 ยท Beginner
Removing Things
The shortest module and one of the most important. What rm takes, what rm -r takes, and how a single asterisk decides how many files are involved.
By the end
- Say exactly what a removal command will take
- Explain what
rm -radds - Work out how many files an asterisk stands for, before running anything
- Know what the Trash does and does not have to do with this
This is the shortest module in the course. It has its own page because the commands in it are the ones most worth slowing down for, and folding them into the previous module would guarantee they got read last and least.
There is nothing frightening here. These commands are ordinary and you will use them. What they are not is forgiving, and the whole of the difference is in reading the line before you run it.
What rm does
rm removes a file. It does not ask, it does not print anything, and the prompt simply comes back.
It is also not the Finder’s delete. When you delete something in Finder it goes to the Trash, where it waits until you empty it. The manual page for rm never mentions the Trash, and a file removed this way is not sitting in it. Do not expect the Trash workflow after using rm.
That wording is careful on purpose. Whether a removed file could be recovered by some other means is a separate question this course does not get into. The practical point is narrow: the thing you would normally reach for is not going to be there.
What -r adds
Try to remove a folder and rm declines:
What happens
rm archive rm: archive: is a directory
Adding -r — for recursive — tells it to go into the folder and remove everything inside, and everything inside those, all the way down. One short option turns a command that affects one file into a command that affects a whole tree.
None of which is a reason to avoid it; rm -r is how you remove a folder, and you will want to. It is a reason to read the path attached to it with real attention, because rm -r on the wrong folder takes everything that folder contained.
The asterisk decides how many
An asterisk in a path is a wildcard: it stands for any run of characters. *.txt means every name ending in .txt.
The part worth understanding is who expands it. The shell does, before the command runs. By the time rm starts, it has not been handed a pattern; it has been handed the finished list of files that matched. The command has no idea a wildcard was involved.
You can watch this happen with a harmless command. From the practice folder:
Try it — this only prints
echo notes/*.txt echo cases/*.txt
echo prints whatever it is given, so it shows you exactly what the shell handed over. Three files in one case, two in the other. Put rm where echo is and that is the list it would have removed.
This is the single most useful habit in the module. When a command with a wildcard makes you hesitate, run it as echo first. You will see the real list, nothing will happen to it, and then you can decide.
The one case where it does ask
For completeness, because the course should be accurate rather than absolute: the manual describes one situation where rm prompts. If the file’s permissions do not allow writing and you are working at a terminal, it asks for confirmation first.
That is a narrow case and not something to rely on. Most files you meet are writable, and most removals happen in silence.
Exercise: say what each one would take
Do not run these. Read each line and write down which files it would remove, assuming you are in ~/terminal-practice. The point of the exercise is the prediction.
Read these — do not run them
rm notes/case-list.txt rm notes/*.txt rm -r archive rm -r .
Then check your answers with echo, which is safe:
Safe to run
echo notes/case-list.txt echo notes/*.txt
Check your answers
Line 1 removes one named file. Unambiguous, and the easiest kind of removal command to read.
Line 2 removes three: case-list.txt, research-notes.txt and My Research File.txt. If you expected one or two, that is exactly the gap this module exists to close. The asterisk did not mean “the file I was thinking of”.
Line 3 removes the archive folder and everything in it.
Line 4 is the one to notice. A single dot means the current folder, so this says remove everything here, recursively — the entire practice folder. It is a perfectly ordinary-looking line, four characters long, and it is the broadest command on the page. Nothing about how it looks signals that.
That is the lesson. Scope does not announce itself. You work it out by reading the path and the options, every time.