5

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'?

3 Answers 3

8

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.
3
  • Bash doesn't care about empty braces.
    – Dennis Williamson
    Commented Jul 10, 2012 at 0:39
  • 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. Commented Jul 10, 2012 at 19:31
  • 1
    And you probably need to through a sudo into the command somewhere, ideally in the front (before the find).
    – nohillside
    Commented Jul 10, 2012 at 20:25
4

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.)
1
  • 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
    Commented Jul 10, 2012 at 0:28
1

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.

1
  • A file not owned by me placed within a folder owned by me would be broken afterwards (in regards of correct ownership).
    – Julian
    Commented Jul 9, 2012 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .