The basic command to remove a folder in terminal

To delete a directory (folder) in terminal, use the rmdir command followed by the folder name. Type rmdir foldername and press Enter. This works only if the folder is completely empty — if it contains any files or subfolders, the command will fail and tell you the directory is not empty.

If your folder has files inside it, use rm -r instead. The command is rm -r foldername. The -r flag means "recursive," which tells the terminal to delete the folder and everything inside it. Be careful with this command — once you press Enter, the files are gone and cannot be recovered from the trash.

Before you delete anything, navigate to the parent folder first. If you want to delete a folder called "old_documents" that sits inside your Downloads folder, open terminal and type cd Downloads, then rm -r old_documents. This prevents mistakes and makes it clear what you are removing.

Key Takeaways

  • Use rmdir foldername to delete an empty folder, or rm -r foldername to delete a folder and all its contents.
  • The -r flag is permanent — deleted files do not go to trash and cannot be undone, so double-check the folder name before you press Enter.
  • Navigate into the parent folder first using cd so you can see exactly what you are deleting.
  • If you are unsure what is inside a folder, use ls foldername to list its contents before you delete it.

How to check what is inside a folder before deleting it

The safest approach is to look inside the folder first. Type ls foldername to see what files and subfolders it contains. If the list is empty or contains only files you recognize and want to remove, you can proceed. If you see something you want to keep, stop and move those files elsewhere first.

For a more detailed view, use ls -la foldername. This shows hidden files (files that start with a dot, like .config) that would not appear in a normal listing. Hidden files are often important system files, so seeing them before you delete can prevent accidents.

The difference between rmdir and rm -r

rmdir is the safer choice because it refuses to delete a folder unless it is completely empty. If you type rmdir myfolder and the folder contains even one file, terminal will print an error message and the folder stays intact. This built-in safety check prevents you from accidentally wiping out a folder you thought was empty.

rm -r is more powerful but also more dangerous. It deletes the folder and everything inside it without asking for confirmation. There is no error message, no second chance, and no way to recover the files afterward. Use rm -r only when you are certain the folder and its contents are something you want gone permanently.

A middle ground is to use rm -ri, which adds the -i flag for "interactive mode." This makes terminal ask you to confirm each file before deleting it. Type rm -ri foldername, and you will see a prompt for each item inside. You can say yes or no to each one, which gives you a chance to stop if you see something you want to keep.

What to do if you get a "permission denied" error

Sometimes terminal will not let you delete a folder because you do not have permission to remove it. This happens when the folder is owned by a different user or protected by the system. The error message will say "permission denied" or "operation not permitted."

First, check who owns the folder by typing ls -l foldername. This shows the owner's name. If it is your username, you may need to change the folder's permissions. Type chmod 755 foldername to give yourself write access, then try deleting it again with rm -r foldername.

If the folder is owned by another user or by "root" (the system administrator), you may need to use sudo before your delete command. Type sudo rm -r foldername and enter your password when prompted. Use sudo only when you are certain you have permission to delete the folder, because it bypasses the normal safety checks.

Deleting multiple folders at once

If you want to delete several folders in the same location, you can name them all in one command. Type rm -r folder1 folder2 folder3 to delete three folders at once. Separate each folder name with a space. Terminal will remove all of them without stopping to ask.

You can also use a wildcard to delete folders that match a pattern. For example, rm -r old_* will delete every folder whose name starts with "old_". Be very careful with wildcards — if you mistype the pattern, you might delete folders you wanted to keep. Always use ls first to see which folders match the pattern.

How to undo a deletion (if you catch it in time)

Once you delete a folder with rm, it is gone from your computer. Terminal does not send files to the trash — it removes them completely. There is no undo button and no recovery option in most cases.

Your only option is to restore from a backup if you have one. If your computer uses Time Machine (on Mac) or File History (on Windows), you may be able to recover the folder from a previous backup. Open your backup software and look for the folder in an earlier snapshot of your system. This works only if you made a backup before you deleted the folder.

The best protection is to think twice before you press Enter. Read the command out loud before you run it. Make sure the folder name is spelled correctly and that you are in the right location. A few seconds of checking can save you from losing important files.

Frequently Asked Questions

What does the dash before the r in rm -r mean?

The dash tells terminal that what follows is a flag or option that changes how the command works. -r means "recursive," which tells rm to delete the folder and everything inside it, including subfolders. Without the -r, the rm command will not delete folders at all — it only removes individual files.

Can I delete a folder that has subfolders inside it?

Yes, but only with rm -r. The -r flag works on nested folders of any depth. If you have a folder called "projects" that contains a folder called "2023" that contains a folder called "january," typing rm -r projects will delete all three levels at once.

How do I delete a folder with spaces in its name?

Put the folder name in quotes. If your folder is called "my documents," type rm -r "my documents" or rmdir "my documents". Without quotes, terminal will treat "my" and "documents" as two separate things and give you an error.

Is there a way to delete a folder without using terminal?

Yes. On Mac, drag the folder to the Trash, then empty the Trash. On Windows, right-click the folder and select Delete. Both methods send the folder to trash first, so you can recover it if you change your mind. Terminal deletion is permanent, so use the graphical method if you are not completely sure.