Change owner for all files owned by x

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.

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.)