The Basic Command to Remove a File

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 (replacing "filename.txt" with your actual filename), then press Enter. The file is gone immediately — Linux does not move deleted files to a trash folder by default, so the file cannot be recovered through a graphical interface.

If you are in a different folder than the file, you can delete it by typing the full path: rm /home/username/Documents/filename.txt. Use pwd to see which folder you are currently in, and ls to list the files in that folder so you can verify the exact filename before you delete it.

Key Takeaways

  • The rm command deletes a file permanently — it does not go to trash, so verify the filename before you press Enter.
  • Use rm filename.txt to delete a single file, or rm file1.txt file2.txt file3.txt to delete multiple files at once.
  • The -i flag asks for confirmation before each deletion: rm -i filename.txt prevents accidental removal.
  • To delete a folder and everything inside it, use rm -r foldername, but only after you are certain you want to remove the entire contents.

Deleting Multiple Files at Once

If you need to remove several files, you can list them all in one command: rm file1.txt file2.txt file3.txt. This deletes all three files in a single action.

You can also use a wildcard pattern to delete files that match a name. For example, rm *.txt deletes every file ending in .txt in the current folder. Be very careful with wildcards — they match more files than you might expect. Always use ls *.txt first to see exactly which files will be deleted before you run the rm command.

Adding Confirmation to Prevent Accidents

The -i flag makes rm ask "are you sure?" before deleting each file. Type rm -i filename.txt and Linux will prompt you to confirm. Type y and press Enter to delete, or type n and press Enter to keep the file. This is especially useful when you are learning or when you are deleting multiple files with a wildcard.

Some Linux systems are set up to use rm -i by default, so you may already see a confirmation prompt. You can check by typing just rm and pressing Enter with no arguments — if the system shows you a message about usage, it is not set to confirm by default.

Deleting Folders and Everything Inside Them

To delete a folder that contains files, use the -r flag, which stands for "recursive." Type rm -r foldername to delete the folder and all files and subfolders inside it. This is permanent and cannot be undone, so double-check the folder name and use ls -la foldername first to see what is inside.

You can combine flags for extra safety: rm -ri foldername will ask you to confirm before deleting each file inside the folder. This takes longer but gives you a chance to stop if you see something you did not expect to delete.

Understanding File Permissions and Ownership

You can only delete a file if you own it or if you have write permission on the folder containing it. If you try to delete a file you do not own, Linux will show an error like "Operation not permitted." You cannot delete a file just by having read or write permission on the file itself — you need write permission on the folder.

If you own the folder but not the file, you can still delete the file. If you do not own the folder, you cannot delete anything in it, even if you own individual files. Use ls -l filename.txt to see who owns a file and what permissions are set. If you need to delete a file you do not own, ask the file owner or a system administrator.

What Happens After You Delete a File

When you delete a file with rm, the file is removed from the directory listing immediately. The space it occupied on disk is marked as available for new files. In theory, deleted data can sometimes be recovered using specialized tools if the space has not been overwritten, but this requires technical knowledge and does not work on all systems or filesystems.

For most everyday use, deleted files are gone. If you delete something important by accident, your best option is to restore it from a backup if one exists. This is why many people use the -i flag or keep backups of important files.

Frequently Asked Questions

Can I undo a deletion after I press Enter?

No. The rm command does not send files to trash — it removes them immediately. Once you press Enter, the file is deleted and cannot be recovered through the terminal. If you have a backup, you can restore from that. Otherwise, the file is gone.

What is the difference between rm and rmdir?

The rmdir command deletes only empty folders. The rm -r command deletes a folder and everything inside it, whether it is empty or not. Use rmdir when you want to be extra careful and only delete a folder you know is empty.

How do I delete a file with a space in its name?

Put the filename in quotes: rm "my file.txt". Without quotes, Linux treats "my" and "file.txt" as two separate arguments and tries to delete both. You can also use a backslash before the space: rm my\ file.txt.

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

Not without changing the permissions first. If you own the folder but not the file, you can delete it anyway because you have write permission on the folder. If you do not own the folder, you cannot delete anything in it. Contact the file owner or administrator if you need a file removed that you cannot access.

What does the -f flag do?

The -f flag forces deletion without asking for confirmation, even if you do not have write permission on the file. Use it carefully — rm -f filename.txt will delete the file silently with no prompt. Combine it with -r for folders: rm -rf foldername deletes a folder and everything inside it without any confirmation.