To set an icon on a file or directory is straight forward using the "Get Info" dialog in Finder.
But how do you do this using the command line?
On your Mac, select the file or folder. Choose File > Get Info in the menu bar. At the top of the Info window, select the small custom icon.
Right-click a folder > select Properties > Customize > Change icon > choose from the available icons > and click OK to apply your choice. Alternatively, choose Browse to search a specific custom icons folder. You can follow these same steps to change folder icons in Windows 11.
Right-click the folder that you want to create a shortcut to, and then click Create Shortcut. Right-click the shortcut you created, click Properties, and then click Change Icon. Click the picture of the icon you would like the shortcut to use, click OK, and then click OK.
Here is a bash script "setIcon.sh" for it
#!/bin/sh
# Sets an icon on file or directory
# Usage setIcon.sh iconimage.jpg /path/to/[file|folder]
iconSource=$1
iconDestination=$2
icon=/tmp/`basename $iconSource`
rsrc=/tmp/icon.rsrc
# Create icon from the iconSource
cp $iconSource $icon
# Add icon to image file, meaning use itself as the icon
sips -i $icon
# Take that icon and put it into a rsrc file
DeRez -only icns $icon > $rsrc
# Apply the rsrc file to
SetFile -a C $iconDestination
if [ -f $iconDestination ]; then
# Destination is a file
Rez -append $rsrc -o $iconDestination
elif [ -d $iconDestination ]; then
# Destination is a directory
# Create the magical Icon\r file
touch $iconDestination/$'Icon\r'
Rez -append $rsrc -o $iconDestination/Icon?
SetFile -a V $iconDestination/Icon?
fi
# Sometimes Finder needs to be reactivated
#osascript -e 'tell application "Finder" to quit'
#osascript -e 'delay 2'
#osascript -e 'tell application "Finder" to activate'
rm $rsrc $icon
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With