5

I try to change owner to root:users recursively below a directory, if owner is other than root:users.

cd /dir/
find . \( ! -user root -o ! -group users \) -print0 | xargs -0 chown -vc root:users 

I get error:

chown: missing operand after ‘root:users’
Try 'chown --help' for more information.

Why I get the error? How can I fix it?

  • 1
    chown -R root:users /somedir should handle this just fine. – jordanm Jan 2 '19 at 2:04
13

Use the recursive switch on chown:

chown -R root:users dir

And that should do it.

More to why you have an error: if the find command doesn't find any files, then chown will be executed without an operand at the end, which generates this error.

If you are really intent on sticking with your original command format, you can add the -r switch to xargs and it should get rid of the error when no files are found.

5

In addition to the observation that you can use the -R flag to chown, the reason why you are getting the error message is almost certainly[1] due to you having no files which need to be changed. The gnu version of xargs has an extra flag -r to tell xargs not to run the command if there is no input to the command.

However using xargs is the wrong approach, a POSIX version of find will batch up commands for you. Use

find . \( ! -user root -o ! -group users \) -exec chown -vc root:users {} +

note the final + rather than the often seen \; in examples of using -exec. With \; the command is run once per matching file, but with + files are grouped and run in batches.

There are some cases where xargs should still be used, for example if you wanted to read from the input and process the lines in pairs, but these are rare. Almost always you want to either run a command per file or else batch up as many files as possible and run one command per batch.

[1] Depending on the xargs implementation, you might have exactly filled a number of command buffers with files, but much more probable is no files at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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