On Sunday, July 17 at 17:47 (-0700), Grant said:

> ran this and the output was voluminous but looked good:
> 
> /usr/bin/find /home/user -type f -name "*-`/bin/date -d 'yesterday'
> +\%Y\%m\%d`*.jpg"
> 
> So I ran it again, adding -delete right before -type.  After a lot of
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

That was a mistake.

> processing I got a line of output like this for each file:
> 
> /usr/bin/find: `/home/user/1-2011071612345.jpg': No such file or
> directory
> 
> Unfortunately the command actually deleted the entire /home/user
> folder.  Can anyone tell me what went wrong?  Maybe '/home/user' was
> at the very top of the long list that scrolled up the screen when I
> ran the find command without -delete?
> 
Well this is an unfortunate way to learn how find works.  A better way
would be:

$ man find

Basically find works of a chain of selection criteria.  It crawls all
the files/dirs and when one item in the chain is true for the criteria,
it checks for the other.  For example

$ find /path -type f -name blah -print

Crawls /path, for each file/dir it checks if it is a regular file (-type
f), if that is true, it checks if it's name is "blah", if that is true,
it prints the name (blah).

Therefore, 

$ find /path -delete -type f -name ....

Crawls path, then checks "-delete".. but wait, -delete evaluates to
"true if removal succeeded" (find(1)), so it deletes the file, then
checks to see if it is a regular file, then if that is true then it
checks the name... but all that doesn't matter because your files are
deleted.

You should never put -delete at the beginning of a chain and, arguably,
you shouldn't use -delete at all.  It even says in the man page:

        Warnings:  Don't  forget that the find command line is evaluated
        as an expression, so putting -delete first will make find try to
        delete everything below the starting points you specified.  When
        testing a find command line that you later intend  to  use  with
        -delete,  you should explicitly specify -depth in order to avoid
        later surprises.  Because -delete  implies  -depth,  you  cannot
        usefully use -prune and -delete together.





Reply via email to