The basic command to delete a file
To delete a file in the terminal, type rm followed by the filename, then press Enter. For example, if you want to delete a file called old_notes.txt, you would type rm old_notes.txt and press Enter. The file disappears when ready — there is no trash or recycle bin to recover it from.
The rm command works the same way on Mac, Linux, and Windows (if you are using Windows Subsystem for Linux or a Linux terminal). On Windows PowerShell, the command is slightly different: you would type Remove-Item old_notes.txt instead. This guide covers the rm command, which is the standard across most systems.
Before you delete anything, make sure you are in the correct folder. Type pwd (print working directory) to see where you are, and ls to list the files in that folder. This prevents you from accidentally deleting the wrong file.
Key Takeaways
- The command rm filename deletes a file permanently with no recovery option, so double-check the filename before you press Enter.
- Use pwd to confirm which folder you are in and ls to see the files before you delete anything.
- To delete multiple files at once, type rm file1.txt file2.txt file3.txt or use a wildcard like rm *.txt to delete all files with a certain extension.
- The -i flag asks for confirmation before deleting: type rm -i filename to add a safety step.
- To delete a folder and everything inside it, use rm -r foldername, but only when you are certain you want to remove the entire folder.
Deleting multiple files at once
If you need to delete several files, you can name them all in one command. Type rm file1.txt file2.txt file3.txt and press Enter. The terminal deletes all three files in a single action.
You can also use a wildcard to delete files that match a pattern. For example, rm *.txt deletes every file in the current folder that ends with .txt. The asterisk means "any characters before this extension." This is fast but dangerous — if you type rm * by itself, you will delete every file in the folder. Always type out the pattern carefully and use ls first to see exactly which files match.
Adding a confirmation step before deletion
Because deleted files cannot be recovered, the terminal offers a safety flag called -i (interactive mode). Type rm -i filename and the terminal will ask "remove filename?" before actually deleting it. You type y for yes or n for no, then press Enter.
This is especially useful when you are using wildcards. Type rm -i *.txt and the terminal will ask about each file one at a time. You can say yes to some and no to others. This takes longer but prevents accidents when you are deleting many files at once.
Deleting folders and everything inside them
To delete a folder, you need the -r flag, which stands for "recursive" — it deletes the folder and all files and subfolders inside it. Type rm -r foldername and press Enter. The entire folder vanishes.
This is powerful and dangerous. Before you use rm -r, navigate into the parent folder (the folder that contains the one you want to delete) and type ls to see it listed. Make absolutely sure you have the right folder name. You can combine the safety flag with the recursive flag: rm -ri foldername will ask for confirmation before deleting the folder and its contents.
Understanding why deleted files cannot be recovered
When you delete a file in the terminal using rm, the operating system removes the file's entry from the folder listing when ready. The space on your hard drive is marked as available for new files, but the data itself may still be there for a short time until something else overwrites it.
Unlike the graphical trash or recycle bin, the terminal does not move files to a holding area first. Deletion is final. If you realize you made a mistake seconds after pressing Enter, you may be able to recover the file using specialized recovery software, but this is not may provide and becomes less likely the longer you wait or the more you use your computer afterward.
Checking what you are about to delete
Before you type any delete command, use ls -la to see a detailed list of files in your current folder. The -la flags show hidden files and extra information like file size and modification date. This helps you confirm you are looking at the right file before you delete it.
If you are deleting with a wildcard, type the wildcard as part of an ls command first. For example, if you plan to type rm *.log, first type ls *.log to see exactly which files will be deleted. This takes five seconds and can prevent hours of regret.
Common mistakes and how to avoid them
The most common mistake is deleting from the wrong folder. Always type pwd before you delete anything to confirm where you are. The second mistake is using rm * when you meant to delete files with a specific extension — the asterisk alone matches everything, not just what you intended.
Another mistake is forgetting the -r flag when trying to delete a folder. If you type rm foldername without -r, the terminal will refuse and show an error message saying "is a directory." This is actually helpful — it stops you from accidentally deleting a folder. Add the -r flag and try again only if you are certain.
Frequently Asked Questions
Can I undo a delete command after I press Enter?
No. Once you press Enter, the file is deleted and cannot be brought back through the terminal. If you realize when ready that you made a mistake, you might recover the file using recovery software, but this is not may provide. Always use ls to check before you delete, and use the -i flag for confirmation when you are unsure.
What does the error "permission denied" mean when I try to delete?
This means the file or folder is owned by a different user or protected by the system. You may need to use sudo (superuser do) to delete it, but only do this if you are certain the file should be deleted. Type sudo rm filename and enter your password when prompted. Never use sudo with wildcards unless you are very sure what you are deleting.
How do I delete a file with spaces in its name?
Put the filename in quotes. For example, if the file is called my old notes.txt, type rm "my old notes.txt". Without quotes, the terminal treats each word as a separate filename and tries to delete three files: my, old, and notes.txt.
Is there a way to delete files safely without losing them forever?
You can move files to a designated trash folder instead of deleting them permanently. Type mv filename ~/.Trash on Mac or mv filename ~/Trash on Linux. This moves the file to a folder you can empty later. Some users create their own "archive" folder for files they might need someday before deleting them permanently.
What is the difference between rm and rmdir?
The rmdir command deletes only empty folders. If a folder contains any files, rmdir will refuse to delete it. Use rm -r to delete a folder and everything inside it. rmdir is safer because it forces you to empty a folder first, giving you a chance to see what you are about to lose.