Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

Can I selectively remove Safari favicon icons?

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?

Dave's Answer:

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 http://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.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

I ran your script and it deleted ALL my favicons. OS 10.3.9

Posted by: Allan Dowdeswell at July 7, 2005 9:16 PM

That's most weird. What output did you see when you ran it? Tons of matching cache files, or just a few?

Posted by: Dave Taylor at July 7, 2005 10:19 PM

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.

Posted by: Drew Thaler at November 30, 2005 9:22 PM

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.

Posted by: Michael S. at January 6, 2006 8:31 PM

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

Posted by: jimlongo at January 17, 2006 5:10 PM

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...

Posted by: Q... at January 27, 2006 7:52 PM

don't you think the trap should be just before the find?

putting it first yields ENOENT if one of the sanity checks fail.

Posted by: peter honeyman at August 31, 2006 4:52 PM

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?

Posted by: peter honeyman at August 31, 2006 4:58 PM

why do you want the 'a' flag on the call to ps?

Posted by: peter honeyman at August 31, 2006 5:04 PM

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,

\(^_^)/

Posted by: peter honeyman at August 31, 2006 5:15 PM

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!

Posted by: peter honeyman at August 31, 2006 6:42 PM

After hours of fun trying to figure out WHY my favicon wouldn't refresh I found your solution. Thank you. I owe you.

--Joyce

Posted by: Joyce at December 24, 2006 2:59 PM

I WOULD PREFER TO AVOID ALL FAVICONS. cAN i DELETE SAFARI?

Posted by: jsryan at February 12, 2007 11:07 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]