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 where the file is, and type rm filename.txt (replace "filename.txt" with the actual name). Press Enter, and the file is gone. Linux does not move deleted files to a trash folder — they are removed when ready, so double-check the filename before you press Enter.

The rm command works the same way whether you are deleting a document, image, or any other file type. You only need to know the exact filename, including the extension. If the filename has spaces in it, put the whole name in quotes: rm "my document.txt".

If you are not sure whether you are in the right folder, type pwd first to see your current location, or type ls to list all files in the folder. This takes two seconds and prevents deleting the wrong file.

Key Takeaways

  • The rm command deletes files permanently — they do not go to trash, so verify the filename before you press Enter.
  • Type rm filename.txt in the terminal, replacing "filename.txt" with the actual name of the file you want to delete.
  • If a filename contains spaces, wrap it in quotes: rm "my document.txt".
  • Use ls to list files in your current folder and pwd to see where you are before deleting anything.
  • To delete multiple files at once, list them separated by spaces: rm file1.txt file2.txt file3.txt.

Deleting multiple files at the same time

If you need to delete several files, you can list them all in one command. Type rm file1.txt file2.txt file3.txt, separating each filename with a space. This is faster than deleting them one at a time.

You can also use a wildcard to delete files that match a pattern. For example, rm *.txt deletes all files ending in .txt in your current folder. Be very careful with wildcards — they match many files at once, and you cannot undo the deletion. Type ls *.txt first to see which files will be deleted before you run the rm command.

Deleting files in a different folder

You do not have to navigate to a folder to delete a file in it. You can type the path to the file directly. For example, rm /home/username/Documents/oldfile.txt deletes the file without changing your current location.

If the path is long or you use it often, navigate to the folder first with cd. Type cd /home/username/Documents, then rm oldfile.txt. This is easier to read and less error-prone than typing a long path.

What to do if you get a "permission denied" error

If you see "permission denied" when you try to delete a file, you do not have permission to remove it. This usually means the file is owned by a different user or the folder has restricted permissions. Type ls -l filename.txt to see who owns the file and what permissions are set.

If you own the file but still cannot delete it, the folder itself may be protected. Try navigating to the parent folder and deleting from there. If the file is owned by root or another user, you may need to ask the system administrator to remove it, or use sudo rm filename.txt if you have administrator access. Using sudo bypasses permission checks, so only do this if you are certain you want to delete the file.

Recovering a deleted file

Once you delete a file with rm, it is gone from the normal filesystem. Linux does not have a built-in undo for the rm command. However, if your system has backups running, a system administrator may be able to recover the file from a backup. Ask your IT department or system administrator when ready if you need a deleted file restored.

Some recovery tools exist for Linux, but they work only if the disk space has not been overwritten yet. The longer you wait after deletion, the less likely recovery becomes. If you deleted something important by accident, stop using the computer and contact your administrator right away.

Removing empty folders

The rm command deletes files, but to delete an empty folder, use rmdir foldername. This only works if the folder is completely empty. If the folder contains files, you must delete the files first, then delete the folder.

If you want to delete a folder and everything inside it at once, use rm -r foldername. The -r flag means "recursive" and tells rm to delete the folder and all its contents. Be extremely careful with this command — it deletes everything inside without asking for confirmation. Type ls -r foldername first to see what will be deleted.

Frequently Asked Questions

Can I delete a file I do not own?

No, unless you have administrator access. If you try, you will see a "permission denied" error. Only the file owner or root can delete a file. If you need a file removed that belongs to someone else, ask the owner or your system administrator.

What if the filename has special characters or spaces?

Put the entire filename in quotes: rm "my file (1).txt". This tells the terminal to treat the whole thing as one filename instead of separate words. If you forget the quotes, the terminal will think each word is a separate filename and may delete the wrong files.

How do I delete a file without typing the full name?

Use tab completion. Type the first few letters of the filename, then press Tab. The terminal will finish the name for you if there is only one match. If multiple files start with those letters, press Tab again to cycle through them. This is faster and reduces the chance of typos.

Is there a way to delete files safely without losing them forever?

Not with the standard rm command. Some Linux users install a trash utility that moves deleted files to a trash folder instead of removing them permanently. If your system does not have one, you can manually move files to a "trash" folder you create yourself, then delete them later if you are sure you do not need them.

What does the -f flag do in the rm command?

The -f flag forces deletion without asking for confirmation. Normally, if a file is read-only, rm asks before deleting it. With -f, it deletes anyway. Avoid using -f unless you are certain — it removes the safety check that prevents accidental deletion.