Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I copyied files from my MacBook, mainly files from /usr. The one and only user on my computer is called 'Julian' (like on my MacBook). A lot of the copied files are now owned by '501' (id of "Julian" from the MB).

drwxrwxr-x@  28 501     admin   952  8 Jul 23:02 man/

Is there a way to change the owner of all files owned by '501' to 'Julian'?

share|improve this question

migrated from stackoverflow.com Jul 10 '12 at 13:24

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers 3

up vote 3 down vote accepted

Sure, with the near-magical find command. The simplest way is like this:

find . -user 501 -exec chown Julian {} +

The "find" command is explained in the manpage, and in a number of tutorials and howtos like this one, nut the short idea is "find everything that matches these criteria, and do this command with each one".

  • . means to look in (and beneath) the current working directory.
  • -user 501 means to only find files owned by user 501.
    • Note that this is BSD-specific; in some other POSIX systems, user takes a username, and a separate uid flag takes numeric user IDs.
  • -exec … {} + means to run whatever's in the "…" (in this case, "chown Julian") repeatedly, passing it as many of the found files as possible.
    • So, if there are 5000 files, it may end up calling chown 4 times (on the first 1203, then on the next 1888, and so on).
    • Note that this is a BSD extension (although GNU has a similar extension); there is a portable equivalent with ; instead of +, but this will call chown once for each file, which will generally take a lot longer. (The standard solution is to use -print0 and pipe the result to xargs -0. But, since you're doing this on a BSD system, you don't have to do that.)
    • Note that if you're typing this in the shell, some shells will require you to escape the braces, but bash (the default shell on modern Macs) does not.
share|improve this answer
    
Bash doesn't care about empty braces. –  Dennis Williamson Jul 10 '12 at 0:39
    
@DennisWilliamson: Thanks; edited. –  abarnert Jul 10 '12 at 0:48
    
I suggest chown Julian: in order to update group ownership to Julian's default group as well. It's technically unnecessary when you own the files, but you never know how it can affect you down the road. –  Jason Salaz Jul 10 '12 at 19:31
    
And you probably need to through a sudo into the command somewhere, ideally in the front (before the find). –  patrix Jul 10 '12 at 20:25
add comment

This will do what you want:

find . -type f -uid 501 -print0 | xargs -0 sudo chown Julian

Explanation:

  • The . starts the search from the current directory.
  • The -type f requires only files to match, not directories. (Omit this if you want to change directories too.)
  • The -uid 501 requires all matches to be owned by this user.
  • The -print0 separates matches by nulls instead of spaces so that any path containing spaces is not misinterpreted.
  • The xargs -0 ensures that xargs input looks for null delimiters instead of spaces. Since chown can accept multiple files, xargs is used to ensure that sudo chown is run only as often as needed to affect every listed file. (In this respect it is much faster than an equivalent find ... -exec.)
share|improve this answer
1  
This works, but not idiomatic in BSD, and overly complex. "-uid" is a "for-compatibility-with-GNU-only" switch in BSD find, and print0/xargs -0 is unnecessary because BSD find has the "exec … +" formats. –  abarnert Jul 10 '12 at 0:28
add comment

Not sure if this will break things in OS X if the files are system related. But to change the owner of the files do a

sudo chown -R Julian <directory>

-R means the owner is changed for all files recusivly in the current dir. So be careful.

man chown should give you some additional details.

share|improve this answer
    
A file not owned by me placed within a folder owned by me would be broken afterwards (in regards of correct ownership). –  Julian Jul 9 '12 at 23:48
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.