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

  • 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 '15 at 13:28
up vote 29 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.
  • Awesome, that's just what I wanted. – ocodo 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? – ocodo 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. – ocodo 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
  • 2
    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 '15 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'
  • And for a file? Note my comment re: avi files – ocodo 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? – Jari Keinänen Mar 15 '11 at 9:20
  • I'll give it another go, perhaps I made a mistake before. – ocodo 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! – singmotor Aug 7 '15 at 19:59

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, xxd and an embedded Python script that calls Cocoa, courtesy of this helpful answer.

It allows setting/removing/extracting custom icons for/from files or folders, including on APFS volumes on macOS 10.13 (High Sierra).

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
  • @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 '15 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 '15 at 3:41
  • 2
    This is a nice out of the box working solution. +1 – user3439894 Feb 28 '16 at 0:48
  • This no longer works in 10.10. The project has moved to github: if you want to check for an update. – Pat Niemeyer Mar 2 '16 at 15:29
  • 1
    @PatNiemeyer: Thanks, I've updated the osxiconutils link, but note that this answer is about utility fileicon, which still works (and, in fact, was created, because osxiconutils no longer works) - I've updated the answer to make that clearer. – mklement0 Mar 2 '16 at 15:39

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.

  • 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
  • 1
    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 '15 at 1:29
  • 1
    Didn't exactly find the answer, but squaring the images with transparent padding before hand worked just fine:… – Gustavo Bezerra Apr 28 '15 at 1:55

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"
  • Thanks for that, very helpful indeed. BTW, I'm editing out the GUI method, to reduce noise. – ocodo Mar 24 '12 at 0:12
  • Unfortunately most code is now deprecated. – Mugen Oct 27 '15 at 9:06

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.

NOTE: That crazy looking $'/Icon\r' thing that I do is because the name of the Icon file ends with a literal carriage return \r. See for yourself with:

find -maxdepth 1 | less -U

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

  • 1
    The link - and MacRuby - are apparently now dead; MacRuby was apparently succeeded by the commercial, subscription-based RubyMotion. – mklement0 Jul 30 '15 at 21:25

Assuming that we have icns-file already. Create temp resource file which points to icns-file:

$ echo "read 'icns' (-16455) \"Icon.icns\";" >> Icon.rsrc

Append the resource file as value of extended attribute "" to a file:

$ Rez -a Icon.rsrc -o FileName.ext

Show the icon of the file:

$ SetFile -a C FileName.ext

Append resource file as value of extended attribute "" to a magic icon file inside current folder:

$ Rez -a Icon.rsrc -o Icon$'\r'

Show the icon of current folder:

$ SetFile -a C .

Hide the magic icon file inside current folder (press ⇧⌘. to show/hide hidden files in Finder):

$ SetFile -a V Icon$'\r'

Additional details

Icon data is stored as value of extended attribute "" (Terminal command "xattr -p FileName.ext" prints the value). For a folder there is magic (which is empty and hidden) file Icon$'\r' inside the folder. To extract icon data from extended attribute "" into plain text resource file (from which we know correct icns-type identifier "-16455"):

$ DeRez -only icns FileWithIcon.ext > Icon.rsrc
$ DeRez -only icns /Folder/With/Icon/Icon$'\r' > Icon.rsrc

Under macOS 10.13 High Sierra command $ sips -i ImageFile.icns/png/jpg generates error "--addIcon is no longer supported". Switch "-i" means "--addIcon" as extended attribute "" onto this file itself using the content of this image file.

Strangely enough the best solution seems to be not to use Apple's own tools but the Python code because it has various advantages not being limited by:

  • output file resolution
    (it works till 1024x1024)
  • input file format
    (tried with ICNS and PNG)
  • permissions to install
    (define it as a function)


Now (on macOS Sierra) @koiyu’s answer seems to work, so you could use this two-liner to copy the icon from a source folder to your destination folder:


DeRez -only icns $1/Icon$'\r' > icon.rsrc; Rez -a icon.rsrc -o $2/Icon$'\r'
SetFile -a C $2; SetFile -a V $2/Icon$'\r'; rm icon.rsrc
  • Welcome to Ask Different! This seems like it would be better as a comment. I know you don't have the points required yet, but I've asked someone who has the power to come take a look. In addition, I've written an Applescript that does all this for you in a neat bundle, see my answer to a similar question on this SE. – JMY1000 May 20 '16 at 2:04
  • Yes, I tried but could not do it. Furthermore I think that the 2nd part of your comment would be better as an answer, not being related to the 1st one. – dardo82 May 22 '16 at 2:41

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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