How do you delete a file?
Type rm and the file name. rm old-menu-2024.csv deletes the file, prints nothing, and the prompt comes back. You can list several at once, as in rm old-menu-2024.csv old-specials.txt, and each one goes. The name is short for remove.
If the name is wrong, rm stops with rm: cannot remove 'old-menu.csv': No such file or directory and deletes nothing. That error is a good outcome. The bad outcome is a name that is right but means the wrong file, which is why the rest of this page is about checking first.
rm old-menu-2024.csv
lsWhere do deleted files go?
Nowhere. The command line has no recycle bin or trash. When rm removes a file, the space it used is marked free and the name disappears. You can’t undo it, and there’s no “are you sure” message by default. On a server, unless someone set up backups, that file is gone.
This is the main difference from deleting in a file manager, where the file sits in a bin until you empty it. Some people install a tool called trash-cli on their own computers, which adds a trash command that sends files to the normal bin. It’s a sensible extra on a laptop. On servers, you can’t count on it being there, so it pays to learn careful habits with rm itself.
How do you remove a folder?
There are two ways, and the difference is the whole point. rmdir tmp removes a folder only if it’s empty. If anything is inside, it refuses: rmdir: failed to remove 'drafts': Directory not empty. That refusal is a free safety check, so reach for rmdir first.
rm -r drafts removes the folder and everything inside it: files, folders inside folders, all of it, without listing what it deleted. The -r stands for recursive. It’s the right tool for a folder you’ve looked inside and truly don’t want, such as an old test copy of a website.
Plain rm on a folder refuses with rm: cannot remove 'drafts': Is a directory. You can’t delete a folder you’re inside either: this practice terminal stops you, and on a real system you’d end up in a folder that no longer exists. Move out with cd .. first.
rmdir tmp
ls drafts
rm -r drafts

rmdir only removes empty folders, so on drafts it refuses and you lose nothing. rm -r deletes everything inside first, then the folder, and there is no bin to undo it.How do you check before you delete?
Run ls with exactly the same names you’re about to give rm. If you plan rm -r drafts, first run ls drafts and read what’s inside. If you plan rm *.csv, first run ls *.csv. The list you see is the list that will go. When it looks right, press the Up arrow, change ls to rm, and press Enter. That way the names can’t change between checking and deleting.
Check where you are with pwd, too, especially when the command uses a relative name or a wildcard. rm -r images in ~/bakery and the same line in ~/bike-shop/website delete very different things. The prompt shows your folder; read it every time before a delete.
Before deleting anything you might want later, make a copy or an archive. cp -r drafts drafts-old takes a second, and a later lesson shows how to pack a folder into a single .tar.gz file.
pwd
ls drafts
rm -r draftsWhat do -i and -f do?
rm -i asks before each file: rm: remove regular file 'menu.csv'?. Type y and Enter to delete, or anything else to keep it. It’s good when you’re deleting a handful of files by hand and want a last look. rm -I, with a capital, asks once before deleting more than three files or before a recursive delete, which is less tiring. In this practice terminal, -i always answers no, so nothing is deleted and a note tells you what would have been asked.
-f means force. It does two things: it never asks, even about write-protected files, and it doesn’t complain about names that don’t exist. A write-protected file shows as -r--r--r-- in ls -l, and a real rm stops to ask about it; rm -f skips the question. This practice terminal deletes it with a note, so read the ls -l columns yourself.
Because -f hides errors, a typo in the name produces no message at all. Use it when you’ve checked the name and want the command to finish without stopping, and leave it off the rest of the time.
Why is rm -rf dangerous?
rm -rf combines both options: delete folders and everything inside, and never ask or complain. It’s fast and it’s often the right command for clearing a build folder. It is also how people delete whole projects by accident.
The classic accidents are small typos. A space in rm -rf ~ /old-site, meant as ~/old-site, turns one argument into two, and the first one, ~, is your whole home folder. A variable that turned out empty in a script makes rm -rf "$DIR"/ point at the top of the disk. Modern GNU rm refuses to delete / itself without an extra option, and this practice terminal won’t delete your home or /, but neither safeguard protects the folders you work in.
Habits that help: type the folder name in full, never only a wildcard; run ls on the same path first; prefer rm -r without -f so a surprise question can stop you; and never run rm -rf with sudo unless you know exactly why it needs it.
ls build
rm -r buildWhat about names with spaces or odd characters?
A space splits a name into two. rm old notes.txt tries to delete a file called old and another called notes.txt. If notes.txt exists, it goes, and that’s the wrong file. Put quotes around the full name: rm "old notes.txt". Tab completion adds the backslashes for you.
A file whose name starts with a hyphen, such as -draft.txt, looks like an option to rm. Use rm ./-draft.txt or rm -- -draft.txt; the -- means “no more options after this”.
What do AI tools get wrong here?
AI assistants reach for rm -rf for almost every cleanup, often with a wildcard: rm -rf node_modules dist *.log. Read every name in a suggested delete and ask what it matches in your folder, not theirs. Run ls with the same names before anything else. If a reply includes sudo rm -rf or a path starting with /, stop and find out why.
They also suggest deleting a folder to “fix” something, such as a cache or a config, without saying what you’ll lose. Before following that advice, copy the folder somewhere safe, or rename it with mv so you can put it back if the fix doesn’t work.
Field notes
rmdeletes files at once, and there is no trash to get them back.rmdirremoves only empty folders;rm -rremoves a folder and its contents.- Run
lswith the same names before you runrm. rm -iasks before each file, which helps when you are unsure.
Easy to get wrong
Plain rm won’t remove a folder. Try rmdir first: it refuses when the folder has something in it, which gives you a chance to look.
The stray space makes ~, your whole home folder, its own argument. Keep the path in one piece, and leave off -f so you’re warned about anything odd.
Without quotes, rm sees two names and may delete a different file called notes.txt.
ls with the same pattern shows exactly what will be deleted. Then press Up and change ls to rm.
Questions people ask
Can I get a deleted file back?
Usually not. There’s no bin, and recovery tools rarely work on a busy disk. Your real options are backups, a copy in Git, or a copy on another computer. Make one before big deletes.
Is rm the same on a Mac?
Yes for everyday use. Mac rm has the same -r, -i and -f options and no bin either. Deleting in Finder is different: that goes to the Trash.
Should I alias rm to rm -i?
Some people do, and some servers come set up that way. The risk is getting used to the question and then running plain rm on a computer that doesn’t ask. Checking with ls first works everywhere.
What does rm -v do?
It prints each name as it’s removed. With rm -rv drafts it lists each file inside, such as removed 'drafts/easter-flyer.txt', and then removed directory 'drafts'.