The rm command removes files permanently
To delete a file in Linux, you use the rm command followed by the filename. Open your terminal, navigate to the folder 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.
The rm command works the same way whether you are using Ubuntu, Fedora, CentOS, Debian, or any other Linux distribution. You need write permission in the folder containing the file, but you do not need to own the file itself to delete it if you have the right folder permissions.
Key Takeaways
- The rm command deletes files permanently without moving them to trash, so double-check the filename before you press Enter.
- Use rm -i filename to get a confirmation prompt before deletion, which prevents accidental removal.
- The rm -r flag deletes folders and everything inside them, and it works even if the folder is not empty.
- Deleted files cannot be recovered through normal means, so backups are your only safety net if you delete something you need later.
Using the interactive flag to prevent accidents
If you want Linux to ask you to confirm before deleting, use rm -i filename.txt. The -i flag stands for "interactive" and forces the system to display a prompt asking "remove regular file 'filename.txt'?" You then type y and press Enter to confirm, or type n and press Enter to cancel.
This is the safest way to delete files when you are still learning Linux, because it gives you a moment to reconsider. Many experienced users set up an alias so that rm always runs with the -i flag by default, making it harder to delete something by accident.
Deleting multiple files at once
You can delete several files in a single command by listing them all. Type rm file1.txt file2.txt file3.txt and press Enter to remove all three at once. You can also use a wildcard pattern: rm *.txt deletes every file ending in .txt in the current folder.
Wildcards are powerful but dangerous. Before you run a command with a wildcard, use ls *.txt first to see exactly which files match the pattern. This shows you what will be deleted before you actually delete it. Combine the wildcard with the interactive flag for extra safety: rm -i *.txt will ask you to confirm each file one at a time.
Removing folders and everything inside them
To delete a folder, use rm -r foldername. The -r flag stands for "recursive" and tells rm to delete the folder itself plus all files and subfolders inside it. Without the -r flag, rm will refuse to delete a folder and show an error message instead.
The -r flag is one of the most dangerous commands in Linux because it can delete large amounts of data in seconds. Always use rm -ri foldername to get a confirmation prompt for each item being deleted. If you are deleting a folder you created yourself and you are certain of its contents, rm -r alone is fine, but the interactive version costs nothing and has saved countless users from mistakes.
Checking what you are about to delete
Before you run any rm command, use ls to list the files in your current folder. Type ls -la to see all files including hidden ones (files starting with a dot). This shows you the exact names and sizes of files so you can be certain you are deleting the right thing.
You can also use pwd to print your current working directory, which tells you exactly which folder you are in. Many deletion mistakes happen because a user thinks they are in one folder but they are actually in another. Knowing your location before you delete anything is the simplest way to avoid accidents.
Understanding file permissions and ownership
You can only delete a file if you have write permission in the folder containing it. If you try to delete a file and see "Permission denied," it means the folder's permissions prevent you from making changes. You can check permissions by typing ls -l, which shows a string of letters and dashes at the start of each line.
If you own the folder, you can change its permissions using the chmod command, but that is a separate topic. If you do not own the folder, you will need to ask the owner or a system administrator to delete the file or give you permission to do so. Trying to delete a file you do not have permission to remove will not harm anything — Linux simply refuses the command.
What happens after you delete a file
Once you press Enter after an rm command, the file is deleted. Linux does not send files to a recycle bin or trash folder the way Windows and macOS do. The space the file occupied on disk is marked as available for new data, but the original file content may still be recoverable using specialized tools if the space has not been overwritten yet.
For most purposes, a deleted file is gone forever. If you regularly delete files you might need later, set up a backup system or use a version control tool like Git. If you have deleted something important and need to recover it, stop using the computer immediately and contact a data recovery service, because every file you create or modify after the deletion makes recovery less likely.
Frequently Asked Questions
Can I undo a deletion in Linux?
No. Once you delete a file with rm, it is gone. Linux does not have an undo command for the terminal. If you have a recent backup, you can restore the file from that backup. If you do not have a backup, data recovery services may be able to recover the file, but this is expensive and not always successful.
What is the difference between rm and rmdir?
The rmdir command deletes only empty folders. If a folder contains any files, rmdir refuses to delete it and shows an error. The rm -r command deletes folders whether they are empty or not. Use rmdir when you want extra protection against accidentally deleting a folder that still contains files you need.
How do I delete a file with a space in its name?
Put the filename in quotes: rm "my file.txt". Without quotes, Linux treats "my" and "file.txt" as two separate things and shows an error. You can also use a backslash before the space: rm my\ file.txt. Both methods work equally well.
What does the -f flag do in rm?
The -f flag stands for "force" and tells rm to delete files without asking for confirmation, even if the file is read-only. Avoid using -f unless you are certain you want to delete something, because it removes the safety net that interactive mode provides. Never combine -f and -r unless you are absolutely sure.
Can I delete a file I do not own?
Only if you have write permission in the folder containing it. Ownership of the file itself does not matter — what matters is whether you can modify the folder. If you cannot delete a file you do not own, ask the file owner or a system administrator to delete it or change the folder permissions.