[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: GNU find: "print0" and "-type" arguments

Martin Steigerwald wrote:
> martin@merkaba:~/Zeit/find-Test> find \( -type d -print \) -o \( -name "file" -printf  "%s %p" \) -o \( -name "anotherfile" -print0 \)
> .
> ./anotherfile./dir
> 0 ./file%                                                                                                                                    
> martin@merkaba:~/Zeit/find-Test>

It is inconsistent to mix -print0 with -print and -printf.  Just use
one or the other consistently.

> Which is the same as without braces:
> martin@merkaba:~/Zeit/find-Test> find -type d -print -o -name "file" -printf "%s %p" -o -name "anotherfile" -print0
> .
> ./anotherfile./dir
> 0 ./file% 


> Now I am wondering about the order.
> Why does find print "another file" before ".dir" and "file" after "another 
> file"?

You seem to be missing the basic operation of find.  The find program
iterates across ever file and processes arguments from left to right
for that file.  As long as the action returns true then find continues
to process arguments from left to right.  If any argument returns
false then processing stops for that file.  Find then proceeds to the
next file and restarts processing arguments for the next file from
left to right.

  find -type d -print -o -name "file" -printf "%s %p" -o -name "anotherfile" -print0

For every file find processes it walks across the argument list.  For
your example arguments it is something like this:

  for each file do
    if type d then
      if name "file" then
        printf "%s %p"
        if name "anotherfile" then

Also 'find' walks through the directory in the order of the entries in
the list.  It doesn't sort the entries first.  This means that they
are in an arbitrary order.  They might appear in any order but the
order will be repeatable for that particular directory.


Attachment: signature.asc
Description: Digital signature

Reply to: