The basic command to remove a folder

To delete a folder in Linux, use the rmdir command for empty folders or the rm command with the -r flag for folders that contain files. The command you choose depends on whether the folder is empty.

For an empty folder, type rmdir foldername and press Enter. The folder disappears when ready. If the folder contains files or other folders, rmdir will refuse to delete it and show an error message instead.

For a folder with contents, type rm -r foldername and press Enter. The -r flag tells Linux to delete the folder and everything inside it, including subfolders and all their files. This action cannot be undone, so double-check the folder name before you press Enter.

Key Takeaways

  • Use rmdir foldername to delete an empty folder, and Linux will refuse if anything is inside.
  • Use rm -r foldername to delete a folder and all its contents at once, including subfolders and files.
  • Add -i to the rm command to have Linux ask for confirmation before deleting each item, which prevents accidental loss.
  • The full path to a folder (like /home/username/Documents/oldfolder) works the same way as a folder name in your current directory.
  • Deleted folders do not go to a trash bin — they are removed permanently, so verify the folder name before executing the command.

Deleting a folder with files inside

When you use rm -r, Linux removes the folder and everything nested inside without stopping to ask. This includes documents, images, configuration files, and any subfolders with their own contents. The deletion happens in seconds, and there is no recovery option built into the command itself.

If you want Linux to pause and ask before deleting each item, use rm -ri foldername instead. The -i flag stands for "interactive" and makes Linux print a question for each file and subfolder. You type y to delete or n to skip. This slows the process but gives you a chance to stop if you see something you did not mean to remove.

Another option is rm -r foldername/ with a trailing slash. Some systems treat this slightly differently, but the result is the same: the folder and its contents are deleted. The trailing slash is optional and does not change the behavior on most modern Linux distributions.

Using the full path to delete a folder

You do not have to be inside a folder's parent directory to delete it. You can type the full path from anywhere in the terminal. For example, if you are in your home directory and want to delete a folder at /home/username/Documents/oldfolder, type rm -r /home/username/Documents/oldfolder and press Enter.

The full path starts from the root of the system (the forward slash at the beginning) or from your home directory (using the tilde ~ as shorthand). So rm -r ~/Documents/oldfolder does the same thing as the full path above, but is shorter to type.

This approach is useful when you are working in a different part of the file system and do not want to navigate back to the folder's location. It also makes it easier to see exactly which folder you are deleting, because the full path is right there in the command.

Protecting yourself from accidental deletion

The most common mistake is typing the wrong folder name or using rm -r on a folder you did not mean to delete. Linux does not move deleted folders to a trash bin — they are gone permanently. A few habits reduce the risk.

First, list the folder's contents before you delete it. Type ls foldername to see what is inside. If the output looks wrong, stop and check the folder name. Second, use rm -ri instead of rm -r when you are deleting something important. The interactive mode forces you to confirm each deletion, which catches mistakes before they happen.

Third, consider using mv to move a folder to a temporary location instead of deleting it when ready. Type mv foldername /tmp/foldername to move it to the system's temporary folder. If you realize later that you needed it, you can move it back. After a few days or weeks, the system usually clears out the /tmp folder automatically.

What happens when a folder is in use

If a program is actively using a file inside the folder you are trying to delete, rm -r will still delete the folder and the file. The program may crash or behave unexpectedly because the file it was reading or writing to no longer exists. Linux does not prevent you from deleting files that are open.

Before deleting a folder, close any programs that might be using files inside it. If you are unsure, type lsof foldername to see which processes have files open in that folder. The output shows the program name and process ID. If nothing appears, the folder is safe to delete.

Removing folders with special characters in the name

Folder names with spaces, quotes, or other special characters need careful handling. If a folder is named my old folder (with spaces), typing rm -r my old folder will not work because Linux treats each word as a separate argument. Instead, wrap the name in quotes: rm -r "my old folder" or escape the spaces with backslashes: rm -r my\ old\ folder.

Folder names with asterisks, question marks, or dollar signs can be misinterpreted by the shell. Quotes are the safest approach. For example, rm -r "folder*name" deletes a folder literally named folder*name, not all folders that start with "folder".

If you are unsure about the exact name, use tab completion. Type rm -r, then the first few letters of the folder name, then press Tab. Linux will fill in the rest of the name and handle special characters automatically.

Frequently Asked Questions

Can I recover a folder after I delete it with rm -r?

No. The rm command removes files and folders permanently. They do not go to a trash bin or recycle folder. If you deleted something important, your only option is to restore from a backup. Some systems have file recovery tools, but they work only if the disk space has not been overwritten yet, and recovery is not may provide.

What is the difference between rmdir and rm -r?

rmdir deletes only empty folders and refuses if anything is inside. rm -r deletes a folder and all its contents in one command. Use rmdir when you want Linux to stop you if the folder is not empty. Use rm -r when you want to delete everything at once.

How do I delete a folder that starts with a dash or hyphen?

A folder named -oldfolder can confuse the command because Linux thinks the dash is a flag. Type rm -r -- -oldfolder instead. The double dash tells Linux to stop looking for flags and treat everything after it as a folder name.

Can I delete multiple folders at once?

Yes. Type rm -r folder1 folder2 folder3 to delete three folders and their contents in one command. You can also use wildcards: rm -r folder* deletes all folders that start with "folder". Be very careful with wildcards because they can match more folders than you intended.

What does the -f flag do in rm -rf?

The -f flag means "force" and tells Linux to delete without asking for confirmation, even if files are write-protected. rm -rf is the fastest way to delete but also the most dangerous because it gives you no chance to stop. Avoid it unless you are certain about what you are deleting.