pfrenssen
pfrenssen

Reputation: 5515

Using -print0 with -o in find

I am using -print0 to modify the output of find to use NULL terminators instead of newlines. However I can't get this to work when using find's -o (OR) function.

This works fine, it prints out a newline-separated list of files that are either not owned by user 'pieter' OR not owned by group 'www-data':

find . ! -user pieter -o ! -group www-data

But when I append -print0 to this I get no output anymore:

find . ! -user pieter -o ! -group www-data -print0

This however works fine:

find . ! -user pieter -print0

What am I missing? I have tried adding various placements of pairs of parentheses but to no avail.

Upvotes: 9

Views: 2556

Answers (1)

leedm777
leedm777

Reputation: 24052

You're missing parens. Note that you have to escape them so the shell passes them on:

find . \( ! -user pieter -o ! -group www-data \) -print0

You can find out more about find on its man page, especially in the Examples section.

In case you're wondering why that's necessary, it's because of the order of operations. Every expression in find returns either true or false. If you don't put an explicit operator (-a, -o or ,) between the expressions, -a is assumed.

So, your original command is equivalent to find . ! -user pieter -o ! -group www-data -a -print0, which will only evaluate print0 on files owned by user pieter, but not group www-data.

Upvotes: 11

Related Questions