Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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.


Related Shell Script Programming articles:
✔   Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔   Parsing "id" strings in a Shell Script?
Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Script to test line lengths for Twitter compatibility?
I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  








Reader Comments To Date: 14

Allan Dowdeswell said, on July 7, 2005 9:16 PM:

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

Dave Taylor said, on July 7, 2005 10:19 PM:

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

Drew Thaler said, on November 30, 2005 9:22 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.

Michael S. said, on January 6, 2006 8:31 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.

jimlongo said, on January 17, 2006 5:10 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

Q... said, on January 27, 2006 7:52 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...

peter honeyman said, on August 31, 2006 4: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.

peter honeyman said, on August 31, 2006 4:58 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?

peter honeyman said, on August 31, 2006 5:04 PM:

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

peter honeyman said, on August 31, 2006 5:15 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,

\(^_^)/

peter honeyman said, on August 31, 2006 6:42 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!

Joyce said, on December 24, 2006 2:59 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

jsryan said, on February 12, 2007 11:07 AM:

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

TommyVF said, on April 20, 2010 12:46 PM:

So if you have Favicons on your site from the beginning, this won't happen?

Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.