How to Delete a File in Terminal
In this article
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
- 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).
- Navigate to the directory containing the file (if needed). Use
cdto change directories. For example:cd Documents. To check where you are, typepwd(print working directory). - List the files to confirm the exact name. Type
ls(on Linux/macOS) ordir(on Windows Command Prompt) to see the files. This helps avoid typos and ensures you're deleting the right file. - Run the
rmcommand 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. - Press Enter. The file is deleted immediately. There is no confirmation prompt unless you use the
-iflag (see Tips). - Verify the deletion (optional). Run
lsagain 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
rmon a directory without the-rflag. This gives an error likerm: cannot remove 'folder': Is a directory. To delete a directory and its contents, userm -r folder(be very careful—this is irreversible). - Forgetting that
rmbypasses the trash. There is no undo. Once deleted, the file is gone. - Using wildcards carelessly. For example,
rm *.txtdeletes all .txt files in the current directory. Double-check the pattern before running. - Running
rmas root (sudo) when not needed—this can delete system files if you make a mistake.
Tips
- Use the
-iflag for safety:rm -i file.txtwill ask for confirmation before deleting. - Use
-vto see what's being deleted:rm -v file.txtprints the name of each file as it's removed. - Use absolute paths when in doubt:
rm /home/username/Documents/report.pdfavoids 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
delcommand works similarly:del file.txt. PowerShell also supportsRemove-Itemordel. - 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.