LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   how to change files group but only if they belong to specified group ? (http://www.linuxquestions.org/questions/linux-general-1/how-to-change-files-group-but-only-if-they-belong-to-specified-group-919103/)

froff 12-16-2011 02:37 PM

how to change files group but only if they belong to specified group ?
 
hello
I would like to change group of set of files but only group1 to group2 - files with other group than group1 should remain untouched. Is it possible in any easy way?

Dark_Helmet 12-16-2011 03:12 PM

Yes. The chown command can do what you want (man chown and look at the "--from" and "-R" options).

As an example, pretend you have a directory that contains all the files you need to change named /home/projects/projB (which includes subdirectories) and that you want to change files owned by "groupa" to "alphag". This command should work:
Code:

chown --from=:groupA -R :alphag /home/projects/projB/*
Do not run this on your important files until you have tested it first.

lithos 12-16-2011 03:28 PM

or you can first find the files of specified group and then re-own with another if you want:
Code:

find /path/to/dir -group groupname
to change the owner the complete command would be:
Code:

find /path/to/dir -group groupname | xargs chown user:groupname
good luck

man find

Dark_Helmet 12-16-2011 04:14 PM

Keep in mind that lithos's approach will work, but has some potential problems.

1. If you have a LOT of matching files, then piping find's output to xargs could create a "line too long" error for the chown. This could be worked around by using find's "-exec" option.

2. The form of the chown command: "chown user:group <file>" will overwrite the owner of the file. If the original owner needs to be retained, omit the "owner" part (for example: "chown :group <file>")

Not criticizing--just as an fyi.

Because, as lithos mentioned, you can use "find /path/to/dir -group groupname" as a test to see what files will be modified--as opposed to creating a testing environment to be certain my command will do what you need.

Telengard 12-16-2011 04:32 PM

I'd have done something like this:

Code:

find /path/to/search/ -group 'old-group' -exec chown ':new-group' {} \;
I don't have enough experience with xargs to know when to use it or not.

froff 12-17-2011 02:51 PM

Quote:

Originally Posted by Dark_Helmet (Post 4551909)
Yes. The chown command can do what you want (man chown and look at the "--from" and "-R" options).

As an example, pretend you have a directory that contains all the files you need to change named /home/projects/projB (which includes subdirectories) and that you want to change files owned by "groupa" to "alphag". This command should work:
Code:

chown --from=:groupA -R :alphag /home/projects/projB/*
Do not run this on your important files until you have tested it first.


Thanks a lot!
I investigated only options of chgrp thinking that it is more appropriate. It was my mistake.


All times are GMT -5. The time now is 12:13 PM.