The rm command removes files permanently
To delete a file in Linux, open a terminal and type rm filename, then press Enter. The file disappears when ready — Linux does not move deleted files to a trash folder by default, so once you press Enter, the file is gone. There is no undo button.
This is different from deleting files on Windows or Mac, where files go to a recycle bin first. In Linux, rm is final. That speed is useful when you know exactly what you want to remove, but it also means you need to be careful about what you type.
Key Takeaways
- The command rm filename deletes a single file permanently, with no recovery option unless you have a backup.
- Use rm -i filename to add a confirmation prompt before deletion, which prevents accidental removal.
- To delete a folder and everything inside it, use rm -r foldername, but only after checking the contents first.
- Deleted files cannot be recovered through the operating system, so verify the filename and path before you press Enter.
How to delete a single file safely
Before you type the delete command, navigate to the folder where the file lives. Use ls to list the files in your current location so you can see exactly what is there. Once you have confirmed the filename, type rm filename and press Enter.
If you want a confirmation prompt before the file disappears, use rm -i filename instead. The -i flag stands for "interactive" and makes the system ask "remove regular file 'filename'?" before it deletes anything. You type y for yes or n for no. This takes one extra second but prevents the mistake of deleting the wrong file.
A common beginner mistake is typing the command in the wrong folder. If you are not sure where you are, type pwd (print working directory) to see the full path. Then use cd foldername to move to the right location before you delete anything.
Deleting multiple files at once
You can delete several files with one command. Type rm file1 file2 file3 and all three disappear when you press Enter. This is faster than deleting them one at a time, but it also means one typo can delete the wrong files.
To be safer, use rm -i file1 file2 file3 so the system asks about each file before removing it. You can say yes to some and no to others. If you have many files to delete and they follow a pattern — like all files ending in .txt — you can use rm *.txt to delete them all at once, but again, use rm -i *.txt to see what you are about to remove.
Deleting folders and everything inside them
To delete a folder, you need the -r flag, which stands for "recursive." Type rm -r foldername and the folder plus all files and subfolders inside it are deleted. This is powerful and dangerous — one wrong folder name can wipe out hours of work.
Before you use rm -r, open the folder in your file manager or use ls -la foldername to see what is actually inside. Make sure you are deleting the right thing. Then use rm -ri foldername to combine the recursive flag with the interactive flag, so the system asks about each item before deleting it. This is slower but much safer.
Never use rm -r / or rm -r /* — these commands delete your entire system and cannot be undone. It is a common prank to suggest this to new Linux users. Do not do it.
Understanding file paths and wildcards
When you type a filename, Linux looks for it in your current folder. If the file is somewhere else, you need to give the full path. For example, rm /home/username/Documents/oldfile.txt deletes the file even if you are not in the Documents folder. You can also use ~ as shorthand for your home folder, so rm ~/Documents/oldfile.txt works the same way.
A wildcard is a symbol that matches multiple files. The asterisk * matches any characters, so rm *.log deletes all files ending in .log. The question mark ? matches exactly one character, so rm file?.txt deletes file1.txt, file2.txt, and fileA.txt but not file10.txt. Always use ls first to see what the wildcard will match before you delete.
What happens after you delete a file
When you delete a file with rm, the operating system removes the filename from the directory listing, but the actual data on the disk is not when ready overwritten. In theory, a specialist with the right tools could recover the file. In practice, this is difficult and expensive, and it only works if nothing else has written to that part of the disk yet.
If you need to make sure a file cannot be recovered, you can use shred filename instead of rm. The shred command overwrites the file multiple times before deleting it, making recovery much harder. This takes longer and uses more disk activity, so most people only use it for sensitive files like passwords or financial records.
The safest approach is to keep backups of anything important. If you delete a file by mistake, a backup is the only way to get it back. Linux does not have a trash folder to rescue you, so backups are your safety net.
Frequently Asked Questions
Can I undo a delete in Linux?
No. Once you press Enter after typing rm, the file is gone from the operating system's perspective. There is no undo command and no trash folder. Your only option is to restore from a backup if you have one. This is why using rm -i to add a confirmation prompt is a good habit.
What is the difference between rm and rmdir?
The rmdir command deletes only empty folders. If the folder has any files in it, rmdir will refuse to delete it and show an error. The rm -r command deletes folders and everything inside them without asking. Use rmdir when you want extra protection against accidentally deleting a folder with files in it.
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'. Without quotes, Linux treats the space as a separator and looks for a file called "my" instead. You can also use a backslash before the space: rm my\ file.txt. Both methods work the same way.
What if I delete a file by mistake?
If you have a recent backup, restore the file from it. If you do not have a backup and the file was just deleted, you might recover it with a tool like testdisk or photorec, but success is not may provide and depends on how much the disk has been used since deletion. Prevention through backups and the -i flag is much more reliable than recovery.