LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Conditionally change group ID (https://www.linuxquestions.org/questions/linux-general-1/conditionally-change-group-id-859103/)

JohnE1 01-27-2011 03:47 PM

Conditionally change group ID
 
I have a set of files that have a group ID number where no group exists in the system with that number.

I want to recursively read the group IDs of all (including hidden) files in the current and sub-dirs, test each file's group ID, and if it equals the search GID number, I want to execute a chgrp command on that file.

Anyone have an admin script already made for this task?

Thanks in advance!

tommylovell 01-27-2011 04:03 PM

'cd' to the directory that you want to start in.

Then try
Code:

find . -group xxxx -exec ls -l {} \;
where xxxx is the "bad" GID. This will show you all of the files with that GID.

If you're happy with the list
Code:

find . group xxxx -exec chgrp yyyy {} \;
to change xxxx to yyyy.

Or, if you want to confirm each one
Code:

find . group xxxx -ok chgrp yyyy {} \;
which could be tedious...

JohnE1 01-27-2011 05:29 PM

Thanks, Tommy! I'll give that a try and post back here.

PTrenholme 01-27-2011 07:09 PM

When I've had the problem, I've just created a group with the "unassigned" number, and proceeded from there. But the find solution should suggested by johnE1 will also work.

JohnE1 01-27-2011 09:16 PM

SOLVED: Conditionally change group ID
 
Tommy, your examples were very helpful. Thank you!

Here's what I ended up using.

To verify find results:
find . group xxxx -print0 | xargs -0 ls -AL

To change group of each file (and sub-directory) found:
find . group xxxx -print0 | xargs -0 chgrp yyyy

NOTE: Those are zeroes (0's).

tommylovell 01-27-2011 09:34 PM

Cool. Your way is an excellent alternative, especially (I think) when there are a very large number of files to change.
As the Perl people say, "Tim Toady" - There's More Than One Way To Do It (TMTOWTDI).


All times are GMT -5. The time now is 06:52 PM.