Display find results, and pipe to xargs

I have an overnight script which runs across a large directory to repair permissions and ownership. I also have this command output the list of files affected so that cron can email these as a log file. Previously I had the command in the form:

find /path/to/files -not -user myname -print -exec chown myname "{}" \;

As it is a long command sometimes I never receive a log email even though the ownership and permissions have changed, so I want to make the script more efficient. As such I have changed the command to:

find /path/to/files -not -user myname -print0 | xargs -0 chown myname

Is there anything I can do to that command so that it sill lists the files on standard output so that they will be emailed by cron in addition to piping them to xargs?

I cannot include the -print action in the find command as this will also be piped to xargs. Likewise if I include an -exec action with an echo command. Also I do not want to use the -t option with xargs as I just want the list of files and not the chown commands it generates.

The best solution I have been able to come up with is to use tee to ssave output in a temporary file to display afterwards, but I am hoping there is better solution.

Thanks,

Michael.

Try:

find /path/to/files -not -user myname -print0 | xargs -i -0 sh -c 'chown myname {}; echo {}'

I have just tried that, and although it works, it is executing the commands individually for each file. Presumably this means it is not offering any performance advantage over that just using find's -exec action, if not less because of having to spawn the shell as well? The only reason for switching to use xargs was so that it can pass multiple files to chown reducing the number of times it would be called.

Thanks for the help though.

Michael.

find /path/to/files -not -user myname -print -exec chown myname "{}" +
1 Like

That is exactly what I wanted. Everything tested okay and I have been able to update my script.

I feel so stupid now. I have read the find man page so often I should have known that.

Many thanks,

Michael.