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:- Do a find (see "man find") beginning at the directory "/" (i.e., the root directory)
- (find) all files that are owned by the user "foo" (if "foo" was removed from the system/netinfo, in place of "foo," you could use the owner's number that would be displayed by a long listing (ls -l)
- for each file found, execute/run the program 'chown' (change ownership, see "man chown"), which changes the ownership to the user "bar"
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.:
- find / -group foo -exec chgrp bar {} \; to similarly change group ownership
- find . -user foo -o -group bar -exec chown fiddle:faddle {} \; to find files, starting in my current working directory, that are owned by user foo *or* (the -o) group bar and change them to be owner/group fiddle/faddle, respectively
- find . -name "*foo*" -exec chown me {} \; -exec chmod 666 {} \; to find all files named "something / nothing" + foo + "something / nothing" (single or double quotes or "\"s are required if there are special characters [like *] or spaces in the name) then change the ownership to "me" and (executing a second, different command) change the mode to be read/write access for all (see "man chmod")
- find . -type d -a -user me -exec chmod o-rwx {} \; to find all files of type directory (i.e., all folders) *and* that are owned by the user "me" then run change mode to remove (if present) the "others" permissions for read, write, and execute (well, execute is search permission, if it's a directory)
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!). #;-)

