I’ve been slowly adding all the nifty features to my new Web site and am going a bit crazy with Safari. I changed my favicon.ico favorites icon for my Web site, but Safari insists that it’s not right, though when I view the site in Firefox, Camino or any other browser it’s fine. How do I get Safari to be happy?
The problem isn’t that Safari doesn’t respect the favicon shortcut, but that Safari saves all the favorite icons in an internal database and once it’s visited a site and stored its icon, Safari has no way of knowing that the icon’s changed.
To convince Safari to play nice, you need to actually go into the icon cache and remove the icons stored for your Web site. Unfortunately, this is incredibly hard to accomplish. Further, using “Clear Cache” or any other options within Safari don’t affect the icon cache.
One strategy you could use is to brute force just remove all the favicons from the cache and let Safari start rebuilding them for every site you visit. That’s done by using this command within the Terminal (Applications -> Utilities -> Terminal): find ~/Library/Safari/icons -type f -print | xargs rm. But there’s a better way to accomplish this, a less brute-force strategy, utilizing a simple shell script.
A quick glimpse at the ~/Library/Safari/icons directory reveals that it’s in a weird format that’s not at all human readable. Further, each file therein is in a binary format suitable for Safari to read but certainly not anything we can figure out.
Which is where the wonderful utility grep comes in, because grep can scan data files looking for patterns just as easily as it can scan text files. Armed with that knowledge, and knowing that you can use the find command to traverse a directory tree, just matching the files and skipping the directories in the output, the basic command to find what cache files contain a given domain name is:
$ cd ~/Library/Safari/icons $ find . -type f -print | xargs grep "askdavetaylor.com" Binary file ./04/08/3481099592-1085979264.cache matches Binary file ./08/05/3359939716-3145438806.cache matches Binary file ./10/04/4077213356-2278820684.cache matches Binary file ./15/11/1480449008-3264651962.cache matches $
In this instance, I have found all the cache files that contain the string ‘askdavetaylor.com’, though you can obviously search for your domain instead.
The logical next step is to pour this command into a shell script and, um, add some bells and whistles to make it more useful. And so…
#!/bin/sh # Given a specific domain name, this script will try to find all the # cached icons for that domain. With the '-r' flag included, it'll actually # remove those icons. # # This script is (C) 2004 by Dave Taylor. Permission is granted to # disseminate this so long as this comment is retained intact. Learn # more about Dave's scripts by visiting http://www.intuitive.com/wicked/ # or ask Dave a question at https://www.askdavetaylor.com/ icondir="$HOME/Library/Safari/icons" temp="/tmp/$0.$$" removeme=0 trap "/bin/rm -f $temp" 0 # remove temp file on exit # specified -r ? if [ "$1" = "-r" ] ; then removeme=1 shift fi # used properly? if [ $# -eq 0 ] ; then echo "Usage: $0 {-r} domainname ('-r' removes matching cached icons)" exit 0 fi # is Safari running? If so, that's a no-no if [ $removeme -eq 1 -a \ "$(ps aux | grep -i safari | grep -v grep)" != "" ] ; then echo "Error: deleting icons from the cache while Safari is running" >&2 echo "is not a good idea. Quit Safari then run this script again." >&2 exit 0 fi find $icondir -print | \ xargs grep "$1" 2>&1 | grep -v "not permitted" > $temp if [ $removeme -eq 1 ] ; then for filename in $(cut -f3 -d\ $temp) do echo "removing file $filename" rm $filename done echo "Done. Now restart Safari and revisit that site." else cat $temp fi exit 0
Since this script checks to see if Safari is running to avoid deleting cache files just to have them replaced, I strongly encourage you not to have the word ‘safari’ in the name of this script. I call it clearcache on my system.
For the basic search of cache files that match a given domain name, it works exactly as the command line shown earlier does:
$ clearcache askdavetaylor.com Binary file ~/Library/Safari/icons/04/08/3481099592-1085979264.cache matches Binary file ~/Library/Safari/icons/08/05/3359939716-3145438806.cache matches Binary file ~/Library/Safari/icons/10/04/4077213356-2278820684.cache matches Binary file ~/Library/Safari/icons/15/11/1480449008-3264651962.cache matches
To actually delete the cache files, add the -r flag:
$ clearcache -r askdavetaylor.com removing file ~/Library/Safari/icons/04/08/3481099592-1085979264.cache removing file ~/Library/Safari/icons/08/05/3359939716-3145438806.cache removing file ~/Library/Safari/icons/10/04/4077213356-2278820684.cache removing file ~/Library/Safari/icons/15/11/1480449008-3264651962.cache Done. Now restart Safari and revisit that site.
Sure enough, revisiting the site now reloads the favicon.ico, which is to say that it’s now the new favorites icon that’s displayed and saved in the cache for next time.
A complex answer to what should be a simple question, but I hope that script helps you out! If you’d like to learn more about Mac OS X shell script programming, check out my book Wicked Cool Shell Scripts.
So if you have Favicons on your site from the beginning, this won’t happen?
I WOULD PREFER TO AVOID ALL FAVICONS. cAN i DELETE SAFARI?
After hours of fun trying to figure out WHY my favicon wouldn’t refresh I found your solution. Thank you. I owe you.
–Joyce
oops, i forgot to say “thanks, dave!”
the wonderful wifi control system used by the university of michigan diverts requests to an authentication server, then redirects the request to the desired page after login.
how very wonderful that it has a favicon … which then gets attached to the desired page if that page lacks one (or has not been visited before).
did i say wonderful? i mean annoying.
i’ve seen this in hotel wifi control as well.
WE HATES THEM! WE HATES THEM!
so i needed your help … and got it. THANKS, DAVE!
the grep call in the xargs is pretty ham-fisted … imagine what
clearcache -r com
will do. or even something more specific, like
clearcache -r dave.taylor.com
which matches stuff like
http ://foo.com/bar?dave@taylor.com
all in all, i’d say this script needs a little more effort, dave
yr pal,
\(^_^)/
why do you want the ‘a’ flag on the call to ps?
using $0.$$ for the temp file seems to be broken … it sets temp to
temp=/tmp//Users/honey/bin/favico.2469
you want
temp=/tmp/`basename $0`.$$
no?
don’t you think the trap should be just before the find?
putting it first yields ENOENT if one of the sanity checks fail.
Favicon problem
Bug fixed with find ~/Library/Safari/Icons -type f -print | xargs rm and restart Safari (kudos to Dave Taylor). Neither emptying the cache nor the debug meny helped. There is still a bug in that Safari is not aware of favicon change, still.
I already
Thanks, this was just the fix I needed for my problem. Worked like a charm. — I used the brute force method because it was a subdomain….
Q…
It appears to delete all icons from my Safari 2.0.2 (10.4.4) as well.
The output in the terminal will only list the 2 or 3 icons I asked it to delete, but upon restarting Safari there will be no icons anywhere.
The ~/Library/Safari/Icons is still full of folders and cache files.
hth
Deleting the matching files in ~/Library/Safari/Icons blew away all of my favicons too. One of the files was 103k, so I’m guessing it contained a whole lot of icons, not just the one I wanted to delete.
Just a quick note – the path should have a capital letter ‘I’ in ‘icons’: ~/Library/Safari/Icons.
Since most people use standard HFS+ and it’s case-insensitive, a lowercase i works for almost everyone. But if you are using HFSX, UFS, or NFS for your home directory you’ll need the uppercase I.
That’s most weird. What output did you see when you ran it? Tons of matching cache files, or just a few?
I ran your script and it deleted ALL my favicons. OS 10.3.9
online poker
Please check the pages about online poker online casino phentermine
online poker
Please check the pages about online poker online casino phentermine
online poker
Please check the pages about online poker online casino phentermine
online poker
Please check the pages about online poker online casino phentermine