Relating to shortly caring for each day duties, the command line will be each highly effective and harmful. Take the instructions on this article for instance: the rm command permits you to take away (or delete) information. The rmdir command does the identical to directories (also referred to as folders). However watch out: In contrast to while you transfer information to the Trash from the Finder, there’s no approach to get them again should you use these instructions.
Nonetheless, if you wish to faucet into Terminal’s powers in macOS Sonoma or older variations, this can be a command you may’t overlook. I’ll present you the right way to add a safeguard to make sure that you solely delete information you actually need to delete.
Why hassle deleting information with the command line?
Deleting information with the Finder isn’t too troublesome, plus you may all the time fish information out of the Trash should you change your thoughts. So why hassle utilizing the command line? Listed below are some causes:
- You should use wildcards to delete a number of information shortly and effectively.
- You possibly can take away information from the Trash while you encounter cussed errors.
- You possibly can delete information which can be hidden within the Finder. These information, which may comprise settings for sure apps or elements of macOS, comprise a dot (.) earlier than their names and the Finder doesn’t present them.
- In the event you’ve misplaced entry to the Finder as a result of your Mac is on the blink, you may be capable to use the command line to troubleshoot the issue.
delete information
It’s dangerously straightforward to delete information with the rm command. Right here’s an instance. After you launch Terminal (in your /Functions/Utilities folder) kind cd ~/Desktop to navigate to the Desktop listing. To delete a file on the Desktop, kind rm filename, changing filename with the precise title of the file you need to delete. (You probably have a file title with areas, you could put the title in quotes: "For Instance.txt".) In the event you had a file right here named MyFile.rtf that you simply by no means, ever needed to see once more, you can run this command:
rm MyFile.rtf
Whenever you press Return, the file will go poof! The Mac doesn’t affirm if you wish to delete the file. Will probably be gone, toast, historical past. You possibly can’t get it again.
You possibly can even delete a number of information in a single command. You probably have three information in your Desktop that you simply need to delete, and also you need to delete them unexpectedly, you are able to do so like this (if in case you have a file title with areas, you could put the title in quotes: "For Instance.txt".):
rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
Once more, urgent the Return key does the soiled work.
A security web
It’s value repeating: this command deletes information. It nukes them. You possibly can’t get them again. You possibly can’t click on on the Trash icon and retrieve information you’ve by chance deleted.
However there’s a security web: it’s the -i (interactive) flag. So should you’re feeling cautious, you can run the above instructions with this flag as follows:
rm -i MyFile.rtfOr, within the case of deleting a number of information:
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In every case, urgent Return gained’t truly activate the rm command, as a result of the -i flag acts as a pause button. You’ll see the next in Terminal when operating these instructions:

IDG
To be able to proceed, you could kind sure, or just y. Within the case of a number of information, you’ll see one question for every file. Granted, it’s straightforward to get into the behavior of shortly typing y, however the query is meant to make you cease and assume very rigorously about whether or not you actually need to delete that file.
delete empty directories (a.ok.a folders)
Deleting directories, or folders, is a bit totally different. In the event you attempt to run the rm command on a listing, you’ll see the next message:

You possibly can’t delete a listing utilizing the rm command.
IDG
There’s a particular command for deleting directories: rmdir. So to delete a listing named Archives, run this command (You probably have a listing title with areas, you could put the title in quotes: "For Instance".):
rmdir Archives
You possibly can’t use the -i flag with the rmdir command, so the command is a bit riskier.
Notice that this command solely deletes empty directories. If you wish to delete a listing and the information it accommodates, learn on.
delete every little thing in a listing
The rm command has a robust possibility, -R (or -r), in any other case referred to as the recursive possibility. Whenever you run the rm -R command on a folder, you’re telling Terminal to delete that folder, any information it accommodates, any sub-folders it accommodates, and any information or folders in these sub-folders, all the best way down. You enter the command as rm -R directoryname, the place you substitute directoryname for the title of the listing you need to delete. (You probably have a listing title with areas, you could put the title in quotes: "For Instance".)
For instance, let’s say you could have a listing filled with archives, containing sub-directories and information. Deleting every merchandise individually from the Finder or the command line can take a very long time. So simply run the command like this:
rm -R Archives
Keep in mind, this deletion is ultimate. However you need to use the -i flag for defense:
rm -iR Archives
This can ask you to substantiate the deletion of every merchandise. This may be annoying, however except you’re actually positive you need to delete all these information, it’s most likely greatest to be protected.
Can’t empty Trash within the Finder? Use the Terminal
When can the rm -R command turn out to be useful? Say you may’t empty the Trash in your Mac. A file is likely to be locked or you might not have permission to delete a number of information. This kind of glitch is annoying, however you need to use the command line to supply a straightforward answer.
In Terminal, kind the next:
rm -R
Then kind an area.
Within the Finder, open the Trash, after which drag the gadgets it accommodates to the Terminal window. You’ll see a number of information with paths reminiscent of /Customers/.Trash/file.txt.
If there are many information, you might discover that the ensuing record—all on one lengthy line, wrapping within the Terminal window—could also be very lengthy. In the event you’re completely positive that you simply need to delete all these things, press Return. Terminal will empty the Trash. Command line win!

IDG
Need to be taught extra? See our articles about navigating the file system with the command line, studying from man pages, and copying and transferring information.