The basic command to delete a file in Linux
To delete a file in Linux, use the rm command followed by the filename. Open your terminal, navigate to the directory containing the file, and type rm filename.txt (replacing "filename.txt" with your actual filename), then press Enter. The file is deleted immediately — Linux does not move deleted files to a trash folder by default, so the file is gone once you confirm the command.
If you are in a different directory than the file, you can include the full path: rm /home/username/Documents/filename.txt. This works whether the file is in your current location or somewhere else on your system.
Key Takeaways
- The rm command deletes files permanently in Linux — there is no recovery from the trash, so double-check the filename before pressing Enter.
- Use rm -i filename to get a confirmation prompt before deletion, which prevents accidental loss.
- To delete multiple files at once, list them after rm: rm file1.txt file2.txt file3.txt.
- Deleting directories requires the -r flag: rm -r directoryname, which removes the folder and everything inside it.
- Use wildcards like rm *.txt to delete all files with a certain extension, but combine this with -i to review each file first.
Using the interactive flag to prevent accidents
The safest way to delete a file is to add the -i flag, which stands for "interactive." Type rm -i filename.txt and Linux will ask you to confirm before deleting. You type y for yes or n for no. This single step prevents the most common deletion mistake — typing the wrong filename and losing a file you meant to keep.
Many experienced Linux users set up an alias so that rm automatically runs with the -i flag every time. If you want to do this, add the line alias rm='rm -i' to your .bashrc file in your home directory. After you save and close the file, open a new terminal window and rm will prompt you by default.
Deleting multiple files and using wildcards
To delete several files at once, list them after the rm command separated by spaces: rm file1.txt file2.txt file3.txt. This is faster than deleting them one at a time.
You can also use wildcards to match filenames. The asterisk * matches any characters. For example, rm *.txt deletes all files ending in .txt in the current directory. The question mark ? matches a single character, so rm file?.txt would delete file1.txt and file2.txt but not file10.txt. Always combine wildcards with -i to review what you are about to delete: rm -i *.txt.
Deleting directories and their contents
The rm command by itself cannot delete directories — it only works on files. To delete a directory and everything inside it, use the -r flag, which stands for "recursive." Type rm -r directoryname and Linux removes the folder and all files and subdirectories within it.
Because this command is powerful and permanent, always use -r with -i: rm -ri directoryname. Linux will ask you to confirm the deletion of the directory itself and then prompt you for each file inside. This takes longer but gives you a chance to stop if you realize you are deleting the wrong folder.
Checking what you are about to delete
Before running rm, use ls to list the files in your current directory and make sure you are looking at the right location. Type ls or ls -la (which shows hidden files too) and review the output. This simple step catches most mistakes before they happen.
If you are deleting a file by its full path, you can also use ls with that path first: ls /home/username/Documents/filename.txt. If the file exists, ls shows it. If the path is wrong or the file does not exist, ls tells you so, and you know not to run the rm command.
Removing files with special characters or spaces in the name
If a filename contains spaces, put the entire name in quotes: rm "my document.txt". Without quotes, Linux treats each word as a separate filename and tries to delete multiple files, which will fail or delete the wrong files.
For files with special characters like asterisks, question marks, or dollar signs, put a backslash before the character to tell Linux to treat it literally. For example, rm file\*.txt deletes a file literally named "file*.txt" rather than using the asterisk as a wildcard. Alternatively, use quotes: rm "file*.txt".
Securely overwriting files before deletion
The standard rm command deletes the filename from the directory, but the actual data on disk can sometimes be recovered with specialized tools. If you are deleting sensitive information and want to make recovery harder, use the shred command instead: shred -vfz filename.txt. The -v flag shows progress, -f forces the operation, and -z overwrites the space with zeros after shredding.
The shred command takes longer than rm because it overwrites the file multiple times. Use it for files containing passwords, financial records, or other data you want to make unrecoverable. For everyday files, rm is sufficient and much faster.
Frequently Asked Questions
Can I recover a file after I delete it with rm?
Recovery is possible but difficult and not may provide. Once you delete a file with rm, the filename disappears from the directory immediately, but the data may still exist on disk until something else overwrites that space. Recovery requires specialized tools and technical knowledge. The best prevention is to use rm -i and always confirm before deleting.
What does the -f flag do in rm?
The -f flag forces deletion without prompting, even if the file is read-only or protected. Use this only when you are certain — combining -f with wildcards like rm -f *.txt can delete many files at once without any confirmation. Most users should avoid -f and use -i instead.
How do I delete a file if I do not know its exact name?
Use wildcards with ls first to find the file. For example, ls *.txt shows all files ending in .txt, and ls file* shows all files starting with "file". Once you see the exact name, use rm -i with that name or a wildcard pattern you are confident about.
What is the difference between rm and rmdir?
The rmdir command deletes only empty directories. If a directory contains any files, rmdir fails. Use rm -r to delete a directory and its contents. If you want to delete a directory only if it is empty, rmdir is safer because it will not accidentally remove files.