Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

On several of our CentOS 5 systems, the /etc/group file was munged by an automated script.

As a result, there are some files on the system which have an undefined GID-- the GID is not defined in /etc/group. For example, the following file is owned by GID 103, when it should be owned by the group 'mysql'.

# ls -ld /var/lib/mysql/mysql/user.frm 
-rw-rw---- 1 mysql 103 10330 Apr  1 02:47 /var/lib/mysql/mysql/user.frm

Is there a way for me to find ALL files on this system which are owned by undefined groups?

I know I can do something like this to find all files owned by GID 103:

find / -gid 103

However, I want to find ALL files which are owned by an undefined group.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Doesn't your version of find have the -nogroup option?

Here is a perl one-liner:

perl -MFile::Find -e 'find({ wanted => sub { getgrgid((stat(_))[5]) or \
  print ((stat(_))[5], " $File::Find::name\n") }, follow => 1 }, "/")'
share|improve this answer
2  
Despite the other accepted answer, this one really is the -right- one. Using the -nogroup flag with find will return all files with which the GID doesn't map to an actual group. Exactly what the poster was looking for. –  Christopher Karel Aug 4 '10 at 19:52
1  
Wow, I wonder why I have never noticed this option before. –  Zoredache Aug 4 '10 at 19:58
    
You win! I had no idea that option existed. I even scanned the manpage and missed this option. –  Stefan Lasiewski Aug 4 '10 at 22:55

you may try a quick and dirty solution like:

find .  -ls | gawk '$6 ~ /^[0-9]+$/ {print}'

it's definitely not a beauty, but should work.

share|improve this answer
    
I like that better than what I was coming up with :) –  Alex Aug 4 '10 at 18:33

If you have -gid but not -nogroup you may be able to use this or something like it:

gids=($(cut -d: -f3 /etc/group | sed 's/.*/! -gid & /'))
find /dir/to/start ${gids[@]} -ls
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.