The basic command to remove a directory

To delete an empty directory, type rmdir followed by the directory name, then press Enter. For example, rmdir Documents/old_files removes a directory called old_files inside Documents. The directory must be empty — if it contains files or other directories, rmdir will refuse and show an error message.

If the directory has files or folders inside it, use rm -r instead. The -r flag means "recursive," which tells the terminal to delete the directory and everything in it. Type rm -r Documents/old_files and press Enter. The terminal will not ask for confirmation — the directory and all its contents vanish when ready.

Before you use rm -r, make sure you actually want to delete what is inside. There is no trash bin or undo button in the terminal. Once you press Enter, the files are gone.

Key Takeaways

  • Use rmdir to delete an empty directory; the command will fail if anything is inside it.
  • Use rm -r to delete a directory and everything in it at once, with no confirmation prompt.
  • Terminal deletion is permanent — deleted files do not go to trash and cannot be recovered through normal means.
  • You can delete multiple directories in one command by listing them: rm -r folder1 folder2 folder3.
  • If you get a "permission denied" error, the directory may belong to another user or require administrator access.

Checking what is inside before you delete

The safest habit is to look inside a directory before you delete it. Type ls Documents/old_files to list what is in that directory. If the list is long or you want to see hidden files too, type ls -la Documents/old_files. The -la flags show all files, including ones that start with a dot (which are normally hidden).

If you see files you want to keep, move them to a different directory first. Type mv Documents/old_files/important.txt Documents/ to move important.txt out of old_files and into Documents. Once you have moved everything you need, then delete the empty directory with rmdir or the full directory with rm -r.

Deleting directories nested inside other directories

When a directory is inside another directory, you can delete it by typing the full path. For example, rm -r Documents/Projects/2023/archive deletes the archive directory even if you are currently in your home folder. You do not have to navigate into Documents first.

You can also delete a directory and all its parent directories at once if they are empty. Type rm -r Documents/Projects/2023/archive/ and if archive is empty, rmdir will fail — but rm -r will delete it. If you then want to remove the now-empty 2023 folder, you would need to run the command again with a shorter path: rmdir Documents/Projects/2023.

What happens if you do not have permission

If you see "permission denied" when you try to delete a directory, it means the directory belongs to a different user or has restricted permissions. On Mac or Linux, you can try adding sudo before the command: sudo rm -r Documents/old_files. The system will ask for your password. If you enter it correctly, the directory will be deleted.

On Windows using PowerShell, permission errors are less common, but if you encounter one, try running PowerShell as Administrator. Right-click the PowerShell icon and select "Run as Administrator," then run your rm command again.

Do not use sudo or Administrator mode unless you are sure you own the directory or have permission to delete it. Using elevated permissions on the wrong directory can damage your system.

Deleting multiple directories at once

You can delete several directories in a single command by listing them with spaces between. Type rm -r folder1 folder2 folder3 to delete all three. The terminal will remove each one in order without pausing between them.

You can also use a wildcard pattern if the directories follow a naming pattern. For example, rm -r Documents/backup_* deletes every directory in Documents that starts with "backup_" — like backup_2023, backup_2024, and backup_old. Be very careful with wildcards, because they can match more than you expect. Test with ls Documents/backup_* first to see exactly which directories will be deleted.

Recovering a deleted directory

Once you delete a directory with rm or rmdir, it is gone from the terminal's perspective. Most operating systems do not have a built-in recovery tool for terminal deletions. However, if your computer has Time Machine (Mac), System Restore (Windows), or another backup system running, you may be able to recover the directory from a backup.

Check your backup software to see if it has a copy of the directory from before you deleted it. On Mac, open Time Machine from the menu bar and navigate to the date before the deletion. On Windows, right-click the parent folder, select "Restore previous versions," and choose a version from before the deletion. These methods work only if backups were running at the time.

Common mistakes and how to avoid them

The most common mistake is using rm -r / or rm -r ~ by accident. The first deletes your entire system starting from the root directory. The second deletes your entire home directory. Always double-check your path before pressing Enter, especially when using rm -r.

Another mistake is assuming rmdir will delete a directory with files in it. It will not — it will show an error and leave the directory untouched. If you see "directory not empty," use rm -r instead, not rmdir again.

A third mistake is deleting a directory you are currently inside. If you are in Documents/old_files and you type rm -r old_files, the terminal may behave unexpectedly. Navigate out of the directory first — type cd .. to go up one level — then delete it.

Frequently Asked Questions

Can I delete a directory if it has files I do not recognize?

Yes, but look at the file names and sizes first using ls -lah to see how much space they take up. If you do not recognize them and they are not important, rm -r will delete them. If you are not sure, move the directory to a temporary location instead of deleting it, so you can recover it later if needed.

What is the difference between rm and rmdir?

rmdir only deletes empty directories and stops if it finds anything inside. rm -r deletes directories and everything in them without asking. Use rmdir when you want the terminal to stop you if the directory is not empty. Use rm -r when you are sure you want to delete everything.

Does deleting a directory in terminal delete it from the trash too?

No. Terminal deletion bypasses the trash entirely — the directory is removed directly from your hard drive. It never appears in your trash or recycle bin, so you cannot recover it that way.

Can I delete a directory if another program is using it?

On Mac and Linux, you can usually delete a directory even if a program is using files inside it, though the program may crash or behave strangely. On Windows, you may get a "file in use" error. Close the program first, then try deleting the directory again.

What does the hyphen mean in rm -r?

The hyphen tells the terminal that what follows is a flag or option, not a file name. The -r flag modifies how rm behaves. Other common flags are -f (force deletion without prompting) and -i (ask before deleting each item). You can combine them: rm -rf forces recursive deletion.