Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Remove Files and Directories in Linux

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To remove files and directories in Linux, use the rm command for files and rmdir or rm -r for directories. The simplest way is rm filename to delete a file, and rm -r directory to delete a folder and everything inside it. This guide covers the exact commands, safety checks, and what to do when something goes wrong.

What You'll Need

  • A Linux terminal (command line) — open it with Ctrl+Alt+T on most desktop distributions.
  • Basic familiarity with navigating directories using cd and listing files with ls.
  • Appropriate permissions: you need write permission on the directory containing the file, and if removing a directory, write permission on its parent.

Step-by-Step

  1. Open a terminal. Press Ctrl+Alt+T or search for “Terminal” in your applications menu.
  2. Navigate to the location of the file or directory you want to remove. Use cd to change directories. For example: cd /home/username/Documents.
  3. List the contents to confirm the exact name of the item. Run ls to see files and folders in the current directory. Pay attention to spaces and special characters — you may need to quote the name, e.g., rm "my file.txt".
  4. Remove a single file with rm filename. For example: rm notes.txt. This permanently deletes the file.
  5. Remove multiple files by listing them: rm file1.txt file2.txt file3.txt.
  6. Remove an empty directory with rmdir directoryname. This only works if the directory is completely empty.
  7. Remove a directory and its contents (non-empty) with rm -r directoryname. The -r stands for recursive, meaning it deletes the folder and everything inside it.
  8. Add the -f flag to force removal without prompts, but use it carefully: rm -rf directoryname. This bypasses confirmation and ignores nonexistent files.
  9. Use -i for interactive confirmation if you want to be prompted before each deletion: rm -i filename. This is safer for beginners.
  10. Verify the removal by running ls again to confirm the item is gone.

Common Mistakes

  • Using rmdir on a non-empty directory — it will fail with “Directory not empty”. Use rm -r instead.
  • Forgetting the -r flag when deleting a directory — rm directory returns an error like “cannot remove 'directory': Is a directory”.
  • Using rm -rf with a typo — for example, rm -rf / home (with a space) could delete your home directory. Always double-check the path.
  • Deleting files permanently — there is no trash bin for rm; once deleted, the data is gone unless you have a backup.
  • Ignoring special characters — filenames with spaces or symbols need quotes or escaping, otherwise the command may fail or delete the wrong file.

Tips

  • Use -i when you're unsure — it asks for confirmation before each deletion, reducing accidental loss.
  • Create a backup of important files before deleting, especially when using -r or -f.
  • Use tab completion to avoid typos: type the first few letters of the filename and press Tab to autocomplete.
  • Check your current directory with pwd before running rm -rf . — that command deletes the current directory and everything in it.
  • Consider using a safer alternative like trash (if installed) to move files to a trash folder instead of permanent deletion.

Troubleshooting

If the command fails or you accidentally delete the wrong thing, here's what to do:

  • “Permission denied” error — you don't have write permission on the directory. Try adding sudo before the command (e.g., sudo rm filename), but only if you own the system and understand the risk. Alternatively, change permissions with chmod if you own the file.
  • “Directory not empty” — the directory contains hidden files (starting with a dot). Use ls -a to see them, then delete them individually or use rm -r.
  • Accidentally deleted a file — if the file is on a filesystem that supports snapshots or if you have a backup, restore from there. Otherwise, stop using the disk immediately and try recovery tools like extundelete or photorec, but success is not guaranteed.
  • Command not found — rm is part of coreutils and should be present on any Linux system. If not, reinstall coreutils via your package manager.
  • File name starts with a dash — for example, a file named -file. Use rm -- -file to tell rm that the dash is not an option.

Frequently Asked Questions

Can I recover a file deleted with rm?

Generally no, because rm doesn't send files to a trash folder. If you have a backup or a snapshot, restore from there. Otherwise, data recovery tools might work if you act quickly and the disk hasn't been overwritten.

What's the difference between rmdir and rm -r?

rmdir only removes empty directories. rm -r removes directories and all their contents recursively, including files and subdirectories.

Is rm -rf dangerous?

Yes, because it force-deletes without confirmation and recursively. A typo or wrong path can wipe out important data. Always double-check the path and consider using -i instead.

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

Quote the filename: rm "my file.txt" or escape the space: rm my\ file.txt.

Can I delete a directory that is not empty without using rm -r?

No, you must use rm -r or first delete all contents manually, which is tedious. rm -r is the standard way.

What does the -f flag do exactly?

It stands for “force” and tells rm to ignore nonexistent files and never prompt for confirmation. It also overrides write permissions in some cases, so use it with caution.