chown: missing operand after ‘root:users’

28,641

Solution 1

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.

Solution 2

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.

Share:
28,641

Related videos on Youtube

klor
Author by

klor

Updated on September 18, 2022

Comments

  • klor
    klor almost 2 years

    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?

  • Nikhath K
    Nikhath K over 5 years
    If you're going to say "note the..."; you should explain what the difference is. I assume it's batching? But it would be clearer if you stated that explicitly.
  • icarus
    icarus over 5 years
    @RogerLipscombe Better? pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html is the current standard.