The basic command to delete a file in Linux is rm, followed by the filename
To delete a single file, open a terminal and type rm filename.txt, then press Enter. The file disappears when ready — Linux does not move deleted files to a trash folder by default, so the file is gone as soon as you run the command. There is no undo button.
If the file is in a different folder, include the path: rm /home/username/Documents/filename.txt or rm ~/Documents/filename.txt (the tilde ~ is shorthand for your home folder). If you are already in that folder, just the filename works.
Key Takeaways
- The rm command deletes files permanently with no recovery option, so double-check the filename before you press Enter.
- Use rm -i filename to get a confirmation prompt before deletion, which prevents accidents.
- Delete multiple files at once with rm file1.txt file2.txt file3.txt or use wildcards like rm *.txt to delete all files of one type.
- Remove entire folders and everything inside them with rm -r foldername, but use rm -ri foldername to confirm each deletion first.
- The shred command overwrites a file before deleting it, making recovery much harder, though it takes longer than rm.
How to delete a file safely with a confirmation prompt
Because rm deletes without asking, add the -i flag to get a confirmation message: rm -i filename.txt. Linux will ask "remove regular file 'filename.txt'?" and you type y for yes or n for no, then press Enter.
This one extra step catches most mistakes. If you often delete files, you can set up an alias so rm always asks for confirmation. In your terminal, type alias rm='rm -i' and press Enter. From that point forward in that terminal session, every rm command will prompt you. To make it permanent across all future sessions, add that same line to your .bashrc file in your home folder (open it with a text editor like nano: nano ~/.bashrc, add the line at the end, save with Ctrl+X, then Y, then Enter).
Deleting multiple files at once
To delete several files in one command, list them all: rm file1.txt file2.txt file3.txt. This is faster than running rm three separate times.
You can also use wildcards to delete groups of files. The asterisk * matches any characters. For example, rm *.txt deletes every file ending in .txt in the current folder. rm photo_*.jpg deletes every .jpg file that starts with "photo_". Be very careful with wildcards — rm * deletes everything in the current folder, which is why the confirmation flag rm -i *.txt is especially useful here.
Removing entire folders and their contents
To delete a folder and everything inside it, use rm -r foldername. The -r stands for recursive, meaning it deletes the folder, all files in it, and all subfolders and their files. rm -r ~/Documents/old_project removes the old_project folder and everything it contains.
Combine the flags for safety: rm -ri foldername will ask you to confirm each file and folder before deletion. This takes longer but prevents accidents. If you want to skip the confirmation for files but still confirm before deleting the main folder, use rm -r foldername and add -I (capital i) instead: rm -rI foldername asks once before deleting the whole tree.
Making deleted files harder to recover with shred
The rm command only removes the filename from the directory — the actual data on disk can sometimes be recovered with specialized tools. If you are deleting sensitive files like tax documents or old passwords, use shred instead: shred -u filename.txt. The -u flag means "unlink", which deletes the file after shredding it.
shred overwrites the file's data multiple times with random information before deleting it, making recovery much harder. By default it overwrites 3 times, but you can increase that: shred -vfz -n 10 filename.txt overwrites 10 times, with -v showing progress, -f forcing permission changes if needed, and -z overwriting with zeros on the final pass. This takes longer than rm, especially on large files, so use it only for files that actually matter.
Deleting files owned by other users or with restricted permissions
If you try to delete a file and get "Permission denied", the file is owned by another user or has restricted permissions. Try rm -f filename.txt, where -f stands for force. This skips the permission check if you own the parent folder.
If that does not work, you do not have permission to delete it. You can see who owns a file by typing ls -l filename.txt, which shows the owner in the third column. If it is owned by root or another user, you would need to ask that user to delete it, or use sudo rm filename.txt if you have administrator access (though this is risky — only use sudo when you are certain about what you are deleting).
Common mistakes and how to avoid them
The most common mistake is using a wildcard too broadly. rm *.txt in the wrong folder deletes every text file there. Always check your current folder first by typing pwd (print working directory) to see where you are, and ls to see what files are in it. Then run the delete command.
Another mistake is deleting a file you meant to keep because you mistyped the name. Use rm -i to get a confirmation prompt, or use tab completion: type rm file, then press Tab, and the terminal fills in the full filename. This way you see exactly what you are about to delete before you press Enter.
If you accidentally delete something important, stop using the computer when ready and look into recovery tools like testdisk or photorec (both free). The longer you use the computer, the more likely the deleted data gets overwritten. Recovery is not may provide, but it is more likely if you act quickly.
Frequently Asked Questions
Can I recover a file after I delete it with rm?
Not easily. rm only removes the filename from the directory, so the data technically remains on disk until something else overwrites it. Tools like testdisk or photorec can sometimes recover deleted files if you stop using the computer right away, but recovery is not may provide. If you need to keep something, do not delete it.
What is the difference between rm and shred?
rm deletes the filename when ready and leaves the data on disk, making recovery possible. shred overwrites the file's data multiple times before deleting it, making recovery much harder. shred takes longer, so use it only for sensitive files. For everyday files, rm is fine.
How do I delete a file with a space in its name?
Put the filename in quotes: rm "my file.txt" or rm my\ file.txt (the backslash escapes the space). Without quotes or escaping, Linux treats "my" and "file.txt" as two separate arguments and gets confused.
What does the -f flag do in rm?
rm -f forces deletion without asking for confirmation, even if the file has restricted permissions. It is faster but riskier because there is no prompt. Use rm -fi if you want force deletion but still want a confirmation prompt.
Can I delete a folder that is not empty?
Not with plain rm — you need rm -r foldername to delete a folder and everything inside it. Use rm -ri foldername to confirm each deletion, or rm -rI foldername to confirm once before deleting the entire tree.