Context

In Mac OS X / macOS you can select a file in the Finder, click Informations then click the icon, and paste any image/icon that is in the clipboard. Now this file has a custom icon made of this image.

To remove it, the usual way is to select the file, click Informations, select the custom icon then hit delete and it removes the custom icon, restoring the default one.

Question

Is there a way to remove these custom icons from these files without having to open a Finder window? For example with a Terminal command?

Why

A file has a corrupted custom icon, opening the Finder in this folder causes the Finder to beachball and the HDD to freak out. But I still can access the folder content, and this file, from the command line, if I don't first open a Finder window. So I'm thinking that I "just" need to access the metadata differently to remove the custom icon.

有帮助吗?

解决方案

For files with a custom icon, macOS writes the icon data as a resource fork to the file. Use the xattr command in the following form:

xattr -d com.apple.ResourceFork /path/to/filename

For more information about the xattr command, in Terminal type the following command and press: enter

man xattr

Or just type xattr and then right-click on it and select: Open man Page


For folders with custom icon, in Terminal, change directory to the folder containing the custom icon, e.g.:

cd /path/to/foo

Once in the directory, use the following command to remove the custom icon:

rm Icon?

Note that you can also just use:

rm /path/to/foo/Icon?

However, you cannot use quotes, single or double, if the pathname has spaces and must escape the spaces with a backslash, \, e.g.:

rm /path/to/foo\ bar/Icon?

Quoting the pathname will just return the following error:

: No such file or directory

By default, Icon? is a hidden file and the ? in Icon? is actually a linefeed, \n, character.

其他提示

An alternative is to use SetFile to flip the file's "custom icon" bit off:

SetFile -a c /path/to/file

One difference between this and xattr -d com.apple.ResourceFork is the xattr -d destroys the image data in the file's resource fork, whereas SetFile leaves the data in place. If you leave the data in place, you can restore the custom icon later, like so:

SetFile -a C /path/to/file # note the lettercase

If you want, you can extract the custom icon into a separate file before removing it completely from the file:

DeRez -only icns /path/to/file > extracted.rsrc

(Note that this copies the data out. You'd still need to delete the embedded icon using xattr.)

I should also note that xattr -d leaves the "custom icon" bit set on, meaning that if you ever insert new icns data into the file's resource fork, it will show through immediately. So, I think if you really want to make the file as vanilla as possible, you should do both: delete the fork, and unset the custom bit. (But I really don't know if it matters.)

(BTW, you can insert custom icon data like so:)

Rez -append icon.rsrc -o /path/to/file
许可以下: CC-BY-SA归因
不隶属于 apple.stackexchange
scroll top