Smart Systems for Home

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

How to Delete a File in Terminal

· 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 delete a file in a terminal, use the rm command followed by the file name (e.g., rm filename.txt). This works on Linux, macOS, and Windows Subsystem for Linux (WSL). The command permanently removes the file—it does not go to a trash or recycle bin—so double-check the name before pressing Enter.

What You'll Need

  • A terminal or command prompt (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows, or WSL).
  • The exact name or path of the file you want to delete.
  • Write permission for the directory containing the file (you usually have this for files you created).

Step-by-Step

  1. Open the terminal. On macOS, go to Applications > Utilities > Terminal. On Linux, press Ctrl+Alt+T. On Windows, open Command Prompt or PowerShell (or WSL if you prefer Linux commands).
  2. Navigate to the directory containing the file (if needed). Use cd to change directories. For example: cd Documents. To check where you are, type pwd (print working directory).
  3. List the files to confirm the exact name. Type ls (on Linux/macOS) or dir (on Windows Command Prompt) to see the files. This helps avoid typos and ensures you're deleting the right file.
  4. Run the rm command with the file name. For example: rm myfile.txt. If the file is in a different directory, you can include the path: rm /path/to/myfile.txt.
  5. Press Enter. The file is deleted immediately. There is no confirmation prompt unless you use the -i flag (see Tips).
  6. Verify the deletion (optional). Run ls again to confirm the file is gone.

Common Mistakes

  • Deleting the wrong file due to a typo or wrong path. Always list the directory first.
  • Using rm on a directory without the -r flag. This gives an error like rm: cannot remove 'folder': Is a directory. To delete a directory and its contents, use rm -r folder (be very careful—this is irreversible).
  • Forgetting that rm bypasses the trash. There is no undo. Once deleted, the file is gone.
  • Using wildcards carelessly. For example, rm *.txt deletes all .txt files in the current directory. Double-check the pattern before running.
  • Running rm as root (sudo) when not needed—this can delete system files if you make a mistake.

Tips

  • Use the -i flag for safety: rm -i file.txt will ask for confirmation before deleting.
  • Use -v to see what's being deleted: rm -v file.txt prints the name of each file as it's removed.
  • Use absolute paths when in doubt: rm /home/username/Documents/report.pdf avoids confusion if you're not sure of the current directory.
  • For files with spaces in the name, enclose the name in quotes: rm "my file.txt" or escape the space with a backslash: rm my\ file.txt.
  • On Windows Command Prompt, the del command works similarly: del file.txt. PowerShell also supports Remove-Item or del.
  • If you're nervous, move the file to a temporary folder instead of deleting it immediately. You can always delete it later.

Troubleshooting

If you get "No such file or directory": Check your spelling and path. Use ls to see what's actually there.

If you get "Permission denied": You don't have write permission for that directory. Try sudo rm filename (on Linux/macOS) but be aware that using sudo can be risky—only use it if you're certain the file is safe to delete. On Windows, you may need to run the terminal as Administrator.

If the file is in use: On Windows, you may get an error like "The process cannot access the file because it is being used by another process." Close any programs that might be using the file, then try again. On Linux/macOS, you might get a similar error if the file is open; close the application.

If you deleted the wrong file: Unfortunately, rm does not move to trash. If you have a backup or version control (like Git), restore from there. Some systems have file recovery tools, but they are not guaranteed to work. To avoid this in the future, use rm -i.

If the command seems to hang: It might be waiting for confirmation if you used -i. Check the terminal for a prompt like rm: remove regular file 'file.txt'? and type y or n.

Frequently Asked Questions

Can I undo a file deletion in terminal?

No, the rm command permanently deletes files. There is no built-in undo. You can restore from backup or version control if you have one.

What is the difference between rm and unlink?

unlink deletes a single file and is more limited—it cannot delete directories or use wildcards. rm is more powerful and commonly used.

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

Put the name in quotes: rm "my file.txt" or escape the space with a backslash: rm my\ file.txt.

Can I delete multiple files at once?

Yes, list them all: rm file1.txt file2.txt file3.txt. You can also use wildcards like rm *.log to delete all files with that extension in the current directory.

Does rm work on Windows?

In Command Prompt, use del instead. In PowerShell, rm works as an alias for Remove-Item. If you have WSL installed, you can use the Linux rm command inside the WSL terminal.

What does rm -rf do?

-r recursively deletes directories and their contents, and -f forces deletion without asking for confirmation. It is extremely dangerous—it can delete your entire system if run on the wrong path. Only use it when you are absolutely sure of what you're deleting.