Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Remove Safari history files on logout via shell script Web Browsers
If you'd like to remove your Safari history files when you log out, first create the following shell script:
#!/bin/bash

# Point this at your user folder.
useroot=/Users/your_user

# Clear Safari History (except for bookmarks).
rm -f $useroot/Library/Safari/Downloads.plist $useroot/Library/Safari/History.plist 
rm -f $useroot/Library/Safari/HistoryIndex.sk $useroot/Library/Safari/TopSites.plist 
rm -f $useroot/Library/Safari/WebpageIcons.db $useroot/Library/Safari/LastSession.plist

# Add any additional clean-up here.
Save the script somewhere, make it executable, and because it runs as root, make sure it is protected from modification. Then copy and paste this command into Terminal, modifying it to point to wherever you put the above script:

sudo defaults write com.apple.loginwindow LogoutHook "/Users/your_user/scripts/privacy.sh"

To be complete, you should also visit Safari's Preferences, open the AutoFill tab, and clear all of the checkmarks (Safari then won't remember passwords and forms).

[robg adds: I haven't tested this one.]
    •    
  • Currently 1.55 / 5
  You rated: 3 / 5 (11 votes cast)
 
[15,779 views]  

Remove Safari history files on logout via shell script | 14 comments | Create New Account
Click here to return to the 'Remove Safari history files on logout via shell script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Remove Safari history files on logout via shell script
Authored by: NinjaRat on Jul 01, '09 10:21:21AM
I've been using something similar to this for a while but my combination of tools is a bit different. I use Platypus to wrap my script up into an application because it launches faster than either AppleScript or Automator. And I use Do Something When (DSW) to launch the app when Safari quits. Rather than using rm I move the files to ~/.Trash and then use the Secure Empty Trash when I'm feeling especially paranoid.

Also note that if you have Time Machine running then these files will likely be on your backups unless specifically excluded.

Platypus: http://www.sveinbjorn.org/platypus
DSW: http://www.azarhi.com/Projects/DSW/

[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: mat79 on Jul 01, '09 10:38:28AM
why use useroot=/Users/your_user
I really dont get this. whats wrong with using ~ instead?

#!/bin/bash

Clear Safari History (except for bookmarks).
rm -f ~/Library/Safari/Downloads.plist ~/Library/Safari/History.plist 
rm -f ~/Library/Safari/HistoryIndex.sk ~/Library/Safari/TopSites.plist 
rm -f ~/Library/Safari/WebpageIcons.db ~/Library/Safari/LastSession.plist

# Add any additional clean-up here.


[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: leamanc on Jul 01, '09 05:14:15PM

The problem with using ~ to denote your home folder is that it's just bad practice, in terms of portability. It won't work on all shell scripts in all environments. Sure, in this case, you're only going to be running it on OS X, and it will probably work all the time. But it's best to stick to the rules of scripting, and using the tilde, or other path shortcuts, is not recommended.

But, there is something you can use instead: the $HOME variable, as such:

rm -f $HOME/Library/Safari/Downloads.plist $HOME/Library/Safari/History.plist


[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: leamanc on Jul 01, '09 05:23:24PM

Looking into this further, the tilde might not even work, according to this archived OS X Hints forum post.

It says:

Commands don't understand tilde, only the shell.



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: excarnate on Jul 02, '09 02:11:20PM

In this case it isn't commands, it is the shell. The shell expands the ~ so there is no problem.

~ really isn't that bad, I've never run into problems, but $HOME is slightly preferable, you can change the location of where it looks easily, say:
export HOME=/somewhere/else
say if you have an archived home directory you want it to work with. Maybe not something that applies here. Personally on little scripts that are special purpose I'd use ~ as it is easier and very clear.



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: RedFred on Jul 06, '09 04:06:54PM

The Logout hook is run as root so ~ and $HOME refer to /private/var/root, not the home directory of the user logging out. See the hint below for the correct way to do this.



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: palahala on Jul 01, '09 10:47:35AM
The # Add any additional clean-up here should probably include your computer's Flash player's history (which is cross-browser).

[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: palahala on Jul 01, '09 11:48:11AM

And don't forget to follow this hint: Disable Safari 4 Beta's Top Sites web page previews. Or, alternatively, somehow clean up parts of DARWIN_USER_CACHE_DIR and DARWIN_USER_TEMP_DIR...



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: RedFred on Jul 06, '09 04:01:05PM
Logout hooks are run as the root user SO ALWAYS USE CAUTION WHEN DOING THIS!. If you want to know what the short user name of the user logging out is, it is stored in the $1 shell variable, so the correct generalized scripts should be:

#!/bin/bash

# Clear Safari History (except for bookmarks).
/bin/rm -f /Users/$1/Library/Safari/Downloads.plist /Users/$1/Library/Safari/History.plist 
/bin/rm -f /Users/$1/Library/Safari/HistoryIndex.sk /Users/$1/Library/Safari/TopSites.plist 
/bin/rm -f /Users/$1/Library/Safari/WebpageIcons.db /Users/$1/Library/Safari/LastSession.plist

# Add any additional clean-up here.
Also you should use the full path to the rm command so you know you are getting the correct version. Also you may want to add in the
/bin/rm -f /Users/$1/Library/Caches/com.apple.Safari/Cache.db
/bin/rm -rf /Users/$1/Library/Caches/com.apple.Safari/Webpage Previews
For extra credit, you can set up a user agent to clean up Safari everytime it runs, thats what I do.

[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: pingnak on Sep 15, '09 07:57:06PM

OK, I'm glad I posted this some months back, and came back to visit. There was some excellent feedback waiting. I revisited after upgrading to Snow Leopard and noticed I missed some files.

Now it won't miss any files in the Library/Safari folder. HOWEVER, if Apple ever turns evil and renames the 'Bookmarks.plist' file, this will blow away the 'new' bookmarks, and the script will have to be tweaked.

Now blows away Flash persistent object data, too (and as suspected, quite a bit of it had piled up). Somewhat selective of ONLY content of the SharedObject database. There are other Flash settings that shouldn't be nuked.

Now accepts the username from the parameter for the logout hook, and also allows you to run it on demand (i.e. for testing).

Dyke out whatever frightens you. All I care about are my 'shortcuts', and the rest of the 'convenience' be damned.

[code]
#!/bin/bash

#
# Copy/paste this command and point it at wherever you put this script
# sudo defaults write com.apple.loginwindow LogoutHook "/Users/pingnak/scripts/privacy.sh"
#
# To be complete, Safari->Preferences->AutoFill clear all of the checkmarks (it won't remember passwords and forms).
#
# Script runs as root, so make sure this file is protected from casual modification.
#

if [ -d $useroot=/Users/$1 ]
then
useroot=/Users/$1
else
useroot=~
fi

# Clear Safari History (except for bookmarks).
/usr/bin/find Library/Safari/ -not -name Bookmarks.plist -execdir /bin/rm -rf \{\} \;

# Forget cookies
/bin/rm -f $useroot/Library/Cookies/*

# Forget whatever this stuff is
/bin/rm -rf $useroot/Library/Caches/com.apple.Safari

# Forget Flash plugin persistent 'Shared Objects' settings
/bin/rm -rf $useroot/Library/Preferences/Macromedia/Flash\ Player/#SharedObjects

# Add any additional clean-up here.

[/code]



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: pingnak on Sep 15, '09 07:59:48PM
And then, naturally, I spot a bug and can't edit it.

#!/bin/bash

#
# Copy/paste this command and point it at wherever you put this script
# sudo defaults write com.apple.loginwindow LogoutHook "/Users/pingnak/scripts/privacy.sh"
#
# To be complete, Safari->Preferences->AutoFill clear all of the checkmarks (it won't remember passwords and forms).
#
# Script runs as root, so make sure this file is protected from casual modification.
#

if [ -d $useroot=/Users/$1 ] 
then 
	useroot=/Users/$1
else
	useroot=~
fi

# Clear Safari History (except for bookmarks).
/usr/bin/find $useroot/Library/Safari/ -not -name Bookmarks.plist -execdir /bin/rm -rf \{\} \;

# Forget cookies
/bin/rm -f $useroot/Library/Cookies/*

# Forget whatever this stuff is
/bin/rm -rf $useroot/Library/Caches/com.apple.Safari

# Forget Flash plugin persistent 'Shared Objects' settings 
/bin/rm -rf $useroot/Library/Preferences/Macromedia/Flash\ Player/#SharedObjects

# Add any additional clean-up here.



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: pingnak on Sep 15, '09 09:41:30PM

And after some more testing, and some more grepping around, we clean up the search history and some other leftovers, and fix some other bugs I spotted.

[code]
#!/bin/bash

#
# Copy/paste this command and point it at wherever you put this script
# sudo defaults write com.apple.loginwindow LogoutHook "/Users/pingnak/scripts/privacy.sh"
#
# To be complete, Safari->Preferences->AutoFill clear all of the checkmarks (it won't remember passwords and forms).
# If you use 'Time Machine', you should exclude all of these folders, too.
#
# Script runs as root, so make sure this file is protected from casual modification.
#

if [ -z '$1' -o ! -d '/Users/$1' ]
then
useroot=~
else
useroot=/Users/$1
fi

# Clear Safari History (except for bookmarks).
/usr/bin/find $useroot/Library/Safari/ -not -name Bookmarks.plist -execdir /bin/rm -rf \{\} \;

# Forget cookies
/bin/rm -f $useroot/Library/Cookies/*

# Forget whatever this stuff is
/bin/rm -rf $useroot/Library/Caches/com.apple.Safari

# Forget Flash plugin persistent 'Shared Objects' settings
/bin/rm -rf $useroot/Library/Preferences/Macromedia/Flash\ Player/#SharedObjects

# Forget another copy of history
/bin/rm -rf $useroot/Library/Caches/Metadata/Safari/History

# Forget search history embedded in Safari preferences
/usr/bin/defaults write $useroot/Library/Preferences/com.apple.Safari RecentSearchStrings '( )'
#defaults delete ~/Library/Preferences/com.apple.Safari RecentSearchStrings
#/usr/bin/defaults write $useroot/Library/Preferences/com.apple.Safari RecentSearchStrings '( "None of your business." )'

#Since we modified this as 'root', we need to give it back to the user, or we lose all settings.
if test ! -z $1 ; then
chown $1 $useroot/Library/Preferences/com.apple.Safari.*
fi

# If only this worked...
#defaults write ~/Library/Preferences/com.apple.Safari PrivateBrowsingEnabled 1

# Add any additional clean-up here.


[/code]



[ Reply to This | # ]
Remove Safari history files on logout via shell script
Authored by: palahala on Sep 16, '09 03:07:41AM
Somewhat selective of ONLY content of the SharedObject database. There are other Flash settings that shouldn't be nuked.

When you don't clean up $HOME/Library/Preferences/Macromedia/Flash Player/macromedia.com/support/flashplayer then the list of sites one has visited (like visible when using the Settings Manager on Adobe's website) will not be cleared. See also How to automatically remove Flash history/privacy trail? Or stop Flash from storing it? on Super User, especially in case you want to allow some specific sites to store data.

# Forget whatever this stuff is
/bin/rm -rf $useroot/Library/Caches/com.apple.Safari

These are (at least) the Top Sites web page previews, which used to be in a different location in the 4 beta.

# If only this worked...
#defaults write ~/Library/Preferences/com.apple.Safari PrivateBrowsingEnabled 1

Setting PrivateBrowsingEnabled won't keep add-ons from storing any history. I guess when trying to achieve that, using Fast User Switching and a guest account (which is cleared by your Mac upon exit) are much easier then. ;-)



[ Reply to This | # ]
Selectively Remove Safari history via shell script.
Authored by: bjgreenberg on Mar 03, '12 08:20:24PM

I'd like to know how to do a selective delete of sites from within terminal so I can script it.

For example, I don't want to delete the entire history, just the site 'samplesite.com' from Safari's history and leave all other sites alone.

Anyone know how to do this?



[ Reply to This | # ]