top7
How to remove files and directories in Terminal
Published
1 month agoon
By
admin#take away #recordsdata #directories #Terminal
Relating to shortly caring for each day duties, the command line might be each highly effective and harmful. Take the instructions on this article for example: the rm
command means that you can take away (or delete) recordsdata. The rmdir
command does the identical to directories (additionally know as folders). However watch out: In contrast to while you transfer recordsdata to the Trash from the Finder, there’s no technique to get them again should you use these instructions.
Nonetheless, if you wish to faucet into Terminal’s powers, it is a command you may’t overlook. I’ll present you add a safeguard to make sure that you solely delete recordsdata you actually wish to delete.
Why hassle deleting recordsdata with the command line?
Deleting recordsdata with the Finder isn’t too troublesome, plus you may all the time fish recordsdata 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 recordsdata shortly and effectively.
- You may take away recordsdata from the Trash while you encounter cussed errors.
- You may delete recordsdata which are hidden within the Finder. These recordsdata, which might comprise settings for sure apps or components of macOS, comprise a dot (.) earlier than their names and the Finder doesn’t present them.
- For those who’ve misplaced entry to the Finder as a result of your Mac is on the blink, you would possibly be capable of use the command line to troubleshoot the issue.
The way to delete recordsdata
It’s dangerously straightforward to delete recordsdata with the rm
command. Right here’s an instance. After you launch Terminal (in your /Purposes/Utilities folder) sort cd ~/Desktop
to navigate to the Desktop listing. To delete a file, sort rm filename
, changing filename
with the precise title of the file you wish to delete. (If in case you have a file title with areas, you want to put the title in quotes: "For Instance.txt"
.) For those who had a file right here named MyFile.rtf that you just by no means, ever wished to see once more, you would run this command:
rm MyFile.rtf
While you press Return, the file will go poof! The Mac doesn’t verify if you wish to delete the file. Will probably be gone, toast, historical past. You may’t get it again.
You may even delete a number of recordsdata in a single command. If in case you have three recordsdata in your Desktop that you just wish to delete, and also you wish to delete them unexpectedly, you are able to do so like this (in case you have a file title with areas, you want to 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.
It’s price repeating: this command deletes recordsdata. It nukes them. You may’t get them again. You may’t click on on the Trash icon and retrieve recordsdata you’ve by chance deleted.
However there’s a security internet: it’s the -i
(interactive) flag. So should you’re feeling cautious, you would run the above instructions with this flag as follows:
rm -i MyFile.rtf
Or, within the case of deleting a number of recordsdata:
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In every case, urgent Return received’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 working these instructions:

IDG
To be able to proceed, you want to sort sure
, or just y
. Within the case of a number of recordsdata, 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 fastidiously about whether or not you actually wish to delete that file.
The way to delete empty directories (a.ok.a folders)
Deleting directories, or folders, is a bit totally different. For those who attempt to run the rm
command on a listing, you’ll see the next message:

You may’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 (If in case you have a listing title with areas, you want to put the title in quotes: "For Instance"
.):
rmdir Archives
You may’t use the -i
flag with the rmdir
command, so the command is a bit riskier.
Be aware that this command solely deletes empty directories. If you wish to delete a listing and the recordsdata it accommodates, learn on.
The way to delete all the pieces in a listing
The rm
command has a robust choice, -R
(or -r
), in any other case referred to as the recursive choice. While you run the rm -R
command on a folder, you’re telling Terminal to delete that folder, any recordsdata it accommodates, any sub-folders it accommodates, and any recordsdata or folders in these sub-folders, all the way in which down. You enter the command as m -R directoryname
, the place you substitute directoryname
for the title of the listing you wish to delete. (If in case you have a listing title with areas, you want to put the title in quotes: "For Instance"
.)
For instance, let’s say you will have a listing stuffed with archives, containing sub-directories and recordsdata. 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
Bear in mind, this deletion is closing. However you should utilize the -i
flag for defense:
rm -iR Archives
This can ask you to verify the deletion of every merchandise. This may be annoying, however except you’re actually positive you wish to delete all these recordsdata, it’s most likely finest to be secure.
Can’t empty Trash within the Finder? Use the Terminal
When can the rm -R
command come in useful? Say you may’t empty the Trash in your Mac. A file may be locked or you could not have permission to delete a number of recordsdata. This kind of glitch is annoying, however you should utilize the command line to supply a simple resolution.
In Terminal, sort the next:
rm -R
Then sort an area.
Within the Finder, open the Trash, after which drag the objects it accommodates to the Terminal window. You’ll see a number of recordsdata with paths resembling /Customers/.Trash/file.txt.
If there are many recordsdata, you could discover that the ensuing record—all on one lengthy line, wrapping within the Terminal window—could also be very lengthy. For those who’re completely positive that you just wish to delete all this stuff, press Return. Terminal will empty the Trash. Command line win!

IDG
Need to study extra? See our articles about navigating the file system with the command line, learning from man pages, and copying and moving files.
Related
You may like
-
Gabby Petito family files $50 million wrongful death lawsuit against Utah police
-
Cuba Battles to Contain Major Blaze at Fuel-Storage Terminal
-
How to Upload Files Over FTP With PowerShell
-
Florida python challenge brings competitors to Everglades to remove invasive snakes
-
Ne-Yo’s wife files for divorce, alleges he fathered a child
-
Monica Lewinsky wants Beyoncé to remove lyric about her from ‘Partition’