Sign up ×
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 – user588 May 12 '12 at 9:36
Note that it does not scale well. Tried to apply icons to lots of directories in one directory — Finder worked very slow. – kolen Apr 28 at 13:28

7 Answers 7

up vote 16 down vote accepted

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
Note that in recent OSX versions the developer tools are in the $PATH (stubs in /usr/bin that know where to find the actual tools), and not in /Developer/Tools anymore, so you should invoke them just by their filenames; i.e., in the case at hand, as just Rez and DeRez. – mklement0 Jul 31 at 21:13

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
I am desperately trying to do this for a USB thumb drive. All of the commands "work" in that they don't return errors, but I am still left with a thumb drive with an unchanged icon. Any thoughts and I'd be eternally grateful! – Acoustic77 Aug 7 at 19:59

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

Download the binary here:

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

In addition to Ruby, here's a Python version:

#!/usr/bin/env python
import Cocoa
import sys

Cocoa.NSWorkspace.sharedWorkspace().setIcon_forFile_options_(Cocoa.NSImage.alloc().initWithContentsOfFile_(sys.argv[1].decode('utf-8')), sys.argv[2].decode('utf-8'), 0) or sys.exit("Unable to set file icon")

./ image.png myfile

Python that pre-installed in Mac OS already has PyObjC so you don't have to install any packages.

share|improve this answer
Very nice, thank you – Slomojo Dec 15 '14 at 14:43
I added decoding of UTF-8 to unicode, otherwise script not worked with non-ASCII filenames. Also added check of return value, failure to set icon will result in return code of 1. – kolen Dec 15 '14 at 21:21
Is there anyway we can specify the icon not have aspect ratio modified? It works great, but image files get reshaped to a square. Thanks! – Gustavo Bezerra Apr 28 at 1:29
Didn't exactly find the answer, but squaring the images with transparent padding before hand worked just fine:… – Gustavo Bezerra Apr 28 at 1:55

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/ /path/to/icon.png


replace_icon /path/to/

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! ##
# 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
Neat function. You should double-quote your variable references to protect them from unwanted shell expansions (for instance, a & in a URL would give you trouble, as would embedded spaces in the value of $droplet). – mklement0 Jul 31 at 14:30

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

share|improve this answer
The link - and MacRuby - are apparently now dead; MacRuby was apparently succeeded by the commercial, subscription-based RubyMotion. – mklement0 Jul 30 at 21:25

With the benefit of several years of hindsight:

user588's answer and koiyu's answer work well, but they rely on utilities (Rez, DeRez, and SetFile) that:

  • aren't installed by default (they come with either Xcode or the developer command-line utilities)
  • are now deprecated (Rez and DeRez, because they relate to Carbon)

osxiconutils look interesting, but won't compile any longer (as of OSX 10.10.4).

Therefore I've created CLI fileicon, which should work on a pristine OSX machine (no prerequisites); it is a Bash script based primarily on xattr and sips.

It allows setting/removing/extracting custom icons for/from files or folders.

You can install it as follows:

  • If you have Node.js installed, from the npm registry, with
    [sudo] npm install -g fileicon
  • Otherwise:
    • Download the CLI as fileicon (this link will stay current).
    • Make it executable with chmod +x fileicon.
    • Move it or symlink it to a folder in your $PATH, such as /usr/local/bin (requires sudo).

Here's the usage information; for complete information, refer to the manual:

$ fileicon -h

Set a custom icon for a file or folder:

    fileicon set      <fileOrFolder> <imageFile>

 Remove a custom icon from a file or folder:

    fileicon rm       <fileOrFolder>

 Get a file or folder's custom icon:

    fileicon get [-f] <fileOrFolder> [<iconOutputFile>]

 Test if a file or folder has a custom icon:

    fileicon test     <fileOrFolder>

 -q ...  silence status output

 Standard options: --help, --man, --version, --home
share|improve this answer
@Slomojo: [In response to a since-deleted comment] I get that not everybody has or wants to install Node.js, that's why the direct download link is there as well (see the 'Otherwise:` list item in my answer) - it's a self-contained Bash script (the only thing you forgo by installing this way is installation of the man page, but you can get a plain-text rendering of the manual page with fileicon --man). – mklement0 Aug 4 at 3:34
@Slomojo: [In response to a since-deleted comment] As for why installing via npm is offered: I've found the npm ecosystem to be my favorite package manager overall, and I've published a number of cross-platform, script-based CLIs there that aren't Node.js-related. I've also create a single-command, user-level installer for npm (via n and Node.js) that makes it easy to use npm even for people who otherwise have no interest in Node.js: curl -L | bash. – mklement0 Aug 4 at 3:41

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.