How to Remove Files and Directories in Linux
In this article
To remove files and directories in Linux, use the rm command for files and rmdir or rm -r for directories. The simplest way is rm filename to delete a file, and rm -r directory to delete a folder and everything inside it. This guide covers the exact commands, safety checks, and what to do when something goes wrong.
What You'll Need
- A Linux terminal (command line) — open it with
Ctrl+Alt+Ton most desktop distributions. - Basic familiarity with navigating directories using
cdand listing files withls. - Appropriate permissions: you need write permission on the directory containing the file, and if removing a directory, write permission on its parent.
Step-by-Step
- Open a terminal. Press
Ctrl+Alt+Tor search for “Terminal” in your applications menu. - Navigate to the location of the file or directory you want to remove. Use
cdto change directories. For example:cd /home/username/Documents. - List the contents to confirm the exact name of the item. Run
lsto see files and folders in the current directory. Pay attention to spaces and special characters — you may need to quote the name, e.g.,rm "my file.txt". - Remove a single file with
rm filename. For example:rm notes.txt. This permanently deletes the file. - Remove multiple files by listing them:
rm file1.txt file2.txt file3.txt. - Remove an empty directory with
rmdir directoryname. This only works if the directory is completely empty. - Remove a directory and its contents (non-empty) with
rm -r directoryname. The-rstands for recursive, meaning it deletes the folder and everything inside it. - Add the
-fflag to force removal without prompts, but use it carefully:rm -rf directoryname. This bypasses confirmation and ignores nonexistent files. - Use
-ifor interactive confirmation if you want to be prompted before each deletion:rm -i filename. This is safer for beginners. - Verify the removal by running
lsagain to confirm the item is gone.
Common Mistakes
- Using
rmdiron a non-empty directory — it will fail with “Directory not empty”. Userm -rinstead. - Forgetting the
-rflag when deleting a directory —rm directoryreturns an error like “cannot remove 'directory': Is a directory”. - Using
rm -rfwith a typo — for example,rm -rf / home(with a space) could delete your home directory. Always double-check the path. - Deleting files permanently — there is no trash bin for
rm; once deleted, the data is gone unless you have a backup. - Ignoring special characters — filenames with spaces or symbols need quotes or escaping, otherwise the command may fail or delete the wrong file.
Tips
- Use
-iwhen you're unsure — it asks for confirmation before each deletion, reducing accidental loss. - Create a backup of important files before deleting, especially when using
-ror-f. - Use tab completion to avoid typos: type the first few letters of the filename and press
Tabto autocomplete. - Check your current directory with
pwdbefore runningrm -rf .— that command deletes the current directory and everything in it. - Consider using a safer alternative like
trash(if installed) to move files to a trash folder instead of permanent deletion.
Troubleshooting
If the command fails or you accidentally delete the wrong thing, here's what to do:
- “Permission denied” error — you don't have write permission on the directory. Try adding
sudobefore the command (e.g.,sudo rm filename), but only if you own the system and understand the risk. Alternatively, change permissions withchmodif you own the file. - “Directory not empty” — the directory contains hidden files (starting with a dot). Use
ls -ato see them, then delete them individually or userm -r. - Accidentally deleted a file — if the file is on a filesystem that supports snapshots or if you have a backup, restore from there. Otherwise, stop using the disk immediately and try recovery tools like
extundeleteorphotorec, but success is not guaranteed. - Command not found —
rmis part of coreutils and should be present on any Linux system. If not, reinstall coreutils via your package manager. - File name starts with a dash — for example, a file named
-file. Userm -- -fileto tellrmthat the dash is not an option.
Frequently Asked Questions
Can I recover a file deleted with rm?
Generally no, because rm doesn't send files to a trash folder. If you have a backup or a snapshot, restore from there. Otherwise, data recovery tools might work if you act quickly and the disk hasn't been overwritten.
What's the difference between rmdir and rm -r?
rmdir only removes empty directories. rm -r removes directories and all their contents recursively, including files and subdirectories.
Is rm -rf dangerous?
Yes, because it force-deletes without confirmation and recursively. A typo or wrong path can wipe out important data. Always double-check the path and consider using -i instead.
How do I delete a file with a space in its name?
Quote the filename: rm "my file.txt" or escape the space: rm my\ file.txt.
Can I delete a directory that is not empty without using rm -r?
No, you must use rm -r or first delete all contents manually, which is tedious. rm -r is the standard way.
What does the -f flag do exactly?
It stands for “force” and tells rm to ignore nonexistent files and never prompt for confirmation. It also overrides write permissions in some cases, so use it with caution.