how to force delete a file mac
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 as an example: the rm
command allows you to remove (or delete) files. The rmdir
command does the same to directories (also know as folders). But be careful: Unlike when you move files to the Trash from the Finder, there's no way to get them back if you use these commands.
Still, if you want to tap into Terminal's powers, this is a command you can't overlook. I'll show you how to add a safeguard to ensure that you only delete files you really want to delete.
Why bother deleting files with the command line?
Deleting files with the Finder isn't too difficult, plus you can always fish files out of the Trash if you change your mind. So why bother using the command line? Here are some reasons:
- You can use wildcards to delete multiple files quickly and efficiently.
- You can remove files from the Trash when you encounter stubborn errors.
- You can delete files that are hidden in the Finder. These files, which can contain settings for certain apps or parts of macOS, contain a dot (.) before their names and the Finder doesn't show them.
- If you've lost access to the Finder because your Mac is on the blink, you might be able to use the command line to troubleshoot the problem.
How to delete files
It's dangerously easy to delete files with the rm
command. Here's an example. After you launch Terminal (in your /Applications/Utilities folder) type cd ~/Desktop
to navigate to the Desktop directory. To delete a file, type rm filename
, replacing filename
with the actual name of the file you want to delete. (If you have a file 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 wanted to see again, you could run this command:
rm MyFile.rtf
When you press Return, the file will go poof! The Mac doesn't confirm if 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 a single 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 so like this (if you have a file name with spaces, you need to put the name in quotes: "For Example.txt"
.):
rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
Again, pressing the Return key does the dirty work.
It's worth repeating: this command deletes files. It nukes them. You can't get them back. You can't click on the Trash icon and retrieve files you've accidentally deleted.
But there is a safety net: it's the -i
(interactive) flag. So if you're feeling cautious, you could run the above commands with this flag as follows:
rm -i MyFile.rtf
Or, in the case of deleting multiple files:
rm -i MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf
In each case, pressing Return won't actually activate the rm
command, because the -i
flag acts as a pause button. You'll see the following in Terminal when running these commands:
IDG
In order to proceed, you need to type yes
, or simply y
. In the case of multiple files, you'll see one query for each file. Granted, it's easy to get into the habit of quickly typing y
, 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 different. If you try to run the rm
command on a directory, you'll see the following message:
IDG
There's a special command for deleting 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 can't use the -i
flag with the rmdir
command, so the command is a bit riskier.
Note that this command only deletes 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
(or -r
), otherwise known as the recursive option. When you run the rm -R
command on a folder, you're telling Terminal to delete that folder, any files it contains, any sub-folders it contains, and any files or folders in those sub-folders, all the way down. You enter the command as m -R directoryname
, where you substitute directoryname
for the name of the directory you want to delete. (If you have a directory name with spaces, you need to put the name in quotes: "For Example"
.)
For example, let's say you have a directory full of archives, containing sub-directories and files. Deleting 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
Remember, this deletion is final. But you can use the -i
flag for protection:
rm -iR Archives
This will ask 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 Trash in the Finder? Use the Terminal
When can the rm -R
command come in handy? Say you can't empty the Trash on your Mac. A file might be locked or you may not have permission to delete one or more files. This sort of glitch is annoying, but you can use the command line to provide an easy solution.
In Terminal, type the following:
rm -R
Then type a space.
In the Finder, open the Trash, and then drag the items it contains to the Terminal window. You'll see one or more files with paths such as /Users/.Trash/file.txt.
If there are lots of files, you may find that the resulting list—all on one long line, wrapping in the Terminal window—may be very long. If you're absolutely sure that you want to delete all these items, press Return. Terminal will empty the Trash. Command line win!
IDG
Want to learn more? See our articles about navigating the file system with the command line, learning from man pages, and copying and moving files.
Note: When you purchase something after clicking links in our articles, we may earn a small commission. Read our affiliate link policy for more details.
Kirk McElhearn (@mcelhearn) writes about Macs, music and more on his blog Kirkville. He also runs Kirk's iTunes Forum, where users can discuss iTunes, iOS devices, music, and more.
how to force delete a file mac
Source: https://www.macworld.com/article/222596/command-line-deleting-files-folders-mac-terminal.html
Posted by: grantcarch1952.blogspot.com
0 Response to "how to force delete a file mac"
Post a Comment