The command to delete a directory depends on whether it's empty
To delete an empty directory, use rmdir followed by the directory name. To delete a directory that contains files or other directories, use rm -r followed by the directory name. The difference matters because rmdir will refuse to delete anything with contents inside, while rm -r will delete the directory and everything in it without asking again.
Before you run either command, navigate to the parent directory — the folder that contains the directory you want to delete. You can see what's inside your current location by typing ls and pressing Enter. Once you're in the right place, the commands are straightforward.
Key Takeaways
- Use rmdir directoryname to delete an empty directory; the command will fail if anything is inside.
- Use rm -r directoryname to delete a directory and all its contents at once, without confirmation prompts.
- Navigate to the parent directory first using cd, then run the delete command with just the directory name.
- Deleted files and directories do not go to a trash bin — they are removed permanently, so double-check the path before pressing Enter.
Deleting an empty directory with rmdir
The rmdir command is the safest choice when you know a directory is empty. Type rmdir foldername and press Enter. If the directory is truly empty, it will disappear with no message — silence means success in the terminal.
If the directory contains anything — even a hidden file — rmdir will print an error message like "Directory not empty" and refuse to delete it. This built-in safety feature prevents accidental loss of files. If you see this error, you have two choices: manually delete the contents first, or use rm -r instead.
Deleting a directory with all its contents using rm -r
The rm -r command deletes a directory and everything inside it in one action. The -r flag stands for "recursive," meaning it works through all nested folders and files. Type rm -r foldername and press Enter.
Unlike rmdir, rm -r will not stop to ask "are you sure?" — it will delete immediately and permanently. There is no undo and no trash bin to recover from. For this reason, many people add a safety step: type ls foldername first to see what's inside, then run the rm -r command only after you've confirmed you want everything gone.
Checking what's inside before you delete
To see the contents of a directory without deleting it, use ls directoryname. This shows all visible files and folders inside. If you want to see hidden files too (files that start with a dot), type ls -a directoryname.
For a more detailed view that shows file sizes and dates, use ls -l directoryname. These commands let you confirm what you're about to delete before you run rm -r. Taking thirty seconds to look first can save you from losing something important.
Deleting directories with spaces or special characters in the name
If a directory name contains spaces, put the name in quotes: rm -r "my folder" or rmdir "my folder". Without quotes, the terminal treats each word as a separate argument and the command fails.
For names with special characters like hyphens or parentheses, quotes also work: rm -r "folder-name" or rm -r "my (old) files". If you're unsure whether a name needs quotes, use them anyway — they don't hurt. This approach works consistently across Mac, Linux, and Windows terminals.
Using the full path instead of navigating first
You don't have to navigate to the parent directory. You can delete a directory from anywhere by typing the full path. For example, rm -r /Users/yourname/Documents/oldfolder will delete oldfolder even if you're currently in a different location.
Full paths are useful when you're working in one place but need to clean up somewhere else. Start with a forward slash to indicate the root of your system, or start with a tilde (~) to indicate your home directory. For example, rm -r ~/Downloads/tempfiles deletes tempfiles from your Downloads folder no matter where you are. This method is especially helpful when you're managing multiple projects or cleaning up old files scattered across different folders.
Frequently Asked Questions
Can I undo a deletion after I run the command?
No. Deleted directories and files are gone permanently — they do not go to a trash bin. The terminal does not have an undo function. Always double-check the directory name and path before pressing Enter, especially when using rm -r.
What does the error "Permission denied" mean?
You do not have permission to delete that directory. This usually happens when the directory is owned by a different user or requires administrator access. You can try sudo rm -r directoryname, but this asks the system for elevated privileges and should only be used when you're certain you own the directory.
How do I delete a directory with a very long name?
Type the first few letters of the name, then press Tab. The terminal will auto-complete the full name. This saves typing and reduces the chance of mistakes. If multiple names start the same way, press Tab again to cycle through the options.
Is there a way to delete a directory without using the terminal?
Yes. On Mac, drag the folder to the Trash. On Linux with a graphical interface, right-click the folder and select Delete or Move to Trash. On Windows, use File Explorer. The terminal is faster once you know the commands, but the graphical method is safer if you're still learning.