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 folder containing the file, and type rm filename.txt (replace "filename.txt" with your actual filename), then press Enter. The file is gone when ready — Linux does not move deleted files to a trash folder by default.

This is different from deleting files on Windows or Mac, where files go to a recycle bin first. Once you press Enter after an rm command, the file cannot be recovered through normal means. There is no undo button.

Key Takeaways

  • The rm command deletes files permanently and when ready, with no recovery option, so double-check the filename before you press Enter.
  • Use rm with a full path (like rm /home/username/Documents/oldfile.txt) if you are not already in the folder containing the file.
  • To delete multiple files at once, list them after rm separated by spaces, or use a wildcard like rm *.txt to delete all files ending in .txt.
  • The -i flag (rm -i filename.txt) makes Linux ask for confirmation before deleting, which prevents accidental loss.
  • To delete a folder and everything inside it, use rm -r foldername, but only after confirming the folder contains what you think it does.

Deleting files in your current folder versus other locations

If you are already in the folder where the file lives, type rm filename.txt and the file deletes. You can check what folder you are in by typing pwd (print working directory) and pressing Enter.

If the file is in a different folder, you have two options. You can navigate to that folder first using cd (change directory) — for example, cd Documents moves you into your Documents folder — then use rm as normal. Or you can type the full path to the file: rm /home/username/Documents/filename.txt. The full path starts from the root of your system (the / at the beginning) and lists every folder down to the file itself.

Deleting multiple files at once

To delete several files in the same folder, list them all after rm separated by spaces: rm file1.txt file2.txt file3.txt. All three files delete with one command.

You can also use a wildcard to delete files that match a pattern. The asterisk (*) stands for "any characters". For example, rm *.txt deletes every file in your current folder that ends in .txt. The command rm old_* deletes every file that starts with "old_". Be very careful with wildcards — they can delete far more than you intended if you are not specific enough.

Protecting yourself from accidental deletion

Add the -i flag to make Linux ask for confirmation before each deletion. Type rm -i filename.txt, and Linux will print the filename and ask "remove regular file 'filename.txt'?" Type y and press Enter to delete, or type n and press Enter to keep the file. This takes a few extra seconds but prevents mistakes.

You can also combine -i with multiple files or wildcards: rm -i *.txt asks for confirmation before deleting each .txt file in your folder. If you have many files, this becomes tedious, but it is the safest approach when you are not completely certain what you are deleting.

Deleting folders and everything inside them

To delete a folder, use rm with the -r flag (recursive): rm -r foldername. This deletes the folder and all files and subfolders inside it. There is no warning and no recovery, so check the folder contents first by typing ls foldername to see what is in it.

You can combine -r with -i for safety: rm -ri foldername asks for confirmation before deleting each item inside the folder. This is slow but thorough. If you are certain about what you are deleting and want no prompts, rm -r foldername works without asking.

What happens after you delete a file

When you delete a file with rm, Linux marks the space on your hard drive as available for new data, but the file itself is not when ready overwritten. In theory, a specialist with the right tools could recover deleted files for a short time after deletion. In practice, for personal use on a home computer, deleted files are gone.

If you need to delete files so thoroughly that recovery is impossible — for example, before selling a computer or disposing of a hard drive — you need a different tool. The shred command overwrites a file multiple times before deleting it: shred -vfz filename.txt. The -v flag shows progress, -f forces deletion even if the file is read-only, and -z overwrites with zeros at the end. This takes longer but makes recovery much harder.

Common mistakes and how to avoid them

The most common mistake is using a wildcard too broadly. For example, rm -r * in your home folder deletes everything in your home folder. Always test a wildcard first by typing ls with the same pattern: ls * shows you what files match before you delete them.

Another mistake is deleting the wrong file because you typed the name wrong. Before pressing Enter, read the filename on your screen and make sure it matches what you intended to delete. If you are tired or distracted, use rm -i to add a confirmation step.

Frequently Asked Questions

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

Not through normal Linux tools. Once deleted, the file is gone from your view when ready. Recovery is technically possible for a short window using specialized software, but it is not straightforward and becomes harder the more you use your computer after deletion. Always use rm -i if you are unsure.

What is the difference between rm and rmdir?

The rmdir command deletes only empty folders. If a folder has files in it, rmdir refuses to delete it and tells you the folder is not empty. Use rm -r to delete a folder and its contents. rmdir is safer because it prevents accidental deletion of folders with files you forgot about.

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 a backslash, Linux treats "my" and "file.txt" as two separate arguments and gets confused.

Can I delete a file I do not have permission to delete?

No, unless you own the file or have write permission on the folder containing it. Linux will print "Permission denied" and the file stays. If you need to delete it, you may need to use sudo (which runs the command as the administrator), but only do this if you are certain the file should be deleted.

What does the -f flag do in rm?

The -f flag forces deletion without asking for confirmation, even if the file is read-only. Use rm -f carefully — it is the opposite of -i and gives you no safety net. Combining -f with wildcards (rm -f *.txt) is especially risky.