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 immediately — Linux does not send deleted files to a trash folder by default, so the file is gone once you confirm the command. There is no undo button.

If the filename has spaces in it, wrap the name in quotes: rm "my document.txt". If you are not sure of the exact name, you can list files in your current folder first by typing ls to see what you are working with.

Key Takeaways

  • The rm command deletes files permanently in Linux — they do not go 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 loss.
  • To delete multiple files at once, list them separated by spaces: rm file1.txt file2.txt file3.txt.
  • The rm -r flag deletes folders and everything inside them, and it works even if the folder is not empty.
  • Protected files owned by another user require sudo permission, but using sudo with rm carries real risk if you mistype the path.

Delete multiple files at once using wildcards

If you want to delete several files that share a pattern in their name, you can use a wildcard instead of typing each name. The asterisk * matches any characters. For example, rm *.txt deletes every file in the current folder that ends with .txt.

Be careful with wildcards — rm * deletes everything in the current folder. A safer approach is to use rm -i *.txt, which asks you to confirm each deletion one by one. This gives you a chance to stop if you see a file you did not mean to delete.

Delete folders and their contents with the recursive flag

To delete a folder (also called a directory) and all the files inside it, use rm -r foldername. The -r flag stands for recursive, meaning it deletes the folder itself plus everything nested inside, no matter how many levels deep.

If the folder is not empty and you try rm foldername without the -r flag, Linux will refuse and show an error message. This is a safety feature. If you see that error, you know you need to add -r to proceed, or you can delete the files inside the folder first and then delete the empty folder.

Add a confirmation prompt to prevent accidental deletion

The -i flag (short for interactive) makes rm ask you to confirm before it deletes anything. Type rm -i filename.txt and Linux will print a question asking whether you really want to remove the file. You type y for yes or n for no, then press Enter.

You can combine flags together: rm -ri foldername deletes a folder and everything in it, but asks for confirmation on each file. This is slower if you have hundreds of files, but it is the safest way to delete something important. Some people create an alias in their shell configuration so that rm always runs as rm -i by default, though this slows down everyday work.

Delete files owned by another user with sudo

If a file is owned by another user (or by the root account), you may not have permission to delete it. When you try, you will see a message like "Permission denied". To delete it anyway, use sudo rm filename.txt. The sudo command runs the deletion with administrator privileges.

Using sudo with rm is powerful and dangerous — if you mistype the path, you can delete system files that your computer needs to run. Never copy and paste a sudo rm command from the internet without reading it carefully first. If you are not sure whether a file is safe to delete, ask someone who knows your system before you use sudo.

Securely delete files so they cannot be recovered

The standard rm command deletes the filename from the directory, but the actual data on disk can sometimes be recovered using special tools. If you are deleting files that contain sensitive information — passwords, financial records, or personal documents — you may want to overwrite the data so it cannot be read again.

The shred command overwrites a file multiple times before deleting it. Type shred -vfz filename.txt. The flags mean: -v shows progress, -f forces deletion even if the file is read-only, and -z overwrites with zeros at the end to hide the fact that shred was used. On some systems, wipe or srm serve the same purpose. Check whether your Linux distribution includes shred by typing which shred — if it returns a path, the command is available.

Understand what happens when you delete a file

When you type rm filename.txt, Linux removes the filename from the directory listing and marks that space on disk as available for new data. The actual file content stays on disk until something else writes over it. This is why recovery tools can sometimes restore deleted files — the data is still there, just invisible to the operating system.

This also means that if you delete a file by accident and immediately stop using the computer, a recovery tool might be able to bring it back. But the longer you wait and the more you use the system, the more likely that new files will overwrite the deleted data. There is no may provide recovery will work, and it requires specialized knowledge or paid software.

Frequently Asked Questions

Can I recover a file after I delete it with rm?

Maybe, but not reliably. The file data remains on disk until something else writes over it. Recovery tools like testdisk or photorec can sometimes restore deleted files if you stop using the computer immediately. The longer you wait, the less likely recovery becomes. If the file is critical, stop using the system and look for a recovery tool right away.

What is the difference between rm and rmdir?

rmdir deletes only empty folders and refuses to work if anything is inside. rm -r deletes a folder and everything in it, no matter how full. Use rmdir when you want extra safety — it will not accidentally delete a folder full of files. Use rm -r when you want to delete a folder and all its contents in one command.

Do I need sudo to delete my own files?

No. You can delete any file you own without sudo. You only need sudo if the file is owned by another user or by root. Check who owns a file by typing ls -l filename.txt — the owner name appears in the third column.

What does the -f flag do with rm?

rm -f forces deletion without asking, even if the file is read-only or protected. It is useful in scripts where you do not want confirmation prompts, but dangerous in manual commands because it skips all safety checks. Avoid -f unless you are certain what you are deleting.

Can I delete a file that is currently open?

Yes, Linux allows you to delete a file even while a program is using it. The program keeps the file data in memory and continues working. The filename disappears from the directory, but the file does not fully vanish from disk until the program closes and releases it. This is different from Windows, which locks files while they are open.