Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Discussion & Documentation Documentation, Tips & Tricks
  • Search

Checking for stale /etc files

Unofficial documentation for various parts of Gentoo Linux. Note: This is not a support forum.
Post Reply
Advanced search
19 posts • Page 1 of 1
Author
Message
TheCoop
Veteran
Veteran
User avatar
Posts: 1814
Joined: Sat Jun 15, 2002 5:20 pm
Location: Where you least expect it
Contact:
Contact TheCoop
Website

Checking for stale /etc files

  • Quote

Post by TheCoop » Sat Dec 20, 2003 9:19 pm

Due to the portage /etc protection, and package that installs files in /etc and is then unmerged leaves the files there. Over time /etc can get very cluttered. Therefore here is a very simple one-liner that will print out the paths of all the files in /etc that do not belong to any package. However, beware of deleting them, as deleting some files in /etc may seriously damage your health as you try to find a livecd to boot from

Code: Select all

for n in /etc/*; do if [[ `qpkg -f $n` = "" ]]; then echo $n; fi; done
You can also change it to run under /etc/init.d or /etc/conf.d, cos atm it doesn't do recursive checks

ps. I know most of you probably would have got this by now, but there are a fair few, including me as of 10 mins ago, dont know the first thing about bash scripting and therefore would not be able to do this. Just bear with me, will you? :D
Last edited by TheCoop on Sat Dec 20, 2003 11:38 pm, edited 1 time in total.
95% of all computer errors occur between chair and keyboard (TM)

"One World, One web, One program" - Microsoft Promo ad.
"Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler

Change the world - move a rock
Top
frekiR
n00b
n00b
Posts: 42
Joined: Mon Dec 15, 2003 3:05 pm
Location: Sweden

  • Quote

Post by frekiR » Sat Dec 20, 2003 11:26 pm

Very nice, good job :)
Top
neenee
Veteran
Veteran
User avatar
Posts: 1786
Joined: Sun Jul 20, 2003 12:15 pm

  • Quote

Post by neenee » Sat Dec 20, 2003 11:42 pm

thanks for sharing this. i too do not know enough
about many things in linux myself :wink:
Top
Tazmanian
Apprentice
Apprentice
User avatar
Posts: 222
Joined: Tue Jul 01, 2003 9:11 am

Re: Checking for stale /etc files

  • Quote

Post by Tazmanian » Sun Dec 28, 2003 12:12 am

TheCoop wrote:

Code: Select all

for n in /etc/*; do if [[ `qpkg -f $n` = "" ]]; then echo $n; fi; done
You can also change it to run under /etc/init.d or /etc/conf.d, cos atm it doesn't do recursive checks
A modified version that checks /etc recursively and only queries packages that you have installed:

Code: Select all

for n in `find /etc -type f` ; do
    if [[ `qpkg -I -f $n` = "" ]] ; then
        echo $n
    fi
done
Top
BlindSpy
Apprentice
Apprentice
User avatar
Posts: 263
Joined: Sat Mar 20, 2004 4:17 pm

  • Quote

Post by BlindSpy » Tue Apr 26, 2005 5:05 pm

Awesome work guys! I've been looking for something like this since ecatmur's cruft script has stopped working for me =).
Symlinks to:
xorg.conf
Top
Hack_Benjamin
Apprentice
Apprentice
Posts: 158
Joined: Fri Nov 12, 2004 11:24 pm

  • Quote

Post by Hack_Benjamin » Wed Jul 13, 2005 11:36 am

if i wanted to print what the output was to a file instead of just showing it to me in the term how would i do it?
Top
extragedy
n00b
n00b
Posts: 10
Joined: Thu May 19, 2005 5:55 pm

  • Quote

Post by extragedy » Thu Jul 14, 2005 12:43 am

you can always put ">> your file" without the quotes at the end of the echo file
Top
Hack_Benjamin
Apprentice
Apprentice
Posts: 158
Joined: Fri Nov 12, 2004 11:24 pm

  • Quote

Post by Hack_Benjamin » Thu Jul 14, 2005 11:16 am

sorry, i thought it would be obvious :P

my plan is to hav that script, then save its output and then open an editor to remove what you wish to keep and then do something like:

su -c
for i in cat `file` rm $i


ace.


great script
Top
Grilo
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 114
Joined: Tue Apr 22, 2003 3:15 am
Location: Canada

  • Quote

Post by Grilo » Fri Jul 15, 2005 5:15 pm

Thanks for the script. I thought you should be able to tell it how to skip the files you knew for sure you shouldnt delete. So Luke MacGregor (dpl) modified the script. here it is

Code: Select all

#!/bin/bash
# this is the list of files that you don't want to trash, make sure they're
# in quotations.

KEEP_FILES=("/etc/make.conf" "/etc/X11/Sessions/e17" "/etc/mtab")

NUM_FILES=${#KEEP_FILES[*]}

BOOL=1

OUTPUT="etc-check.output"

# first remove the output file if it is there.
if [ -e $OUTPUT ]; then
       rm $OUTPUT
fi

for n in `find /etc -type f` ; do
   BOOL=1
   if [[ `qpkg -I -f $n` = "" ]] ; then
       for i in ${KEEP_FILES[@]} ; do
               if [[ $n == ${i} ]] ; then
                       BOOL=0
                       echo not inserting $n
               fi
       done
       if [[ $BOOL == "1" ]] ; then
               echo $n >> $OUTPUT
       fi
   fi
done
in the keep files just list the ones to skip.

this is new and improved. it will check to see if you have an output file then delete it so that you have a new clean list after being run


good luck
Grilo
Knowledge is power but the drive to learn is harnessing wizdom
Top
rickj
Guru
Guru
User avatar
Posts: 432
Joined: Thu Feb 06, 2003 8:30 pm
Location: Calgary, Alberta, Canada
Contact:
Contact rickj
Website

  • Quote

Post by rickj » Sun Apr 30, 2006 6:40 pm

Could some knowledgeable person please update these scripts, in the light of the fact that qpkg has gone the way of the KSR33 Teletype?
Top
TheCoop
Veteran
Veteran
User avatar
Posts: 1814
Joined: Sat Jun 15, 2002 5:20 pm
Location: Where you least expect it
Contact:
Contact TheCoop
Website

  • Quote

Post by TheCoop » Sun Apr 30, 2006 10:53 pm

Code: Select all

#!/bin/bash
# this is the list of files that you don't want to trash, make sure they're
# in quotations.

KEEP_FILES=("/etc/make.conf" "/etc/X11/Sessions/e17" "/etc/mtab")

NUM_FILES=${#KEEP_FILES[*]}

BOOL=1

OUTPUT="/tmp/etc-check.output"

# first remove the output file if it is there.
if [ -e $OUTPUT ]; then
       rm $OUTPUT
fi

for n in `find /etc -type f` ; do
   BOOL=1
   if [[ $(equery -q -C b $n) = "" ]] ; then
       for i in ${KEEP_FILES[@]} ; do
               if [[ $n == ${i} ]] ; then
                       BOOL=0
                       echo not inserting $n
               fi
       done
       if [[ $BOOL == "1" ]] ; then
               echo $n >> $OUTPUT
       fi
   fi
done
It takes quite a while though. Best to leave it running in a detached screen or something (sh etc-check.sh && beep)
95% of all computer errors occur between chair and keyboard (TM)

"One World, One web, One program" - Microsoft Promo ad.
"Ein Volk, Ein Reich, Ein Führer" - Adolf Hitler

Change the world - move a rock
Top
think4urs11
Bodhisattva
Bodhisattva
User avatar
Posts: 6659
Joined: Wed Jun 25, 2003 9:51 pm
Location: above the cloud

  • Quote

Post by think4urs11 » Mon May 01, 2006 12:45 pm

Tested it on two machines and must say it is slow even when comparing to trees getting older ;)

times for TheCoop-script
a) P4-2400: 120:11 min
b) Via Eden 600: stopped after 656 minutes (approx. 50% done)

my script
a) P4-2400: 4:11 minutes
b) Via Eden 600: 37:57 minutes

all whats needed to speed it up is
replace

Code: Select all

   if [[ $(equery -q -C b $n) = "" ]] ; then
with

Code: Select all

if [[ $(qfile $n) = "" ]] ; then
qfile is part of app-portage/portage-utils

HTH
Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself
Top
dmartinsca
Guru
Guru
Posts: 303
Joined: Fri Dec 09, 2005 2:01 pm
Location: Ontario, Canada

  • Quote

Post by dmartinsca » Mon May 01, 2006 5:28 pm

Think4UrS11 wrote:Tested it on two machines and must say it is slow even when comparing to trees getting older ;)

times for TheCoop-script
a) P4-2400: 120:11 min
b) Via Eden 600: stopped after 656 minutes (approx. 50% done)

my script
a) P4-2400: 4:11 minutes
b) Via Eden 600: 37:57 minutes

all whats needed to speed it up is
replace

Code: Select all

   if [[ $(equery -q -C b $n) = "" ]] ; then
with

Code: Select all

if [[ $(qfile $n) = "" ]] ; then
qfile is part of app-portage/portage-utils

HTH
How many files in /etc and how many packages do you have installed?
find /etc -type f | wc -l
find /var/db/pkg -name "CONTENTS" | wc -l

I'm working on a script that just uses grep on the CONTENTS files in /var/db/pkg, doesn't look like it's going to be much faster though.
Top
think4urs11
Bodhisattva
Bodhisattva
User avatar
Posts: 6659
Joined: Wed Jun 25, 2003 9:51 pm
Location: above the cloud

  • Quote

Post by think4urs11 » Mon May 01, 2006 5:48 pm

dmartinsca wrote:How many files in /etc and how many packages do you have installed?
(values for Via Eden)

find /etc -type f | wc -l
1097
find /var/db/pkg -name "CONTENTS" | wc -l
298
output of my script:
337
Nothing is secure / Security is always a trade-off with usability / Do not assume anything / Trust no-one, nothing / Paranoia is your friend / Think for yourself
Top
BitJam
Advocate
Advocate
Posts: 2513
Joined: Tue Aug 12, 2003 4:15 pm
Location: Silver City, NM

  • Quote

Post by BitJam » Mon May 01, 2006 10:44 pm

Here is a very fast Perl program that will find cruft in /etc/ and its subdirectories. It is very fast because it reads the CONTENTS files just once and puts possible target files (from /etc) in a hash.

It works fine as is, but it could use a bit more work. The list of IGNORE files could probably be larger and I want to add a feature so it can read in additional ignore files both from the command line and from a file so each person can customize which files to ignore on their own system without modifying the code.

It has two limitations, both of which are easy to fix. The first is that it will fail for filenames with spaces in them in the CONTENTS files. Second, it only searches for beginning matches for files from the IGNORE list which makes it very easy to exclude directories. There should probably be an IGNORE_DIR list that still behaves this way and an IGNORE_FILE list that only ignores exact matches.

I am posting it now because it works and is very fast but I'm tired now and may not get around to adding those other features right away.

Code: Select all

#!/usr/bin/perl -w 

use strict;
use Getopt::Long;

my $TARGET = "/etc";
my $DB_DIR = "/var/db/pkg";
my @IGNORE = ("/etc/OLD_ETC", 
              "/etc/splash", 
              "/etc/runlevels",
              "/etc/ssl/certs",
              "/etc/X11/xorg.conf",
              "/etc/group",
              "/etc/shadow",
              "/etc/gshadow-",
              "/etc/fstab",
              "/etc/localtime",
              "/etc/make.conf",
              "/etc/passwd",
              "/etc/resolv.conf",
              "/etc/hosts",
              );

my $ME = $0; $ME =~ s{.*/}{};
my $USAGE = <<USAGE;
Usage: $ME [options]
Options:
  --target DIR   Look for cruft in DIR (default: /etc)
  --db_dir DIR   Look for package files in DIR (default: /var/db/package)
  --pretend      Does nothing yet.
  --verbose      Prints a few summary lines.
  --help         Print this usage.
USAGE

my $PRETEND;
my $VERBOSE;

GetOptions(
    "help"    => sub { print $USAGE; exit},
    "pretend" => \$PRETEND,
    "verbose" => \$VERBOSE,
    "target"  => \$TARGET,
    "db_dir"  => \$DB_DIR,
) or die $USAGE;

#--- Grab valid files from all contents files ---
my %VALID;

for my $dir (<$DB_DIR/*>) {
    for my $package (<$dir/*>) {
        open(CONTENTS, "$package/CONTENTS") or next;
        while (<CONTENTS>) {
            m/^(?:obj|dir) ($TARGET\S*)/o and $VALID{$1}++;
        }
        close CONTENTS;
    }
}

$VERBOSE and print "Found @{[scalar keys %VALID]} valid file names.\n";

my @FILES = `find $TARGET -print`;
chomp(@FILES);

$VERBOSE and print "Found @{[scalar @FILES]} target files.\n";

my @CRUFT;
FILE:
for my $file (@FILES) {
    $VALID{$file} and next FILE;
    for my $re (@IGNORE) {
        $file =~ /^$re/ and next FILE;
    }
    push @CRUFT, $file;
}

print join "\n", @CRUFT, "\n";

$VERBOSE and print "Found @{[scalar @CRUFT]} cruft files.\n";
Top
mikemcquaid
Tux's lil' helper
Tux's lil' helper
Posts: 108
Joined: Tue May 11, 2004 1:50 pm
Location: Edinburgh, Scotland

  • Quote

Post by mikemcquaid » Sun May 07, 2006 11:35 pm

BitJam:
Can't find string terminator "USAGE" anywhere before EOF at ./etccheck line 25.
Top
BitJam
Advocate
Advocate
Posts: 2513
Joined: Tue Aug 12, 2003 4:15 pm
Location: Silver City, NM

  • Quote

Post by BitJam » Mon May 08, 2006 12:05 am

Make sure that line 34 (that just contains the string "USAGE") has no trailing whitespace.
Top
electrofreak
l33t
l33t
User avatar
Posts: 713
Joined: Wed Jun 30, 2004 2:11 am
Location: Ohio, USA
Contact:
Contact electrofreak
Website

  • Quote

Post by electrofreak » Sat Jun 10, 2006 9:07 pm

Code: Select all

bash: qpkg: command not found
The perl script works just fine, but seems to spit out WAY to many files.
Desktop: ABit AN8, Athlon64 X2 4400+ 939 2.75GHz, 2x1GB Corsair XMS DDR400, 2x160GB SATA RAID-0, 2x20"W, Vista Ultimate x64
Laptop: 15.4" MacBook Pro 2.4Ghz, 2x1GB RAM, 160GB, Mac OS X 10.5.1
Server: PIII 550Mhz, 3x128MB RAM, 160GB, Ubuntu Server 7.10
Top
ttuttle
Tux's lil' helper
Tux's lil' helper
Posts: 131
Joined: Sun Oct 03, 2004 8:44 pm
Contact:
Contact ttuttle
Website

  • Quote

Post by ttuttle » Tue Dec 26, 2006 5:32 pm

Try this:

Code: Select all

find /etc | sort > ~/etc-actual
find /var/db/pkg/ -name CONTENTS | xargs -n 1 cat | cut -d" " -f 2 | grep "^/etc" | sort | uniq > ~/etc-intended
diff ~/etc-actual ~/etc-intended | less
Files marked "< file" are files you have but aren't listed in any package; files marked "> file" are files that are listed in a package but no longer on your system (perhaps example config files you renamed or removed).

It will, like most of these scripts, flag files like /etc/X11/xorg.conf that are obviously important but don't actually come with any package. As the script does not remove anything, it's up to you to remove what you don't want and keep what you do.
Visit my website.
Top
Post Reply

19 posts • Page 1 of 1

Return to “Documentation, Tips & Tricks”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic