When it comes to performing daily tasks quickly, the command line can be both powerful and dangerous. Take the commands in this article as an example: the rm command allows you to delete (or delete) files. The rmdir command does the same with directories (also called directories). But be careful: unlike when you move files from the Finder to the Recycle Bin, there is no way to get them back if you use these commands†
But if you want to harness the powers of Terminal, this is one command you can’t overlook. I’ll show you how to add a protection to make sure you only delete files that you really want to delete.
Why delete files from the command line?
Deleting files with the Finder isn’t that hard, and you can always fish files out of the trash if you change your mind. So why should you use the command line? Here are some reasons:
You can use wildcards to quickly and efficiently delete multiple files.
You can delete files from Recycle Bin if you encounter persistent errors.
You can delete files that are hidden in the Finder. These files, which may contain settings for certain apps or parts of macOS, contain a period (.) before their names, and the Finder won’t show them.
If you can’t access the Finder because your Mac is blinking, you may be able to use the command line to fix the problem.
How to delete files
It is dangerously easy to delete files with the rm order. Here’s an example. After you launch Terminal (located in the /Applications/Utilities folder) type cd ~/Desktop to navigate to the Desktop folder. To delete a file, type rm filenamesubstitute filename with the actual name of the file you want to delete. (If you have a filename with spaces, put the name in quotes: "For Example.txt".) If you had a file here called MyFile.rtf that you never, ever wanted to see again, you could run this command:
When you press return, the file will be poof! The Mac does not confirm whether you want to delete the file. It will be gone, toast, history. You can’t get it back.
You can even delete multiple files in one command. If you have three files on your desktop that you want to delete, and you want to delete them all at once, here’s how to do it (if you have a filename with spaces, put the name in quotes: "For Example.txt"†
rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
Again, hitting the Return key does the dirty work.
It’s worth repeating: this command deletes files. It destroys them. You won’t get them back. You cannot click the trash can icon and retrieve files that you have accidentally deleted.
But there is a safety net: it is the -i (interactive) flag. So if you are feeling careful, you can run the above commands with this flag like this:
In any case, pressing Return won’t actually get the . activate rm command, because the -i flag acts as a pause button. You will see the following in Terminal when you run these commands:
To continue, you must type yesor just y† In case of multiple files, you will see one search for each file. Granted, it’s easy to get into the habit of typing fast ybut the question is meant to make you stop and think very carefully about whether you really want to delete that file.
How to delete empty folders (also called folders)
Deleting folders or folders is a bit different. If you try to rm command in a folder, you will see the following message:
There is a special command for deleting folders: rmdir† So to delete a folder called Archives, run this command (if you have a folder name with spaces, put the name in quotes: "For Example"†
You cannot use the -i flag with the rmdir command, so the command is a bit riskier.
Note that this command only removes empty directories. If you want to delete a folder and the files it contains, read on.
How to delete everything in a folder
The rm command has a powerful option, -R (or -r), also known as the recursive option. When you use the rm -R command on a folder, tells Terminal to delete that folder, all files it contains, all subfolders it contains, and all files or folders in those subfolders, all the way to the bottom. You enter the command as m -R directorynamewhere you replace directoryname for the name of the folder you want to delete. (If you have a directory name with spaces, put the name in quotes: "For Example"†
For example, let’s say you have a folder full of archives, containing subfolders and files. Remove each item individually from the Finder or the command line can take a long time. So just run the command like this:
rm -R Archives
Keep in mind that this removal is permanent. But you can de -i flag for protection:
rm -iR Archives
This will prompt you to confirm the deletion of each item. This can be annoying, but unless you’re really sure you want to delete all those files, it’s probably best to be safe.
Can’t Empty Recycle Bin in Finder? Use the terminal
When can the rm -R command come in handy? Let’s say you can’t empty the Trash on your Mac. A file may be locked or you may not have permission to delete one or more files. This kind of glitch is annoying, but you can use the command line to provide a simple solution.
Type the following into Terminal:
Then type a space.
Open the trash can in the Finder and drag the items in it to the Terminal window. You will see one or more files with paths such as /Users/.Trash/file.txt.
If there are many files, the resulting list – all on one long line, enclosed in the Terminal window – can be very long. If you’re absolutely sure you want to delete all of these items, press Return. Terminal will empty the Recycle Bin. Command Line Win!
Knowing more? Check out our articles on navigating the file system using the command line, learning from man pages, and copying and moving files.