The quickest way to delete a folder in Linux
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 structure is straightforward: type rmdir foldername to remove an empty folder, or rm -r foldername to remove a folder and everything inside it.
The difference between these two commands matters. rmdir will refuse to delete a folder if it contains anything, which acts as a safety check. rm -r will delete the folder and all its contents without asking for confirmation, so you need to be certain before you run it.
Both commands work in the terminal. You need to navigate to the parent directory first — the folder that contains the folder you want to delete — or provide the full path to the folder.
Key Takeaways
- Use rmdir foldername to delete an empty folder, and the command will fail if the folder contains any files.
- Use rm -r foldername to delete a folder and everything inside it, and the deletion happens immediately without a confirmation prompt.
- Navigate to the parent directory first with cd, or provide the full path to the folder you want to delete.
- Add the -i flag to rm -r (as in rm -ri foldername) to get a confirmation prompt before each file is deleted.
Deleting an empty folder with rmdir
The rmdir command removes directories that contain nothing. Open your terminal and navigate to the folder that contains the folder you want to delete. If you want to delete a folder called old_files that sits in your home directory, type cd ~ to go to your home directory first.
Then type rmdir old_files and press Enter. If the folder is empty, it will be deleted and you will return to the command prompt. If the folder contains any files or subfolders, you will see an error message saying the directory is not empty, and the folder will remain.
This safety feature prevents accidental deletion of folders you thought were empty. If rmdir refuses to delete the folder, you know there is still something inside it that you need to deal with first.
Deleting a folder with files using rm -r
The rm command with the -r flag (which stands for recursive) deletes a folder and everything inside it — all files, subfolders, and their contents. Type rm -r foldername and press Enter. The folder and all its contents will be deleted immediately.
There is no confirmation step and no trash or recycle bin — the deletion is permanent. For this reason, double-check the folder name before you press Enter. A common mistake is typing rm -r * by accident, which would delete everything in your current directory.
If you want a safety prompt before deletion, use rm -ri foldername instead. The -i flag stands for interactive and will ask you to confirm the deletion of each file and the folder itself. This slows down the process but gives you a chance to stop if you realize you are deleting the wrong thing.
Using the full path instead of navigating first
You do not have to navigate to the parent directory. You can delete a folder from anywhere by providing its full path. For example, if you want to delete a folder at /home/username/documents/old_files, type rm -r /home/username/documents/old_files from any location in the terminal.
The full path starts with a forward slash and lists each folder in order. You can find the full path of a folder by opening a file manager, right-clicking the folder, and looking for a properties or details option. Some file managers display the path in the address bar at the top.
Using the full path is useful when you are working in a different directory and do not want to navigate back and forth. It also reduces the chance of deleting the wrong folder because you can see the entire location on screen.
Checking what is inside a folder before you delete it
Before you use rm -r, you can look inside the folder to see what you are about to delete. Use the ls command to list the contents. Type ls foldername to see what is in the folder, or ls -la foldername to see hidden files as well.
If the folder contains subfolders with their own files, type ls -R foldername to see everything inside, including nested folders. The -R flag stands for recursive and shows the entire folder structure. This gives you a complete picture before you delete.
Taking a moment to check the contents can prevent the mistake of deleting a folder you did not realize contained important files. Once you are certain, you can proceed with rm -r.
Deleting multiple folders at once
You can delete more than one folder in a single command. Type rm -r folder1 folder2 folder3 to delete three folders at once. Separate each folder name with a space. This works with both folder names and full paths.
You can also use a wildcard pattern to delete folders that match a name. For example, rm -r temp_* will delete all folders that start with temp_. Be very careful with wildcards — make sure you understand exactly which folders will match the pattern before you run the command.
If you use rm -ri with multiple folders, you will be asked to confirm each one individually, which gives you a chance to skip folders you want to keep.
What happens if you delete a folder by mistake
Unlike deleting a folder in a graphical file manager, folders deleted with rm or rmdir do not go to a trash or recycle bin. They are gone immediately and permanently. Standard Linux commands do not have a built-in undo feature.
Some Linux systems have recovery tools that can sometimes restore deleted files if the disk space has not been overwritten, but recovery is not may provide and requires technical knowledge. The best protection is to use rm -ri for important folders, which forces you to confirm each deletion, or to keep backups of folders you cannot afford to lose.
If you delete a folder and realize immediately that it was a mistake, stop using the computer and contact a data recovery specialist. The longer the system runs after deletion, the less likely recovery becomes.
Frequently Asked Questions
What is the difference between rm and rmdir?
rmdir only deletes empty folders and refuses to delete anything that contains files or subfolders. rm -r deletes a folder and everything inside it without checking if it is empty. Use rmdir when you want a safety check, and rm -r when you need to delete a folder that contains files.
Can I undo a deletion after I run rm -r?
No. Folders deleted with rm -r are permanently removed and do not go to a trash folder. They cannot be recovered through normal Linux commands. If you delete something important, stop using the computer immediately and contact a data recovery service, though recovery is not may provide.
How do I delete a folder with a space in its name?
Put the folder name in quotes or use a backslash before the space. For example, rm -r "my folder" or rm -r my\ folder. Without quotes or a backslash, the terminal treats the space as a separator and tries to delete two separate things.
What does the -i flag do in rm -ri?
The -i flag makes rm ask for confirmation before deleting each file and the folder itself. This slows down the deletion but gives you a chance to stop if you realize you are deleting the wrong thing. Type y to confirm each deletion or n to skip it.
Can I delete a folder that I do not have permission to access?
No. If you do not own the folder or have write permission in the parent directory, rm and rmdir will fail with a permission denied error. You may need to use sudo to run the command as an administrator, but this should only be done if you are certain about what you are deleting.