Delete a single file with the rm command

To delete a file in terminal, type rm followed by the filename, then press Enter. For example, if you want to delete a file called "notes.txt", you would type rm notes.txt and press Enter. The file disappears immediately — there is no trash or recycle bin to recover it from.

The filename must be exactly as it appears, including capitalization and the file extension. If the filename has spaces in it, put the whole name in quotes: rm "my document.txt". If you are not sure of the exact name, you can type the first few letters and press Tab to auto-complete.

Before you delete, make sure you are in the right folder. Type pwd to see which folder you are currently in, and type ls to see what files are in that folder. This prevents you from accidentally deleting the wrong file.

Key Takeaways

  • The rm command deletes a file permanently — it does not go to trash, so you cannot undo it.
  • Type the filename exactly as it appears, including capitalization and spaces (use quotes around names with spaces).
  • Always check which folder you are in with pwd and what files are there with ls before deleting.
  • To delete multiple files at once, list them after rm separated by spaces, or use a wildcard like rm *.txt to delete all files with a certain extension.
  • If a file is write-protected, terminal will ask for confirmation before deleting it.

Delete multiple files at once

If you need to delete several files, you can list them all after the rm command separated by spaces. For example, rm file1.txt file2.txt file3.txt deletes all three files in one command.

You can also use a wildcard to delete files that match a pattern. The asterisk * stands for "any characters". For example, rm *.txt deletes every file in the current folder that ends with .txt. Be very careful with wildcards — they delete everything that matches, and you cannot undo it.

If you want to see which files will be deleted before you actually delete them, type ls *.txt first to preview. This shows you exactly what the wildcard will match, so you can be sure before you run the delete command.

Delete a file in a different folder

If the file is in a different folder, you need to include the path to that folder. For example, if you want to delete a file called "old.txt" that is in a folder called "Documents", type rm Documents/old.txt.

If the folder is nested deeper, keep adding folder names separated by slashes. For example, rm Documents/Projects/archive/old.txt deletes the file "old.txt" from the archive folder inside Projects inside Documents.

You can also navigate to the folder first using the cd command, then delete the file. Type cd Documents to move into the Documents folder, then type rm old.txt. Type cd .. to go back up one level if you need to.

What to do if the file is write-protected

Some files are marked as read-only or write-protected so they cannot be accidentally deleted. When you try to delete one, terminal will ask "remove write-protected regular file?" and wait for you to answer.

Type y and press Enter to delete it anyway, or type n and press Enter to cancel. If you do not want to be asked each time, you can use the -f flag to force deletion without asking: rm -f filename.txt. Use this carefully — it bypasses the safety check.

Recover a deleted file

Once you delete a file with rm, it is gone from the terminal's perspective. However, the data may still exist on your hard drive until something else overwrites that space. Recovery is possible but requires special tools and is not may provide to work.

If you just deleted a file and have not done much else on your computer since, you can try recovery software like TestDisk (free, works on Mac and Linux) or Recuva (free, works on Windows). These tools scan your hard drive for deleted files and may be able to restore them. The sooner you try, the better your chances.

The safest approach is to never delete something you might need later. If you are unsure about a file, move it to an "archive" or "old" folder instead of deleting it. You can always delete it later once you are certain.

Delete an empty folder

To delete an empty folder, use the rmdir command instead of rm. For example, rmdir old_folder deletes the folder called "old_folder", but only if it is completely empty.

If the folder has files in it, rmdir will not work. You have to delete the files first, then delete the folder. If you want to delete a folder and everything inside it in one command, use rm -r folder_name. The -r flag means "recursive" — it deletes the folder and all its contents. Be extremely careful with this command because it deletes everything without asking.

Common mistakes and how to avoid them

The most common mistake is deleting the wrong file because you were in the wrong folder or typed the name wrong. Always run pwd and ls before you delete anything. Take two seconds to confirm you are in the right place and the filename is correct.

Another mistake is using a wildcard too broadly. For example, rm * deletes every file in the current folder. rm -r * deletes every file and folder. These commands are dangerous and easy to type by accident. If you use wildcards, always preview with ls first.

A third mistake is deleting a file you need later because you thought you would not. If you are not 100% certain you will never need something again, do not delete it. Storage is cheap. Recovering a deleted file is hard. Keep it.

Frequently Asked Questions

Can I undo a delete in terminal?

No. Once you press Enter after typing rm, the file is deleted permanently. There is no undo command in terminal. The file does not go to trash — it is gone. This is why you must be careful before you delete.

What does the -i flag do?

The -i flag makes terminal ask for confirmation before deleting each file. Type rm -i filename.txt and terminal will ask "remove regular file 'filename.txt'?" before it deletes. This is a safety tool if you are nervous about deleting.

How do I delete a file with a special character in its name?

Put the filename in single or double quotes. For example, if the file is called "my file (1).txt", type rm "my file (1).txt". The quotes tell terminal to treat everything inside as the filename, including spaces and special characters.

Can I delete a file I do not have permission to delete?

No, unless you use sudo. If you do not own the file or do not have write permission on the folder, terminal will refuse to delete it. You can try sudo rm filename.txt, but this requires your password and should only be used when you are certain you need to.

What is the difference between rm and rmdir?

rm deletes files. rmdir deletes empty folders only. If you try to use rmdir on a folder with files in it, it will fail. If you want to delete a folder and everything inside, use rm -r folder_name instead.