find & chmod & xargs – changing uid of files

Because of the system reinstallation & user ids remapping, I wanted to update all of the files from given UID to new UID. One of the ways to do this is to find all files with given UID and perform chown on these files. You could do this one by one, by find . -uid 1000 -exec chown {} \;, but it will be very slow.

You could also use “xargs”, but if you have file names with spaces (and newlines for instance!), then xargs will fail (because it separates arguments by space).

There is a way to inform xargs, that arguments are separated by null characters instead of whitespace. “-0″(zero). There is also a way to separate them like this in find.

find . -uid 1000 -print0 | xargs -0 chown nowy-user

or even better, if you have symbolic links, which will modify ownership of the link, not of the target of the link:

find . -uid 1000 -print0 | xargs -0 chown -h nowy-user

Leave a Reply