Terminal command to remove custom icon from file (HFS+)

Solution 1:

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.

Solution 2:

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