How to delete files and directories in Terminal
When it comes to quickly taking care of daily tasks, the command line can be both powerful and dangerous. Take the commands in this article for example: rm
The command allows you to delete (or delete) files. The rmdir
The command does the same for directories (also called folders). But beware: unlike moving files to the Trash from Finder, there is no way to get them back if you use these commands.
Still, if you want to harness the powers of Terminal, this is a command you can’t ignore. I’ll show you how to add protection to make sure you’re only deleting the files that you really want to delete.
Why bother deleting files with the command line?
Deleting files with Finder isn’t too difficult, and you can always pull files out of the Trash if you change your mind. So why bother using the command line? Here are a few reasons:
- You can use wildcards to delete multiple files quickly and efficiently.
- You can delete files from the Recycle Bin when you encounter persistent errors.
- You can delete hidden files in Finder. These files, which may contain settings for some applications or parts of macOS, contain a period (.) Before their names and the Finder does not display them.
- If you’ve lost access to Finder because your Mac is about to flash, you might be able to use the command line to resolve the issue.
How to delete files
It is dangerously easy to delete files with the rm
order. Here is an example. After launching Terminal (in your / Applications / Utilities folder), type cd ~/Desktop
to access the Desktop directory. To delete a file, type rm filename
, substitute filename
with the actual name of the file you want to delete. (If you have a filename with spaces, you must enclose the name in quotes: "For Example.txt"
.) If you had a file here named MyFile.rtf that you never wanted to see again, you can run this command:
rm MyFile.rtf
When you press Return, the file will become 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 with one command. If you have three files on your desktop that you want to delete and you want to delete them all at once, you can do it like this (if you have a filename with spaces, you need to 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 bears repeating: this command removes files. It stirs them up. You can’t get them back. You cannot click on the Trash can icon and recover the files that you accidentally deleted.
But there is a safety net: it is the -i
flag (interactive). So if you are feeling cautious, you can run the above commands with this flag as follows:
rm -i MyFile.rtf
Or, if multiple files are deleted:
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In either case, pressing Return will not actually activate the rm
order, because the -i
flag acts as a pause button. You will see the following in Terminal when running these commands:
IDG
To continue, you must type yes
, or simply y
. In the case of multiple files, you will see a query for each file. Granted, it’s easy to get into the habit of typing quickly y
, but the question is meant to make you stop and think very carefully about whether you really want to delete this file.
How to delete empty directories (aka folders)
Deleting directories or folders is a little different. If you try to run the rm
on a directory, you will see the following message:

You cannot delete a directory using the rm command.
IDG
There is a special command to delete directories: rmdir
. So, to delete a directory named Archives, run this command (If you have a directory name with spaces, you need to put the name in quotes: "For Example"
.):
rmdir Archives
You cannot use the -i
flag with the rmdir
order, so ordering is a bit riskier.
Note that this command only removes empty directories. If you want to delete a directory and the files it contains, read on.
How to delete everything in a directory
The rm
command has a powerful option, -R
(Where -r
), otherwise known as the recursive option. When you run the rm -R
command on a folder, you tell Terminal to delete that folder, all the files in it, all the subfolders in it, and all the files or folders in those subfolders, all the way down. You enter the command as m -R directoryname
, where you replace directoryname
for the name of the directory you want to delete. (If you have a directory name with spaces, you must enclose the name in quotes: "For Example"
.)
For example, let’s say you have a directory full of archives, containing subdirectories and files. Remove each item individually from Finder Where the command line can take a long time. So just run the command like this:
rm -R Archives
Remember that this deletion is final. But you can use the -i
protection flag:
rm -iR Archives
This will ask you to confirm the deletion of each item. It can be annoying, but unless you’re really sure you want to delete all of these files, it’s probably best to be safe.
Can’t empty the trash in Finder? Use the terminal
When the rm -R
is the command useful? Suppose you cannot empty the Trash on your Mac. A file may be locked, or you may not be allowed to delete one or more files. This kind of problem is annoying, but you can use the command line to provide a simple solution.
In Terminal, type the following:
rm -R
Then type a space.
In Finder, open the Trash, then drag the items it contains to the Terminal window. You will see one or more files with paths such as /Users/.Trash/file.txt.
If there are a lot of files, you might find that the resulting list (all on one long line, wrapping around the Terminal window) can be very long. If you’re absolutely sure you want to delete all of these, hit Return. The terminal will empty the recycle bin. Command line victory!

IDG
You want to know more ? Check out our articles on navigating the file system with the command line, learning from man pages, and copying and moving files.
Comments are closed.