The Basic Command to Remove a Directory

To delete an empty directory in terminal, use the rmdir command followed by the directory name. Type rmdir directoryname and press Enter. The directory disappears immediately — there is no trash or undo, so make sure you want it gone before you press Enter.

If the directory contains files or other directories inside it, rmdir will refuse to delete it and show an error message. This safety feature prevents you from accidentally erasing files you meant to keep. You have two choices: delete the contents first, or use a different command that removes everything at once.

Key Takeaways

  • Use rmdir directoryname to delete an empty directory, and the command will refuse if anything is inside it.
  • Use rm -r directoryname to delete a directory and everything in it, but this action cannot be undone.
  • The -i flag (as in rm -ri directoryname) makes the terminal ask you to confirm before deleting each item, which slows the process but prevents accidents.
  • Always double-check the directory name and path before you press Enter, because deleted files do not go to a trash folder.
  • Navigate to the parent directory first — the folder that contains the directory you want to delete — so you can see what you are about to remove.

Deleting a Directory With Files Inside It

When a directory contains files or subdirectories, rmdir stops and tells you it is not empty. At that point, use the rm command with the -r flag instead: rm -r directoryname. The -r stands for "recursive," which means the command deletes the directory, everything inside it, and everything inside those things, all the way down.

This is powerful and fast, but it is also permanent. Once you press Enter, the directory and all its contents are gone. There is no recovery unless you have a backup. If you are not certain what is inside the directory, navigate to it first and use ls to list what you are about to delete.

How to Check What Is Inside Before You Delete

Before you delete anything, use ls directoryname to see what the directory contains. If it is empty, you will see nothing printed back. If it has files or folders, they will be listed on the screen.

For a more detailed view, use ls -la directoryname. This shows hidden files (files that start with a dot, like .config), file sizes, and permissions. Hidden files are real files that take up space and will be deleted along with everything else, so it is worth checking for them before you use rm -r.

Using the Confirmation Flag to Prevent Accidents

If you are deleting a directory with many files and you want a safety net, add the -i flag: rm -ri directoryname. This makes the terminal ask you to confirm each deletion by typing y (yes) or n (no) before it removes each file and folder.

This approach takes longer — you might answer dozens of prompts — but it gives you a chance to stop if you see something you did not expect. You can also press Ctrl+C at any time to cancel the entire operation and leave the remaining files untouched.

The Difference Between rmdir and rm -r

The rmdir command is the safest choice when you know a directory is empty. It will refuse to delete anything that contains files or folders, which means you cannot accidentally erase data you wanted to keep. Use it as a first step if you are unsure whether a directory is truly empty.

The rm -r command is faster when you want to delete a directory and all its contents in one step. It does not ask questions and does not check — it simply removes everything. The rm -ri variant adds confirmation prompts for each item, giving you a chance to review what is being deleted before it happens. Choose based on how confident you are about what you are removing.

What Happens If You Delete the Wrong Directory

Deleted files in terminal do not go to a trash folder or recycle bin. Once the command finishes, the directory is gone. If you have a recent backup, you can restore from it. If you do not, recovery is difficult and may require specialized tools or professional help.

The best protection is to be careful before you press Enter. Use pwd to confirm which directory you are currently in, use ls to see what you are about to delete, and type the directory name slowly so you do not make a typo. If you are new to terminal, practice on a test directory with files you do not care about losing.

Frequently Asked Questions

Can I delete a directory if I do not have permission?

No. If you do not own the directory or do not have write permission on the parent folder, terminal will show a "Permission denied" error. You may need to use sudo (which runs the command as an administrator), but only do this if you are certain you should have access. Incorrect use of sudo can damage your system.

What does the error "Directory not empty" mean?

It means rmdir found files or folders inside the directory you tried to delete. Use ls directoryname to see what is there, then either delete those items first or use rm -r directoryname to remove everything at once.

How do I delete a directory with a space in its name?

Put the directory name in quotes: rm -r "directory name". Without quotes, terminal treats each word as a separate thing and gets confused. Alternatively, you can use a backslash before the space: rm -r directory\ name.

Is there a way to undo a deletion?

Not in terminal itself. Once a directory is deleted, it is gone unless you have a backup. Some systems keep version history or snapshots, but this depends on your setup. The safest approach is to back up important directories before you delete anything.