Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • 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, Mac & MacOS 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 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.

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
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 *

Search

Recent Posts

  • How Can I Identify Where A Photo Was Taken on a Mac?
  • Easiest Way to Delete/Unpair My Phone in an INFINITI Vehicle?
  • How Can I Change Desktop Wallpaper on my PC Every Day?
  • Possible to Hide Irrelevant or Repetitive Ads on Facebook?
  • The 5 Best Apps If You Need a Ride Anywhere In The World

On Our YouTube Channel

IFI Audio GO Bar USB-C Headphone Amp -- REVIEW

Andobil Windshield Sun Shade Umbrella -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (196)
  • Apple iPad Help (144)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (11)
  • Business Advice (199)
  • Chrome OS Help (23)
  • Computer & Internet Basics (759)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (177)
  • HTML & Web Page Design (245)
  • Instagram Help (47)
  • iPhone & iOS Help (604)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (92)
  • LinkedIn Help (85)
  • Linux Help (163)
  • Linux Shell Script Programming (86)
  • Mac & MacOS Help (889)
  • Most Popular (16)
  • Outlook & Office 365 Help (24)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (17)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (914)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (56)

Archives

Social Connections:

Ask Dave Taylor


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


AskDaveTaylor on Facebook



microsoft insider mvp


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 site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy