Question
I know that you can copy an icon from one volume to another using File > Get Info> .... Edit > Copy & Paste.
Can you do the same thing from the command line?
Answer
Shockingly, yes.
I say shockingly because most Mac-like things done in the Finder aren’t available as a CLI program, but in two steps you can copy an icon from one folder to another, and what is a disk but a big folder?
Copying an Icon From Folder to Folder
There are two things to do when copying an icon: copy the actual icon file from one place to another and then set the Finder bit that says the item has a custom icon. You may have noticed that folders with a custom icon have a file called “Icon?” in them in the Terminal (that’s actually “Icon^M”, or the word “Icon” followed by a carriage return). Likewise, you probably tried to copy this file from one place to another with high hopes that it would work, and when it didn’t asked us. Good try, you almost had it.
The missing part was the bit on the folder. To get this, install the Developer Tools (any solution starting with that is bound to be fun) and look inside the /Developer/Tools/ directory for a CLI program called SetFile. This sets file bits. Specifically, you want to take note of the -a option to set attributes. Run the program without arguments and it will show you a table of things to set. Here, we’re looking for the C attribute in upper case because we want to set it on. To turn it off, you use lower case. Obviously, the author has never used a command-line tool in his life, but I digress … a little. Close all open Finder windows and then run this:
sudo /Developer/Tools/SetFile -a C ThatPeskyFolder
With the Icon? file inside the folder and the bit set, open the enclosing folder in the Finder and the icon shows up. Voila.
Mac OS X 10.3 and below will have to use ditto to copy the icon file as it’s a resource-based icon. Tiger’s shell utilities copy resource forks properly so they’re fine.
Copying an Icon From Disk to Disk
It’s the same, yet different for a disk. The icon is stored in the root of the disk as .VolumeIcon.icns in standard Mac OS X format (a folder icon is in a resource-fork-based Mac OS 9 format). Do the same as you did previously, but copy that file instead. Again, ensure all Finder windows are closed before you test it and realize a remount of the drive may be needed to get it to show up everywhere, but it should at least show in the title bar when showing the root level of the disk.
I’ve been doing backups using Tiger’s /usr/bin/tar command, and it doesn’t seem to get the Icon backed up properly. Have you noticed this? A folder with a custom Icon seems to have a 0-byte Icon^M file in it, and a tar of that folder to another place ends up with no custom icon. Any ideas?