#!/bin/bash # browser-cleanup 1.1.8 # by Jon Jensen # jon@swelter.net # Removes all Netscape and Mozilla history, cookie, and cache files. # When run as root, deletes these files from all users' home directories, # otherwise only for current user's home directory. # Options: # -i interactive delete (except cache, because it has so many files) # -q quiet (default) # -v verbose # -vv very verbose # -l ignore lock files # -p ignore running processes # History: # 1999 April 18 v1.0.0 - netscape-cleanup initial version # 1999 June 7 v1.0.1 - Stopped deleting cache directory (just contents) # to avoid annoying startup error message first launch afterwards. # 1999 Dec 5 v1.0.2 - Start out with a check for running Netscape processes. # 2001 Feb 4 v1.1.0 - Rename to browser-cleanup. Now also clean out Mozilla # folder, genericize utility paths. # 2001 Feb 5 v1.1.1 - Update message verbosity levels, now quiet by default. # Mozilla profiles are somewhat complex; still working on variations. # 2001 Feb 9 v1.1.2 - Parse out history from Mozilla localstore.rdf if a Perl # interpreter is available; otherwise, simply delete it. Mostly you would just # lose window positions and sizes, etc., but I want to avoid that where possible. # 2002 May 4 v1.1.3 - Make .mozilla directory handling smarter, stop trying to # process all users unless run as root # 2002 Oct. 22 v1.1.4 - Clean Phoenix files (same as Mozilla, really) # 2002 Nov. 5 v1.1.5 - Clean Galeon files (also Mozilla-based) # 2003 March 5 v1.1.6 - Clean Konqueror & Opera files # 2003 May 27 v1.1.7 - Clean Mozilla Firebird formhistory.dat and URL.tbl # 2005 Nov. 5 v1.1.8 - Clean Epiphany files and check for running Firefox # To do: # allow multiple options behind a single hyphen (-ql, etc.) # Unix flatfile database locations passwdfile=/etc/passwd shellsfile=/etc/shells # Unix utility configuration (usually not necessary) #shopt -s expand_aliases #alias egrep=/bin/egrep #alias grep=/bin/grep #alias head=/usr/bin/head #alias ls=/bin/ls #alias perl=/usr/bin/perl #alias ps=/bin/ps #alias pwd=/bin/pwd #alias rm=/bin/rm ## useful for testing: ##alias rm='echo /bin/rm' #alias sed=/bin/sed #alias sort=/bin/sort #alias tail=/usr/bin/tail #alias uniq=/usr/bin/uniq #alias whoami=/usr/bin/whoami # parse command-line options for arg; do case "$arg" in -i) ask="$arg" ;; -q) verbose="" ;; -v) verbose="$arg" ;; -vv) verbose="$arg" rmverbose="-v" ;; -l) ignorelock="$arg" ;; -p) ignoreproc="$arg" ;; -*) echo "`head -18 $0 | tail -17 | sed 's/^# //'`" echo exit 0 ;; esac done # running as root or normal user? iamuser=`whoami` if [ $verbose ]; then echo "Running as user $iamuser"; fi if [ "$iamuser" = "root" ]; then psargs="auxww" else psargs="uxww" fi # print all running browser processes processes=`ps $psargs | egrep '(netscape|mozilla|phoenix|galeon|firefox|epiphany)' | grep -v grep | grep -v $0` if [ -n "$processes" ] && [ -z "$ignoreproc" ]; then if [ $verbose ]; then echo "The following browser processes appear to be running:" echo "$processes" fi netscape_processes=`echo "$processes" | grep netscape | awk '{print $1}' | sort | uniq` mozilla_processes=`echo "$processes" | egrep '(mozilla|phoenix|galeon|firefox|epiphany)' | awk '{print $1}' | sort | uniq` fi # get list of all users who have a valid shell for user in `awk -F : '{print $1}' $passwdfile` ; do shell=`grep "^$user:" $passwdfile | awk -F : '{print $7}'` if [ -n "$shell" ] && [ -n "`grep $shell $shellsfile`" ]; then users="$users $user" fi done # remember current directory and my script pathname for later orig_cwd=`pwd` if [ ${0:0:1} = / ]; then self_cmd="$0" else self_cmd="`pwd`/$0" fi # now go through each user's directory if root, current user's only otherwise for user in $users; do home_dir=`grep "^$user:" $passwdfile | awk -F : '{print $6}'` if [ "$iamuser" = "root" ]; then if [ "$verbose" = "-vv" ]; then echo "Working on $home_dir for user $user"; fi else [ "$iamuser" != "$user" ] && continue fi # Netscape loop this_dir="$home_dir/.netscape" if [ -d $this_dir ]; then cd $this_dir if [ -z "$ignorelock" ] && ( [ -e lock ] || [ -L lock ] ); then if [ $verbose ]; then echo "Skipping $this_dir because lock file exists"; fi else if [ -z "$ignoreproc" ]; then if [ -n "`echo "$netscape_processes" | grep $user`" ]; then if [ $verbose ]; then echo "Skipping $this_dir because process is running"; fi else if [ $verbose ]; then echo "Removing junk files from $this_dir"; fi for del_file in history.dat history.db history.list cookies; do if [ -f $del_file ]; then rm $rmverbose $ask $del_file fi done for del_dir in cache; do if [ -d $del_dir ]; then rm -rf $rmverbose $del_dir/* fi done fi fi fi else if [ "$verbose" = "-vv" ]; then echo "No directory $this_dir" fi fi # remove some Galeon-specific files this_dir="$home_dir/.galeon" if [ -d $this_dir ]; then if [ $verbose ]; then echo "Removing junk files from $this_dir"; fi pushd $this_dir >/dev/null \ && rm $rmverbose $ask -r history.xml session_crashed.xml host_favicons.keys favicons favicons.keys \ && popd >/dev/null fi # Mozilla loop for this_dir in \ `find $home_dir/.mozilla -mindepth 1 -maxdepth 1 -type d 2>/dev/null` \ `find $home_dir/.phoenix -mindepth 1 -maxdepth 1 -type d 2>/dev/null` \ `find $home_dir/.galeon -mindepth 1 -maxdepth 1 -type d 2>/dev/null` \ `find $home_dir/.gnome2/epiphany -mindepth 1 -maxdepth 1 -type d 2>/dev/null` do if [ -d $this_dir ]; then if [ -z "$ignoreproc" ]; then if [ -n "`echo "$mozilla_processes" | grep $user`" -a -z "$ignorelock" ]; then if [ $verbose ]; then echo "Skipping $this_dir because process is running"; fi else cd $this_dir profile_dirs=`echo *` if [ -d $this_dir/$user ]; then profile_dirs="$profile_dirs `echo $this_dir/$user/*`" fi for profile_dir in $profile_dirs ; do if [ -d $profile_dir ]; then cd $profile_dir if [ $verbose ]; then echo "Removing junk files from `pwd`"; fi for del_file in history.dat formhistory.dat URL.tbl history.list old\\ history.dat cookies.txt; do if [ -f $del_file ]; then rm $rmverbose $ask $del_file fi done for del_dir in Cache; do if [ -d $del_dir ]; then rm -rf $rmverbose $del_dir/* fi done rdf_file=localstore.rdf if [ -f $rdf_file ]; then if [ -n "`perl -v`" ]; then cp -p $rdf_file $rdf_file.bak && \ perl -x $self_cmd $rdf_file.bak > $rdf_file && \ rm -f $rdf_file.bak else rm $rmverbose $ask $rdf_file fi fi cd .. fi done fi fi else if [ "$verbose" = "-vv" ]; then echo "No directory $this_dir" fi fi done # Opera loop this_dir="$home_dir/.opera" if [ -d $this_dir ]; then if [ $verbose ]; then echo "Removing junk files from $this_dir"; fi pushd $this_dir >/dev/null \ && rm $rmverbose $ask global.dat cookies4.dat autopera.win vlink4.dat \ && rm -f $rmverbose acpo/* cache4/* \ && popd >/dev/null fi # Konqueror loop this_dir="$home_dir/.kde/share" if [ -d $this_dir ]; then if [ $verbose ]; then echo "Removing junk files from $this_dir"; fi pushd $this_dir >/dev/null \ && rm $rmverbose $ask apps/konqueror/konq_history apps/kcookiejar/cookies config/konq_history \ && popd >/dev/null fi done cd $orig_cwd if [ $verbose ]; then echo "All done."; fi exit 0 # Below is a short Perl program to clean out the Mozilla localstore.rdf file. # If a Perl interpreter is not in the path, the .rdf file will simply be deleted. #!/usr/bin/perl -w # slurp entire file undef $/; $_ = <>; # remove URL bar history s{[\t ]*<\s*RDF:Seq.*?nc:urlbar-history.*?<\s*/RDF:Seq\s*>[\t ]*\n?}{}sg; # remove all URL info caching (except internal Mozilla URLs) s{[\t ]*<\s*RDF:Description\s*about="\s*(?!chrome).*?<\s*/RDF:Description\s*>[\t ]*\n?}{}sg; # remove all FTP directory cache info s{[\t ]*<\s*RDF:Description\s*about="\s*.*?directory.xul.*?<\s*/RDF:Description\s*>[\t ]*\n?}{}sg; print; __END__