easy question about piping "find | chgrp" - syntax?

I want to find all occurrences of a file owned by a specific user and change the group affiliation of every instance of those files (owned buy that user).

I tried "sudo find / -user {userName} | chgrp {newGroup}" but it didn't work...all that happened was that Terminal spit out a nastygram saying "usage: chgrp [-fhv] [-R [-H | -L | -P]] group file ...."

Seems like these should be "pipeable" commands, and my problem just has to be something stupid with my lack of knowledge as to how to correctly syntax the desired pipe.

Anybody got any ideas?

Thanx.

Posted on Nov 7, 2005 9:27 AM

Reply
4 replies

Nov 7, 2005 10:56 AM in response to j.v.

Hi j.v.,
"pipeable"? That's new and as good a word as I can think of but it not sufficiently specific. In order to be "pipeable", at least on the receiving end, a utility must accept arguments from STDIN and that's not trivial. It's not difficult but aside from the work, it takes some planning to decide how to combine arguments coming from both the command line and STDIN.

Still, this is a fairly standard situation and chgrp can't be very well written. I checked, it definitely doesn't handle arguments from STDIN. However, chown does. Therefore, aside from Jeff's suggestion, you can use the pipe with chown, like:

find / -user {userName} | sudo chown :{newGroup}

That of course brings another point. Sudo doesn't apply escalated privileges to the entire pipe so it is necessary to apply sudo where it's needed. If both parts of the pipe have to run as root, you can use something like:

sudo bash -c "find / -user {userName} | sudo chown :{newGroup}"

Finally, if there are lots of files there's a chance of overloading the argument capacity of chown. In this case you would have to use chown with xargs. I don't suppose there much point in going into it; Jeff's solution is sounding pretty good about now, eh? Still, I wanted you to know the possibilities.
--
Gary
~~~~
The attacker must vanquish; the defender need only survive.

Nov 7, 2005 12:32 PM in response to Jeff Breitner

Hello together,

an alternative to using "-exec ls -l {}" is "-ok <command> {}" - in this case:
find / -user [userName] -ok chgrp [newGroup] {} \;

From the man page of find:
The -ok primary is identical to the -exec primary with the excep-
tion that find requests user affirmation for the execution of the
utility by printing a message to the terminal and reading a
response. If the response is other than ``y'' the command is not
executed and the value of the -ok expression is false.


Hope this will be useful.
Regards
Martin

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

easy question about piping "find | chgrp" - syntax?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.