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