The Basic Command to Remove a Directory
To delete an empty directory in Linux, use the rmdir command followed by the directory name. Type rmdir directoryname and press Enter. The directory disappears when ready — Linux does not move it to a trash folder first, so the deletion is permanent.
This command only works on empty directories. If the directory contains files or other directories, rmdir will refuse and show an error message saying the directory is not empty. You must empty it first, or use a different command to remove it along with its contents.
Key Takeaways
- Use rmdir directoryname to delete an empty directory, or rm -r directoryname to delete a directory and everything inside it.
- The rm -r command is permanent and does not send files to a trash folder, so double-check the path before you run it.
- Add the -i flag (rm -ri directoryname) to make Linux ask for confirmation before deleting each file.
- Use ls directoryname or ls -la directoryname to see what is inside a directory before you delete it.
Deleting a Directory With Files Inside
If the directory contains files or subdirectories, use rm -r directoryname instead. The -r flag tells Linux to delete the directory and everything inside it, including nested folders. This is the command most people use in practice, because most directories are not empty.
Be careful with this command. Linux deletes the files when ready and permanently — there is no undo. Before you run rm -r, use ls directoryname to see what is actually inside, or use pwd to confirm you are in the right location.
Adding Confirmation Before Deletion
To make Linux ask for permission before deleting each file, add the -i flag: rm -ri directoryname. Linux will show each filename and ask "remove regular file 'filename'?" — type y and press Enter to delete it, or type n and press Enter to keep it.
This slows down the deletion process, especially in directories with many files, but it gives you a chance to stop if you realize you are deleting the wrong directory. For directories with hundreds of files, you can also type a to delete all remaining files without asking again.
Checking What Is Inside Before You Delete
Use ls directoryname to see the files and folders inside without deleting anything. This shows you the names but not the full details. If you want to see file sizes, permissions, and modification dates, use ls -la directoryname instead — the -la flags show all files including hidden ones (those starting with a dot) and display more information about each one.
If the directory has many nested folders, use tree directoryname to see the entire structure at once. The tree command shows you how deep the folders go and what is at each level. If tree is not installed on your system, you can install it with sudo apt install tree on Ubuntu or Debian, or sudo yum install tree on Red Hat or CentOS.
Removing Multiple Directories at Once
To delete several directories in one command, list them all: rm -r directory1 directory2 directory3. Linux deletes each one in order. You can also use a wildcard pattern — for example, rm -r folder* deletes all directories whose names start with "folder".
Wildcards are powerful but dangerous. Before you run a command with a wildcard, use ls folder* first to see exactly which directories match the pattern. This prevents you from accidentally deleting directories you meant to keep.
What Happens If You Do Not Have Permission
If you try to delete a directory you do not own, Linux shows an error like "Permission denied". This usually means the directory belongs to another user or requires administrator access. You can try adding sudo to the beginning: sudo rm -r directoryname. Linux will ask for your password, and if you have administrator rights, the directory will be deleted.
Do not use sudo unless you are certain you want to delete the directory. Running commands with sudo bypasses safety checks, and mistakes are harder to recover from. If you are unsure whether you have the right to delete something, ask the system administrator or the person who owns the directory.
Frequently Asked Questions
Can I undo a deletion after I run rm -r?
No. Linux does not send deleted files to a trash folder — they are removed permanently. Once you run the command, the files are gone. The only way to recover them is to restore from a backup, if one exists. Always double-check the directory path before you delete.
What is the difference between rmdir and rm -r?
rmdir only deletes empty directories and stops if it finds anything inside. rm -r deletes the directory and all its contents, including files and nested folders. Use rmdir when you know the directory is empty and want extra safety; use rm -r when you want to delete everything inside.
How do I delete a directory with a space in its name?
Put the directory name in quotes: rm -r "my directory" or rmdir "my directory". Without quotes, Linux treats "my" and "directory" as two separate things and fails. You can also use a backslash before the space: rm -r my\ directory.
What does the -f flag do in rm -rf?
The -f flag forces deletion without asking for confirmation, even if files are write-protected. Combined with -r, the command rm -rf is extremely dangerous because it deletes everything when ready with no prompts. Avoid it unless you are absolutely certain, and never use it with wildcards or as root.