When it come to promptly lead charge of day-by-day task , the bidding communication channel can be both muscular and severe . Take the instruction in this article as an exemplar : thermcommand allows you to take out ( or delete ) file . Thermdircommand does the same to directories ( also know as folders ) . But be thrifty : Unlike when you move files to the Trash from the Finder , there ’s no fashion to get them back if you use these instruction .
Still , if you want to bug into Terminal ’s office in macOS Sonoma or older versions , this is a command you ca n’t overlook . I ’ll show you how to lend a precaution to assure that you only delete files you really need to delete .
Why bother deleting files with the command line?
Deleting file with the Finder is n’t too unmanageable , plus you’re able to always fish files out of the Trash if you change your intellect . So why annoy using the command line ? Here are some reasons :
How to delete files
It ’s dangerously easy to delete files with thermcommand . Here ’s an example . After you launch Terminal ( in your /Applications / Utilities folder ) typecd ~/Desktopto navigate to the Desktop directory . To erase a file on the Desktop , typerm computer file name , replacingfilenamewith the actual name of the filing cabinet you desire to delete . ( If you have a file cabinet name with spaces , you need to put the name in quotes:“For Example.txt " . ) If you had a file here named MyFile.rtf that you never , ever want to see again , you could go this command :
rm MyFile.rtf
When you campaign Return , the file cabinet will go fagot ! The Mac does n’t affirm if you require to delete the file . It will be gone , toast , history . You ca n’t get it back .
You can’t delete a directory using the rm command.
you could even delete multiple filing cabinet in a single bidding . If you have three files on your Desktop that you want to delete , and you want to delete them all at once , you could do so like this ( if you have a file name with spaces , you require to put the name in quotes:“For Example.txt " . ):
rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
Again , squeeze the Return key does the dirty employment .
You can’t delete a directory using the rm command.
A safety net
It ’s worth retell : this command cancel file . It nuke them . You ca n’t get them back . You ca n’t come home on the Trash picture and retrieve files you ’ve incidentally edit .
But there is a safety equipment net : it ’s the - i(interactive ) iris . So if you ’re feeling conservative , you could unravel the above command with this iris as survey :
Or , in the case of edit multiple single file :
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In each case , compact Return wo n’t actually trigger thermcommand , because the - iflag acts as a pause button . You ’ll see the following in Terminal when running these bid :
IDG
for proceed , you require to typeyes , or simplyy . In the case of multiple single file , you ’ll see one query for each file . yield , it ’s easy to get into the habit of quick typingy , but the question is intended to make you stop and think very carefully about whether you really want to delete that file .
How to delete empty directories (a.k.a folders)
Deleting directories , or folders , is a bit dissimilar . If you essay to run thermcommand on a directory , you ’ll see the following subject matter :
You ca n’t cancel a directory using the rm command .
There ’s a special command for deleting directory : rmdir . So to delete a directory identify Archives , run this program line ( If you have a directory name with distance , you need to put the name in quotes:“For Example " . ):
rmdir Archives
You ca n’t expend the - iflag with thermdircommand , so the command is a bit hazardous .
observe that this bidding only deletes empty directory . If you want to delete a directoryandthe files it contains , read on .
How to delete everything in a directory
Thermcommand has a muscular option,-R(or - universal gas constant ) , otherwise cognize as the recursive option . When you run therm -Rcommand on a folder , you ’re telling Terminal to delete that booklet , any filing cabinet it contains , any hoagie - folders it take , and any files or folders in those sub - folders , all the manner down . You inscribe the bidding asrm - gas constant directoryname , where you substitutedirectorynamefor the name of the directory you need to delete . ( If you have a directory name with spaces , you need to put the name in quotes:“For Example " . )
For model , let ’s say you have a directory full of archives , contain Italian sandwich - directories and files . erase each item severally from the Finderorthe mastery line of descent can take a long time . So just execute the command like this :
rm -R Archives
Remember , this cut is final . But you may use the - iflag for protective covering :
rm -iR Archives
This will ask you to substantiate the deletion of each item . This can be galling , but unless you ’re really sure you want to delete all those files , it ’s probably best to be dependable .
Can’t empty Trash in the Finder? Use the Terminal
When can therm -Rcommand come in ready to hand ? Say you ca n’t empty the Trash on your Mac . A data file might be lock in or you may not have permission to erase one or more files . This sort of bug is bothersome , but you may use the command line to bring home the bacon an wanton solution .
In Terminal , typewrite the following :
rm -R
Then type a infinite .
In the Finder , afford the Trash , and then dredge the token it contains to the Terminal window . You ’ll see one or more file with paths such as /Users/.Trash / file.txt .
If there are lots of file , you may find that the resulting list — all on one tenacious line , twine in the Terminal windowpane — may be very long . If you ’re absolutely certain that you want to blue-pencil all these point , press Return . Terminal will empty the Trash . instruction line profits !
Want to learn more ? See our articles aboutnavigating the file system with the mastery channel , watch from man pages , andcopying and moving files .