Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using -print0 with -o in find

Tags:

find

bash

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.

like image 891
pfrenssen Avatar asked Aug 05 '11 13:08

pfrenssen


1 Answers

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.

like image 120
leedm777 Avatar answered Oct 17 '22 15:10

leedm777