LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 12-16-2011, 02:37 PM   #1
froff
Member
 
Registered: Nov 2005
Location: Poland
Distribution: Mint Mate, Ubuntu
Posts: 46

Rep: Reputation: 1
Question 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?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-16-2011, 03:12 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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.
 
2 members found this post helpful.
Old 12-16-2011, 03:28 PM   #3
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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
 
2 members found this post helpful.
Old 12-16-2011, 04:14 PM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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.
 
2 members found this post helpful.
Old 12-16-2011, 04:32 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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.
 
1 members found this post helpful.
Old 12-17-2011, 02:51 PM   #6
froff
Member
 
Registered: Nov 2005
Location: Poland
Distribution: Mint Mate, Ubuntu
Posts: 46

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by Dark_Helmet View Post
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.

Last edited by froff; 12-18-2011 at 05:04 AM.
 
  


Reply

Tags
chown, group


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Secondary group users need to change the file permissions of primary group files? sunnybmv Linux - Newbie 3 12-10-2009 05:57 PM
Setting a group to which all user should belong to. krzysiek Linux - Newbie 2 07-29-2008 06:32 AM
change group of ftp uploaded files GATTACA Linux - Networking 3 07-21-2008 09:06 PM
see which partitions belong to which volume group kpachopoulos Linux - General 1 03-10-2007 06:52 AM
Can a file belong to more than one group? lostboy Linux - General 2 11-08-2003 12:14 PM


All times are GMT -5. The time now is 02:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration