Smart Systems for Home

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

Delete Files & Folders in Linux Terminal

· 6 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

Deleting files and folders from the Linux command line is a core skill for anyone working with a server, a headless system, or just preferring the speed of the terminal. The rm (remove) command is the primary tool, and with the right flags it can handle single files, multiple files, and entire directory trees. This guide covers the essential commands, safe usage patterns, and how to avoid the most common pitfalls.

What You'll Need

  • A Linux system with a terminal emulator (or SSH access to a remote machine).
  • A user account with write permission on the files or folders you intend to delete. For system files or directories owned by root, you may need sudo.
  • Basic familiarity with navigating the command line (cd, ls, pwd).

Step-by-Step

  1. Open a terminal window. On most desktop Linux distributions, you can press Ctrl+Alt+T or search for "Terminal" in the applications menu. If you're on a remote server, connect via SSH.
  2. Navigate to the directory containing the file or folder. Use the cd command. For example, to go to your Documents folder: cd ~/Documents. Always confirm your current location with pwd before deleting anything.
  3. List the contents to verify what you're about to delete. Run ls -la to see all files, including hidden ones (those starting with a dot), along with permissions and sizes. This is your last chance to double-check.
  4. Delete a single file. The basic syntax is rm filename. For example: rm notes.txt. This permanently removes the file without sending it to a trash folder.
  5. Delete multiple files at once. You can list them all in one command: rm file1.txt file2.txt file3.txt. You can also use wildcards to match patterns, like rm *.log to delete all files ending in .log in the current directory. Be extremely careful with wildcards—they can match more than you expect.
  6. Delete an empty directory. Use the rmdir command: rmdir empty_folder. This only works if the directory is completely empty; otherwise it will fail with an error.
  7. Delete a directory and its contents recursively. Use rm -r foldername (or rm -R). The -r flag tells rm to go through the directory recursively, deleting everything inside it. For example: rm -r old_project.
  8. Add the force flag for problem files. Some files may be write-protected or have restrictive permissions. Adding -f (force) will override that and remove them without prompting. Combine it with recursive: rm -rf foldername. This is the most powerful and dangerous form—use it only when you are absolutely certain.
  9. Use interactive mode for safety. Add the -i flag to be prompted before each deletion: rm -i file.txt or rm -ri folder. This is a good habit when you're unsure about the exact contents.
  10. Verify the deletion. Run ls again to confirm the file or folder is gone. If you deleted a directory, ls -la should show its name no longer exists.

Common Mistakes

  • Using rm -rf without double-checking the path. A single typo, like a space or a missing slash, can turn rm -rf ~/my files into a command that deletes your home directory and a folder named "files".
  • Using wildcards incorrectly. For example, rm *.txt deletes all files ending in .txt, but rm * .txt (with a space) deletes everything in the current directory plus a file named .txt.
  • Forgetting that rm is permanent. There is no trash bin in the command line. Once deleted, the data is gone unless you have a backup. There is no undo command.
  • Using rmdir on a non-empty directory. It will fail and you might think the deletion didn't work, when in fact you just used the wrong command.
  • Deleting files with spaces in their names without quoting. For a file named my file.txt, you must use rm "my file.txt" or escape the space with a backslash: rm my\ file.txt.

Tips

  • Always use -i when you're learning. It forces you to confirm each deletion, which prevents accidents.
  • Use ls with the same wildcard before deleting. For example, run ls *.tmp first to see exactly which files match, then run rm *.tmp.
  • Create an alias for safer deletion. You can add alias rm='rm -i' to your ~/.bashrc or ~/.zshrc file. This makes interactive mode the default for your shell session.
  • Consider using a trash command. Tools like trash-cli provide a trash command that moves files to a recycle bin, giving you a safety net. Install it with your package manager (e.g., sudo apt install trash-cli on Debian/Ubuntu).
  • For important deletions, use rm -i or rm -I. The uppercase -I prompts only once before deleting more than three files or when deleting recursively—less annoying but still safe.
  • Back up critical data regularly. Even with all precautions, accidents happen. A simple tar or rsync backup can save you.

Troubleshooting

  • "Permission denied" error. This means your user doesn't have write permission on the file or the directory containing it. Check the owner and permissions with ls -l. If you need to delete it, you can use sudo rm filename (if you have sudo privileges), but be absolutely sure you want to delete it.
  • "Directory not empty" error with rmdir. The directory contains hidden files or subdirectories. Use ls -la to see them, then either delete them individually or use rm -r instead.
  • File is busy or in use. If you get an error like "Text file busy" or "Device or resource busy", the file is being used by a running process. You can try closing the application or using lsof filename to find the process ID and then kill it, but be careful.
  • You accidentally deleted the wrong file. Immediately stop writing to the disk. If you have a backup, restore it. If not, you might try data recovery tools like testdisk or photorec, but success is not guaranteed, especially on SSDs with TRIM enabled.
  • The command seems to hang. If you're using rm -r on a huge directory, it can take a while. You can add the -v (verbose) flag to see progress: rm -rv folder.

Frequently Asked Questions

Can I recover a file deleted with rm? Generally, no. rm unlinks the file immediately, and the space is marked as available. Unless you have a backup or use specialized recovery software quickly, the data is likely lost.

What's the difference between rm and unlink? unlink is a lower-level command that deletes a single file and nothing else. It's rarely used. rm is the standard tool with more features like recursive deletion and wildcard support.

Is there a way to send files to trash instead of permanent deletion? Yes, install trash-cli and use the trash command. It moves files to the desktop trash, which you can restore later.

How do I delete a file that starts with a dash, like -file.txt? Use rm -- -file.txt. The double dash -- tells rm to stop interpreting options and treat everything after as a filename.

What does rm -rf / do? It attempts to delete everything on the root filesystem. On modern systems, it may be blocked by protections, but if it runs, it will destroy your operating system. Never run this command.

Can I use rm to delete files matching a pattern but keep some? Yes, use extended globbing or a loop. For example, to delete all .txt files except keep.txt: rm !(keep.txt) after enabling shopt -s extglob in bash. Alternatively, use find with -not -name.