The basic command to delete a file in Linux

To delete a file in Linux, use the rm command followed by the filename. Open a terminal and type rm filename.txt, then press Enter. The file disappears immediately — Linux does not move deleted files to a trash folder by default, so the file is gone once you confirm the command.

The rm command works on any file type: documents, images, archives, or anything else. You need write permission in the folder containing the file, but you do not need permission to the file itself. If you do not have permission, Linux will tell you "Permission denied" and the file stays.

Always double-check the filename before you press Enter, because recovery is difficult once a file is deleted. If you are new to the command line, consider using a file manager with a trash folder first — most Linux desktop environments let you right-click a file and move it to trash instead.

Key Takeaways

  • The rm command deletes files permanently by default, so Linux does not send them to trash — verify the filename before pressing Enter.
  • Delete a folder and everything inside it using rm -r foldername, but this is dangerous if you mistype the path.
  • Add the -i flag to make rm ask for confirmation before each deletion, which prevents accidents.
  • Most Linux desktop environments let you right-click files and move them to trash instead of deleting them permanently.

Deleting folders and everything inside them

To delete a folder along with all its contents, use rm -r foldername. The -r flag tells rm to work recursively, meaning it deletes the folder and everything nested inside it — subfolders, files, and all. This is powerful but dangerous: if you mistype the path, you can delete the wrong folder without warning.

For example, rm -r Documents/Old_Projects deletes the Old_Projects folder and everything in it. But rm -r Documents/Old_Projcts (note the typo) might delete a different folder if one exists, or fail silently if it does not.

If you are uncertain, use the -i flag instead: rm -ri foldername makes rm ask you to confirm each file and subfolder before deleting. This is slower but much safer, especially when you are learning.

Using the interactive flag to prevent accidents

Add the -i flag to any rm command to make it ask for confirmation before deleting each file. Type rm -i filename.txt and Linux will ask "remove regular file 'filename.txt'?" — type y and press Enter to confirm, or type n to skip it.

For folders, use rm -ri foldername to get a prompt for each file inside. This takes longer but catches mistakes before they happen. You can also combine other flags: rm -riv adds verbose output, showing you exactly what is being deleted as it happens.

Many Linux users create an alias to make rm default to interactive mode. In your shell configuration file (usually ~/.bashrc or ~/.zshrc), add the line alias rm='rm -i'. After that, every rm command will ask for confirmation unless you override it with the -f flag.

Deleting files by pattern or type

You can delete multiple files at once by using wildcards. The asterisk * matches any characters, so rm *.txt deletes all files ending in .txt in the current folder. The question mark ? matches exactly one character, so rm file?.txt deletes file1.txt and fileA.txt but not file12.txt.

Combine wildcards with the -i flag to stay safe: rm -i *.log asks you to confirm each .log file before deleting. Without the flag, all matching files disappear at once.

Be especially careful with broad patterns. rm -r * in the wrong folder deletes everything in that folder and all subfolders. Always navigate to the exact folder you want to clean up, verify you are in the right place with the pwd command, and use ls to see what will match before running rm.

Removing files with special characters or spaces in the name

If a filename contains spaces, put the entire name in quotes: rm "my document.txt". Without quotes, Linux treats each word as a separate argument and tries to delete files named "my" and "document.txt" separately.

For files with special characters like asterisks, dollar signs, or backslashes in the actual name, escape them with a backslash: rm file\*.txt deletes a file literally named "file*.txt", not all files matching that pattern. Alternatively, use single quotes to treat everything literally: rm 'file*.txt' does the same thing.

If you are unsure about the exact name, use ls to list files first, then copy and paste the name into the rm command. This avoids typos and special character confusion.

Deleting hidden files and system files

Linux files starting with a dot (.) are hidden by default — they do not show up in ls output unless you add the -a flag. To delete a hidden file, type the full name including the dot: rm .bashrc deletes the hidden .bashrc configuration file.

System files and configuration files are often hidden for a reason. Before deleting anything that starts with a dot, make sure you understand what it does. Deleting .bashrc breaks your shell configuration, and deleting files in .config or .local can break applications.

If you need to delete many hidden files at once, use rm -i .* with the interactive flag. This asks you to confirm each one, giving you a chance to skip important files.

What happens when you delete a file

When you run rm, Linux removes the filename from the directory listing and marks the disk space as available for reuse. The actual data on disk is not immediately overwritten — it stays there until another file is written to that space. This is why data recovery tools sometimes work on recently deleted files.

If you want to make deleted files harder to recover, use shred instead of rm: shred -vfz filename.txt overwrites the file with random data multiple times before deleting it. The -v flag shows progress, -f forces deletion even if the file is read-only, and -z overwrites with zeros on the final pass. This is slower but leaves almost nothing for recovery tools to find.

For sensitive files, shred is worth the extra time. For everyday files, rm is fine.

Frequently Asked Questions

Can I undo a deletion in Linux?

No — once rm deletes a file, it is gone from the directory. If you have a recent backup, you can restore from there. Some file managers have a trash folder that holds deleted files for a while, but the command-line rm command bypasses trash entirely. This is why the -i flag exists: it forces you to confirm before the deletion happens.

What is the difference between rm and rmdir?

The rmdir command deletes only empty folders. If a folder contains any files, rmdir refuses to delete it and tells you "Directory not empty". Use rm -r to delete a folder and everything inside it. rmdir is safer because it will not accidentally delete a folder full of files, but it is slower if you need to clean up many nested folders.

How do I delete a file if I do not have permission?

You need write permission in the folder containing the file, not permission to the file itself. If you own the folder, use chmod to add write permission: chmod u+w foldername gives you write access. If someone else owns the folder, ask them to delete the file or give you permission. Using sudo to force deletion can cause problems if you delete the wrong thing, so avoid it unless necessary.

What does the -f flag do?

The -f flag forces deletion without asking questions, even if a file is read-only or you do not have write permission. rm -f filename deletes the file silently. This is useful in scripts where you do not want prompts, but dangerous in interactive use because it skips all safety checks. Never use rm -rf / or similar broad commands with -f.

Can I delete a file that is currently open?

Yes — Linux lets you delete a file even if a program is using it. The file disappears from the directory, but the program keeps the data in memory and can continue working. Once the program closes, the disk space is freed. This is different from Windows, which locks files while they are in use.