Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
etc cruft cleaner
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
isilia
Apprentice
Apprentice


Joined: 25 Feb 2008
Posts: 178
Location: The Netherlands

PostPosted: Sat Nov 29, 2008 7:18 pm    Post subject: etc cruft cleaner Reply with quote

Hi, I wrote a bash script that checks for cruft files in /etc, this is my first useful script written in bash so tips are welcome.
There are two versions of the script, one you can put in /usr/bin/ which will hide your blacklist file in ~/.blacklist and one you can simply run from any directory with your blacklist file in ./blacklist (so you can delete the script afterwards).
/usr/bin version (please note the blacklist is called .blacklist so it might be hidden)
/temporary version

The default blacklist (included in the downloads) is derived from a fresh gentoo installation with only system packages installed.

How to use it:
Code:
isilia@Gentoo ~ $ cruftcleaner --help
Usage: cruftcleaner.sh [directory] [blacklist (optional)]
Cruft cleaner is only designed for /etc/ and its subdirectories
Cruft cleaner depends on gentoolkit as it uses equery!
Blacklists are used to filter files that don't belong to any packages (see equery b) but are not cruft
Blacklists should be written using one line per specified file, without the full path


How it works:
1. It checks if the files/directories in /etc (or any of its subdirectories) belong to any packages using equery b.
2. If not, then it compares the files to the files in a self specified blacklist (I included a default one).
3. If the files are not on the blacklist, it prints the cruft files.

Example:
Code:
isilia@Gentoo ~ $ cruftcleaner /etc/
Gentoolkit is installed... Continuing

Directory seems to be valid...
You have not specified a blacklist, I will continue to use the default blacklist at ~/blacklist

We will now determine which files might be cruft:
/etc/example/
Finished, think twice before deleting anything!

This is my /etc/ directory's contents:
Code:
isilia@Gentoo ~ $ ls /etc/
ConsoleKit            default             gentoo-release        inittab             make.profile         ntp.conf        rpc             sound
DIR_COLORS            dispatch-conf.conf  gimp                  inputrc             man.conf             pam.d           rsyncd.conf     ssh
X11                   eclean              gnome-vfs-2.0         issue               mke2fs.conf          pango           runlevels       ssl
a2ps                  eixrc               gnome-vfs-mime-magic  issue.logo          modprobe.conf        papersize       sane.d          sysctl.conf
adobe                 env.d               gre.d                 ld.so.cache         modprobe.d           passwd          screenrc        syslog.conf
bash                  environment         group                 ld.so.conf          modules.d            passwd-         scsi_id.config  terminfo
bonobo-activation     eselect             group-                locale.gen          mono                 portage         securetty       timezone
ca-certificates       etc-update.conf     gshadow               localtime           mpd.conf             profile         security        udev
ca-certificates.conf  example             gshadow-              login.defs          mplayer              profile.csh     sensors.conf    updatedb.conf
conf.d                exports             gtk-2.0               logrotate.d         mplayerplug-in.conf  profile.env     services        wgetrc
conky                 filesystems         hal                   lynx.cfg            mtab                 protocols       sgml            xdg
cron.daily            fonts               host.conf             lynx.lss            nanorc               rc.conf         shadow          xinetd.d
csh.env               fstab               hosts                 make.conf           networks             resolv.conf     shadow-         xml
cups                  gai.conf            idmapd.conf           make.conf.catalyst  nscd.conf            revdep-rebuild  shells
dbus-1                gconf               init.d                make.globals        nsswitch.conf        rmt             skel


It works on /etc/ subdirectories as well:
Code:
isilia@Gentoo ~ $ cruftcleaner /etc/X11/
Gentoolkit is installed... Continuing

Directory seems to be valid...
You have not specified a blacklist, I will continue to use the default blacklist at ~/.blacklist

We will now determine which files might be cruft:
/etc/X11/XvMCConfig
/etc/X11/xorg.conf.backup
Finished, think twice before deleting anything!

Code:
isilia@Gentoo ~ $ ls /etc/X11/
Sessions  XvMCConfig  chooser.sh  dm  gdm  startDM.sh  wmsession.d  xinit  xorg.conf  xorg.conf.backup  xorg.conf.example

Using a custom blacklist:
Code:
isilia@Gentoo ~ $ cruftcleaner /etc/X11/ custom_blacklist
Gentoolkit is installed... Continuing

Directory seems to be valid...
Will use specified blacklist at custom_blacklist
Warning! Your blacklist does not filter /etc/make.conf, are you sure it's valid?
Pausing for 5 seconds
Continuing...

We will now determine which files might be cruft:
/etc/X11/XvMCConfig
Finished, think twice before deleting anything!

Code:
isilia@Gentoo ~ $ cat custom_blacklist
xorg.conf.backup
Back to top
View user's profile Send private message
mirekm
Apprentice
Apprentice


Joined: 12 Feb 2004
Posts: 158
Location: Gliwice

PostPosted: Sun May 12, 2013 1:18 pm    Post subject: Reply with quote

Faster searh function for etc cruft:
Code:

#!/bin/bash

BASH_COLOR_RESET="\e[0m"
BASH_COLOR_GREEN="\e[01;32m"
BASH_COLOR_RED="\e[01;31m"

FILES_OK=0
FILES_BAD=0
FILES_TOTAL=0


echo "Search for all etc files in portage db"
find /var/db/pkg|grep CONTENTS|xargs cat |grep "obj /etc/"|cut -d" " -f2 |sort >/tmp/lista
echo "Search existing files in /etc/"
find /etc -type f|sort >/tmp/lista2
echo "Comparison of the results"
diff -U0 /tmp/lista /tmp/lista2|grep -v  "\@\@" |tail --lines +3|sort> /tmp/result




In /tmp/result you will get list of files:
- that don't belongs to any package - there are lines starting with "+"
- files that are missing - there are lines starting with "-"
Back to top
View user's profile Send private message
khayyam
Advocate
Advocate


Joined: 07 Jun 2012
Posts: 2245

PostPosted: Sun May 12, 2013 8:52 pm    Post subject: Reply with quote

mirekm wrote:
Code:
find /var/db/pkg|grep CONTENTS|xargs cat |grep "obj /etc/"|cut -d" " -f2 |sort >/tmp/lista

mirekm ... you could simplfy this, and avoid the double grep, UUOC, and cut ...

Code:
find /var/db/pkg -name CONTENTS -exec awk '/^obj[ ]\/etc/{print $2}' {} \; |sort > /tmp/out.lst

or to also filter .keep files:

Code:
find /var/db/pkg -name CONTENTS -exec awk '/^obj[ ]\/etc/ && !/.*keep.*/{print $2}' {} \; |sort > /tmp/out.lst


best ... khay
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum