Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • Video Help
  • Latest Reviews
  • Most Popular
  • Top Categories
  • About Dave
  • Ask!
  • Home
  • Linux Shell Script Programming
  • Can I selectively remove Safari favicon icons?

Can I selectively remove Safari favicon icons?

December 22, 2004 / Dave Taylor / Linux Shell Script Programming, Macbook, iMac and Mac OS X Help / 19 Comments

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

Let’s Stay In Touch!

Never miss a single tutorial, review or article here on AskDaveTaylor, sign up for our fun weekly newsletter. It's the only way to get special content that's only shared via email too!
Name:
Email:
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!

19 comments on “Can I selectively remove Safari favicon icons?”

  1. TommyVF says:
    April 20, 2010 at 12:46 pm

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

    Reply
  2. jsryan says:
    February 12, 2007 at 11:07 am

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

    Reply
  3. Joyce says:
    December 24, 2006 at 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

    Reply
  4. peter honeyman says:
    August 31, 2006 at 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!

    Reply
  5. peter honeyman says:
    August 31, 2006 at 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,
    \(^_^)/

    Reply
  6. peter honeyman says:
    August 31, 2006 at 5:04 pm

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

    Reply
  7. peter honeyman says:
    August 31, 2006 at 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?

    Reply
  8. peter honeyman says:
    August 31, 2006 at 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.

    Reply
  9. Empyr�e says:
    February 4, 2006 at 5:50 am

    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

    Reply
  10. Q... says:
    January 27, 2006 at 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…

    Reply
  11. jimlongo says:
    January 17, 2006 at 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

    Reply
  12. Michael S. says:
    January 6, 2006 at 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.

    Reply
  13. Drew Thaler says:
    November 30, 2005 at 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.

    Reply
  14. Dave Taylor says:
    July 7, 2005 at 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?

    Reply
  15. Allan Dowdeswell says:
    July 7, 2005 at 9:16 pm

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

    Reply
  16. online poker says:
    February 1, 2005 at 6:16 am

    online poker

    Please check the pages about online poker online casino phentermine

    Reply
  17. online poker says:
    February 1, 2005 at 6:13 am

    online poker

    Please check the pages about online poker online casino phentermine

    Reply
  18. online poker says:
    February 1, 2005 at 6:12 am

    online poker

    Please check the pages about online poker online casino phentermine

    Reply
  19. online poker says:
    February 1, 2005 at 6:12 am

    online poker

    Please check the pages about online poker online casino phentermine

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Posts

  • Disable Android phone GPS location tracking and recording?
  • Can I customize my LinkedIn Profile appearance?
  • How do I set alerts on my Chase Bank checking account?
  • Does iOS 8 actually delete voicemails on my iPhone?
  • How do I erase or wipe an old Flash Drive?

Categories

  • Articles, Tutorials and Product Reviews
  • Blogs, Blogging and Wordpress Help
  • Building Web Site Traffic
  • Business and Management Advice
  • Cell Phone. Mobile Phone, iPhone and Android Help
  • Computer and Internet Basics
  • d) None of the Above
  • eBay, Auctions and Online Shopping
  • Facebook Questions and Help
  • Gmail and Gmail.com Help
  • Google Plus and G+ Support
  • HTML, Scripting and Web Design
  • Industry and Trade Show News
  • iPad, Android and Windows Tablet Help
  • iPod, iPod Touch and MP3 Player Help
  • Kindle Store and Amazon Kindle Support
  • Linux Commands and Configuration
  • Linux Shell Script Programming
  • Macbook, iMac and Mac OS X Help
  • Most Popular
  • Pinboards and Pinterest Help
  • PPC – Pay Per Click – and AdWords Help
  • Search Engine Optimization – SEO – Tips and Tricks
  • Twitter, LinkedIn and Instagram Help
  • Video Game Tips and Reviews
  • Windows 8 & Windows Live Tips and Support
  • Writing and Getting Published
  • YouTube Videos, Tutorials and Reviews

Archives



Follow Me on Pinterest
Follow Me on Google Plus
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook
AskDaveTaylor on Google Plus

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".
© 2014 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.