An introduction to the 'find' command

Feb 05, '03 09:42:06AM

Contributed by: Anonymous

This is something I wrote in response to the following question on a list:

Could anyone recommend a good command line method (or point me to a previous discussion topic) for globally finding and replacing file ownership? One of our administrators has recently moved on, and I would like to revert ownership of the project files he worked on to the admin user before deleting the user account.
Finding that the find command is often one of the lesser used commands by newbies, here was my response (which I hope others will find useful)...

[Editor's note: Read the rest of the article for some interesting uses and explanations of the "find" command. I modified the formatting of this hint to make it more readable, but the content is as it arrived here.]

Precede the following command(s) with sudo, if required (or use sudo -s to get a privileged shell, first).

To find/replace user ownership (where "foo" is the current user and "bar" is the desired/changed-to user):

 % find / -user foo -exec chown bar {} \;
This says:NOTE: the "funny syntax" (that's funny weird, not funny ha-ha) of "{}" says "insert the pathname of the found file" (properly quoted, b.t.w.) and the "\;" is to signify the end of the "-exec" command (actually, the ";" is the terminator and the "\" is to 'escape' it in [have it ignored by] the shell as it would otherwise have meaning to the shell and be 'gobbled up' by the shell, thus breaking the find command)

Now that you understand that much, you're over the hard part! There are many options that allow a myriad of variants to the find command (learn the find command ... it's a serious, capable, many faceted "friend") -- e.g.:You should be getting the idea that the find command is very versatile, powerful, useful, etc. (if you're into command-line things, that is). It's syntax is a little nasty, but it's actually worth learning. HINT: when learning (or even after you know enough to be real dangerous), it's often worthwhile to run the command without the -exec(s) *first* to ensure that you're not going to do something you didn't plan on. The command can be interrupted with CTRL-C, so you don't need to let it run to completion, once you see it's doing what you wanted it to.

A note about performance -- each applied "-exec" causes a new process to be run, so expect this to take some time on large numbers of found files. Sometimes there are other ways of doing things that may be blindingly faster. For example, if you want to reset the ownership on a set of folders and all their nested files and folders, doing chown -R foo folder1 folder2 ... will be orders of magnitude faster (and easier) than its find equivalent:
 % find folder1 -exec chown foo {} \
% find folder2 -exec chown foo {} \;
...
find without any qualifying arguments will find everything, recursively. Of course, the find approach can select within that heirarchy of folders. #:-)

Anyway, this is probably much more than you wanted to know (it's the consultant-come-mentor in me, I guess).

Hope this helps (now go lean more about find, chown, chgrp, chmod, as these are pretty useful ... and dangerous!). #;-)

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030205064206934