1 |
On Sunday, July 17 at 17:47 (-0700), Grant said: |
2 |
|
3 |
> ran this and the output was voluminous but looked good: |
4 |
> |
5 |
> /usr/bin/find /home/user -type f -name "*-`/bin/date -d 'yesterday' |
6 |
> +\%Y\%m\%d`*.jpg" |
7 |
> |
8 |
> So I ran it again, adding -delete right before -type. After a lot of |
9 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
10 |
|
11 |
That was a mistake. |
12 |
|
13 |
> processing I got a line of output like this for each file: |
14 |
> |
15 |
> /usr/bin/find: `/home/user/1-2011071612345.jpg': No such file or |
16 |
> directory |
17 |
> |
18 |
> Unfortunately the command actually deleted the entire /home/user |
19 |
> folder. Can anyone tell me what went wrong? Maybe '/home/user' was |
20 |
> at the very top of the long list that scrolled up the screen when I |
21 |
> ran the find command without -delete? |
22 |
> |
23 |
Well this is an unfortunate way to learn how find works. A better way |
24 |
would be: |
25 |
|
26 |
$ man find |
27 |
|
28 |
Basically find works of a chain of selection criteria. It crawls all |
29 |
the files/dirs and when one item in the chain is true for the criteria, |
30 |
it checks for the other. For example |
31 |
|
32 |
$ find /path -type f -name blah -print |
33 |
|
34 |
Crawls /path, for each file/dir it checks if it is a regular file (-type |
35 |
f), if that is true, it checks if it's name is "blah", if that is true, |
36 |
it prints the name (blah). |
37 |
|
38 |
Therefore, |
39 |
|
40 |
$ find /path -delete -type f -name .... |
41 |
|
42 |
Crawls path, then checks "-delete".. but wait, -delete evaluates to |
43 |
"true if removal succeeded" (find(1)), so it deletes the file, then |
44 |
checks to see if it is a regular file, then if that is true then it |
45 |
checks the name... but all that doesn't matter because your files are |
46 |
deleted. |
47 |
|
48 |
You should never put -delete at the beginning of a chain and, arguably, |
49 |
you shouldn't use -delete at all. It even says in the man page: |
50 |
|
51 |
Warnings: Don't forget that the find command line is evaluated |
52 |
as an expression, so putting -delete first will make find try to |
53 |
delete everything below the starting points you specified. When |
54 |
testing a find command line that you later intend to use with |
55 |
-delete, you should explicitly specify -depth in order to avoid |
56 |
later surprises. Because -delete implies -depth, you cannot |
57 |
usefully use -prune and -delete together. |