Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

How can I change or set the icon for a file or folder, in the terminal, do I need to use a scripting language?

share|improve this question
See also sveinbjorn.org/osxiconutils –  user588 May 12 '12 at 9:36

5 Answers 5

up vote 8 down vote accepted

I almost started a bounty on this, because I didn't manage to change the icon of a folder using @mankoff's answer. But I found a solution.

To change folder's icon you don't point Rez -append tmp.rsrc to the folder but a special Icon\r file inside the folder. If you haven't set a custom icon to the folder before, the file probably will not exist, but Rez creates it on–the–fly. Deleting the Icon\r file will remove the custom icon, so to prevent accidents it is good to be hidden.

These are the modifications to the mankoff's answer:

# Append a resource to the folder you want to icon-ize.
Rez -append tmpicns.rsrc -o $'myfolder/Icon\r'

# Use the resource to set the icon.
SetFile -a C myfolder/

# Hide the Icon\r file from Finder.
SetFile -a V $'myfolder/Icon\r'
share|improve this answer
And for a file? Note my comment re: avi files –  Slomojo Mar 12 '11 at 2:27
@slomojo sorry, but I can't replicate the error you get—just tested the mankoff's solution with an .avi file. Re-check the file permissions and commands you entered? –  koiyu Mar 15 '11 at 9:20
I'll give it another go, perhaps I made a mistake before. –  Slomojo Mar 15 '11 at 11:22

You'll need the Developer Tools installed, and then the following might work. This takes the graphic in icon.png and applies it to file.ext.

# Take an image and make the image its own icon:
sips -i icon.png

# Extract the icon to its own resource file:
/Developer/Tools/DeRez -only icns icon.png > tmpicns.rsrc

# append this resource to the file you want to icon-ize.
/Developer/Tools/Rez -append tmpicns.rsrc -o file.ext

# Use the resource to set the icon.
/Developer/Tools/SetFile -a C file.ext

# clean up.
rm tmpicns.rsrc
# rm icon.png # probably want to keep this for re-use.
share|improve this answer
Awesome, that's just what I wanted. –  Slomojo Jan 24 '11 at 23:56
I'm having trouble with Rez -append when working with .avi files. ### Rez - Fatal error trying to open the resource file "Video.avi" for writing. ` any ideas? –  Slomojo Jan 26 '11 at 11:24
Interestingly, it also made the file unable to accept a new icon via the Get Info copy/paste method. –  Slomojo Jan 26 '11 at 19:06
is there a restriction on the icon size? If I use a large jpg (1024x768) it would be resized to something really small.. Can I somehow retain its original size? –  nuc Mar 13 '11 at 19:21
@nuc OS X supports an icon size maximum of 512x512, so I see no point (well, maybe other than future-proofing perhaps) saving it bigger. And while you're at the command line, you could resize the image's longest length to 512 e.g. with sips: sips -Z 512 image.png --out smaller-image.png (though if you'd like to have control over the resampling algorithms, maybe an image processing software is a better idea). –  koiyu Mar 15 '11 at 9:26

Check out setfileicon (source code), an utility created by Damien Bobillot.

Download the binary here: http://maxao.free.fr/telechargements/setfileicon.gz

After unpacking the file, make it executable:

chmod +x setfileicon

Then you can use it as follows:

./setfileicon "my-icon.icns" "my-file-or-directory"
share|improve this answer
Thanks for that, very helpful indeed. BTW, I'm editing out the GUI method, to reduce noise. –  Slomojo Mar 24 '12 at 0:12

I have a github project where I create AppleScript "droplets" from [shell/bash, python, perl, ruby, etc.] scripts that take file paths as arguments. I wrote this bash function for changing the icon of an folder (because AppleScript bundles are Folders with a .app extension).

    if [[ $icon =~ ^https?:// ]]; then
        curl -sLo /tmp/icon $icon
    rm -rf $droplet$'/Icon\r'
    sips -i $icon >/dev/null
    DeRez -only icns $icon > /tmp/icns.rsrc
    Rez -append /tmp/icns.rsrc -o $droplet$'/Icon\r'
    SetFile -a C $droplet
    SetFile -a V $droplet$'/Icon\r'

Once defined, you call the function with 2 arguments:

replace_icon /path/to/AppleScript.app /path/to/icon.png


replace_icon /path/to/AppleScript.app http://i.imgur.com/LmUvWqB.png

As you can see the second argument can be an image on your system, or a URL.

Also, as I say with every bash script, you should always include this for safety:

## No bash script should be considered releasable until it has this! http://j.mp/safebash ##
# Exit if any statement returns a non-true return value (non-zero).
set -o errexit
# Exit on use of an uninitialized variable
set -o nounset
share|improve this answer

Another option is to use MacRuby:

/usr/local/bin/macruby -e 'framework "Cocoa";NSWorkspace.sharedWorkspace.setIcon(NSImage.alloc.initWithContentsOfFile("/tmp/a.png"),forFile:"/tmp/file",options:0)'

You can download an installer for MacRuby from http://macruby.org.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.