Delete Files & Folders in Linux Terminal
In this article
Deleting files and folders from the Linux command line is a core skill for anyone working with a server, a headless system, or just preferring the speed of the terminal. The rm (remove) command is the primary tool, and with the right flags it can handle single files, multiple files, and entire directory trees. This guide covers the essential commands, safe usage patterns, and how to avoid the most common pitfalls.
What You'll Need
- A Linux system with a terminal emulator (or SSH access to a remote machine).
- A user account with write permission on the files or folders you intend to delete. For system files or directories owned by root, you may need
sudo. - Basic familiarity with navigating the command line (
cd,ls,pwd).
Step-by-Step
- Open a terminal window. On most desktop Linux distributions, you can press
Ctrl+Alt+Tor search for "Terminal" in the applications menu. If you're on a remote server, connect via SSH. - Navigate to the directory containing the file or folder. Use the
cdcommand. For example, to go to your Documents folder:cd ~/Documents. Always confirm your current location withpwdbefore deleting anything. - List the contents to verify what you're about to delete. Run
ls -lato see all files, including hidden ones (those starting with a dot), along with permissions and sizes. This is your last chance to double-check. - Delete a single file. The basic syntax is
rm filename. For example:rm notes.txt. This permanently removes the file without sending it to a trash folder. - Delete multiple files at once. You can list them all in one command:
rm file1.txt file2.txt file3.txt. You can also use wildcards to match patterns, likerm *.logto delete all files ending in.login the current directory. Be extremely careful with wildcards—they can match more than you expect. - Delete an empty directory. Use the
rmdircommand:rmdir empty_folder. This only works if the directory is completely empty; otherwise it will fail with an error. - Delete a directory and its contents recursively. Use
rm -r foldername(orrm -R). The-rflag tellsrmto go through the directory recursively, deleting everything inside it. For example:rm -r old_project. - Add the force flag for problem files. Some files may be write-protected or have restrictive permissions. Adding
-f(force) will override that and remove them without prompting. Combine it with recursive:rm -rf foldername. This is the most powerful and dangerous form—use it only when you are absolutely certain. - Use interactive mode for safety. Add the
-iflag to be prompted before each deletion:rm -i file.txtorrm -ri folder. This is a good habit when you're unsure about the exact contents. - Verify the deletion. Run
lsagain to confirm the file or folder is gone. If you deleted a directory,ls -lashould show its name no longer exists.
Common Mistakes
- Using
rm -rfwithout double-checking the path. A single typo, like a space or a missing slash, can turnrm -rf ~/my filesinto a command that deletes your home directory and a folder named "files". - Using wildcards incorrectly. For example,
rm *.txtdeletes all files ending in.txt, butrm * .txt(with a space) deletes everything in the current directory plus a file named.txt. - Forgetting that
rmis permanent. There is no trash bin in the command line. Once deleted, the data is gone unless you have a backup. There is noundocommand. - Using
rmdiron a non-empty directory. It will fail and you might think the deletion didn't work, when in fact you just used the wrong command. - Deleting files with spaces in their names without quoting. For a file named
my file.txt, you must userm "my file.txt"or escape the space with a backslash:rm my\ file.txt.
Tips
- Always use
-iwhen you're learning. It forces you to confirm each deletion, which prevents accidents. - Use
lswith the same wildcard before deleting. For example, runls *.tmpfirst to see exactly which files match, then runrm *.tmp. - Create an alias for safer deletion. You can add
alias rm='rm -i'to your~/.bashrcor~/.zshrcfile. This makes interactive mode the default for your shell session. - Consider using a trash command. Tools like
trash-cliprovide atrashcommand that moves files to a recycle bin, giving you a safety net. Install it with your package manager (e.g.,sudo apt install trash-clion Debian/Ubuntu). - For important deletions, use
rm -iorrm -I. The uppercase-Iprompts only once before deleting more than three files or when deleting recursively—less annoying but still safe. - Back up critical data regularly. Even with all precautions, accidents happen. A simple
tarorrsyncbackup can save you.
Troubleshooting
- "Permission denied" error. This means your user doesn't have write permission on the file or the directory containing it. Check the owner and permissions with
ls -l. If you need to delete it, you can usesudo rm filename(if you have sudo privileges), but be absolutely sure you want to delete it. - "Directory not empty" error with
rmdir. The directory contains hidden files or subdirectories. Usels -lato see them, then either delete them individually or userm -rinstead. - File is busy or in use. If you get an error like "Text file busy" or "Device or resource busy", the file is being used by a running process. You can try closing the application or using
lsof filenameto find the process ID and then kill it, but be careful. - You accidentally deleted the wrong file. Immediately stop writing to the disk. If you have a backup, restore it. If not, you might try data recovery tools like
testdiskorphotorec, but success is not guaranteed, especially on SSDs with TRIM enabled. - The command seems to hang. If you're using
rm -ron a huge directory, it can take a while. You can add the-v(verbose) flag to see progress:rm -rv folder.
Frequently Asked Questions
Can I recover a file deleted with rm? Generally, no. rm unlinks the file immediately, and the space is marked as available. Unless you have a backup or use specialized recovery software quickly, the data is likely lost.
What's the difference between rm and unlink? unlink is a lower-level command that deletes a single file and nothing else. It's rarely used. rm is the standard tool with more features like recursive deletion and wildcard support.
Is there a way to send files to trash instead of permanent deletion? Yes, install trash-cli and use the trash command. It moves files to the desktop trash, which you can restore later.
How do I delete a file that starts with a dash, like -file.txt? Use rm -- -file.txt. The double dash -- tells rm to stop interpreting options and treat everything after as a filename.
What does rm -rf / do? It attempts to delete everything on the root filesystem. On modern systems, it may be blocked by protections, but if it runs, it will destroy your operating system. Never run this command.
Can I use rm to delete files matching a pattern but keep some? Yes, use extended globbing or a loop. For example, to delete all .txt files except keep.txt: rm !(keep.txt) after enabling shopt -s extglob in bash. Alternatively, use find with -not -name.