AI Hints
Home›Linux Tutorial›Folders and Wildcards
Linux · Lesson 28 of 120

Removing Folders

Rmdir and rm -r.

⏱ 2 min read▶ Video at 34:12🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

Deleting a folder is a little different from deleting a file. Linux protects you here, by making you say clearly what you want.

Removing Folders
From the video: Removing Folders
  • Folders are different
  • Linux protects you
  • Say clearly what you want

rmdir for empty folders

RM dir removes a folder, but only if it's empty. Here we make an empty folder called old, and RM dir removes it. But Documents has files inside, so RM dir refuses, and says, directory not empty. That's a safety net.

Terminal
mkdir old
rmdir old
rmdir Documents
rmdir: failed to remove 'Documents': Directory not empty

rm -r for folders with files

To delete a folder and everything inside it, use RM dash R, for recursive. It deletes the folder, every file in it, and every folder inside that, all the way down. One command, and the whole Music folder is gone.

Terminal
rm -r Music
ls
Documents  Downloads  Pictures  notes.txt  todo.txt

The command to respect

You'll see RM dash RF online. The F means force. Don't ask, don't complain, just delete. Combined with the wrong path, it can wipe out your work in a second, with no undo. Always double check the path. And never run RM dash RF with a slash, or a tilde, unless you're absolutely sure.

keyvalue
rm -rdelete a folder and its contents
-fforce: no questions
No undogone for good
Alwaysdouble check the path

Summary

RM dir removes empty folders, and RM dash R removes folders with everything inside. Treat RM dash RF with great respect. Next, wildcards. Working with many files at once.

Key takeaways
  • rmdir: empty folders
  • rm -r: everything
  • Respect rm -rf
Next up: Wildcards — many files at once