![]() |
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? |
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/* |
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 groupnameCode:
find /path/to/dir -group groupname | xargs chown user:groupnameman find |
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. |
I'd have done something like this:
Code:
find /path/to/search/ -group 'old-group' -exec chown ':new-group' {} \; |
Quote:
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 10:07 PM. |