5

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.

2 Answers 2

5

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.

3
  • Can't other things be stored in the resource fork? If so, wouldn't xattr -d com.apple.ResourceFork myfile delete all such assets, including ones that aren't the icon?
    – Tom
    Apr 27, 2020 at 2:24
  • 1
    @Tom, In modern versions of macOS it's only used in this manner and can be safely deleted for what the OP needed to do. There are other extended attributes that one may not want to delete but they have a different name, e.g. com.apple.FinderInfo, com.apple.metadata:kMDItemDownloadedDate:, com.apple.metadata:kMDItemWhereFroms, etc. Apr 27, 2020 at 3:06
  • You can quote the pathname with spaces. You just need to remove the backslashes. It's either quoting, or escaping with backslashes, not both. "/some path/with spaces/" or /some\ path/with\ spaces/.
    – mivk
    Aug 1, 2022 at 18:48
1

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
3
  • Cool. Is there a way to make the icon remain in place if e.g. a bash script is edited and then saved in a text editor? Is the best solution to first extract the icon, then edit the file, and then insert the icon again after saving?
    – nisc
    Sep 26, 2021 at 15:20
  • @nisc I think editing the file using another app does not remove the custom icon normally. I do this stuff with my IDE's workspace files (which are just JSON files), and it seems like updating my workspace settings (which re-saves the workspace file) does not remove the custom icon. If your save operation rewrites the file from scratch (which some apps do), you may need a multi-step process like you describe.
    – Tom
    Sep 26, 2021 at 18:26
  • K. I've seen this both with vim and with Atom
    – nisc
    Sep 28, 2021 at 3:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .