-   Linux - General (
-   -   why did find delete everything? (

DJOtaku 07-25-2012 09:16 AM

why did find delete everything?
So I was trying to delete all the files I have with this in the filename: AdboeJPEG

So first I did a find without delete to make sure I was going to get teh right stuff.

find . -name *ADOBEJPEG*

And I got a long list of all the files. So it looked like it only got the files I needed. Perfect. So I ran

find . -delete -name *ADOBEJPEG*

and it deleted everything in that directory whether or not it matched the search. What did I do wrong?

tizzef 07-25-2012 09:53 AM

Hi there,

In the man page of find you will find the following sentence :
"The -delete action also implies -depth."
I think it was the problem that you had encounter...


druuna 07-25-2012 10:15 AM

The order of the options are important (as mentioned 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.
This would probably have done what you expected (although errors might be shown):

find . -name *ADOBEJPEG* -delete
I personally try to zoom in as much as possible (add -type f if you are after files or -type d for directories) and use the -exec rm {} \; option instead of -delete.

This is what i would have used instead:

find . -type f -name "*ADOBEJPEG*" -exec rm {} \;
Test it without the blue part.

DJOtaku 07-25-2012 10:23 AM

ah, I had a feeling order of operations had something to do with it.

Thand druuna. I'll have to give that a shot today. (Luckily I keep backups so I am able to restore all the lost data)

jschiwal 07-25-2012 10:26 AM

Also, you need to _always_ put arguments with wild cards inside of quotes. Otherwise it expands before find runs.

DJOtaku 07-25-2012 10:29 AM


Originally Posted by jschiwal (Post 4737700)
Also, you need to _always_ put arguments with wild cards inside of quotes. Otherwise it expands before find runs.

Can you elaborate on what that means?

TobiSGD 07-25-2012 12:15 PM


Originally Posted by DJOtaku (Post 4737701)
Can you elaborate on what that means?

If you don't put the name with the *-wildcards into quotes they will be expanded by the shell first instead of being used directly by find. If you use for example the option -name abc*.txt and you have a file abcd.text in your current directory the shell will expand the wildcards to that name, so that find runs with the option -name abcd.txt. This doesn't happen if you quote the wildcards, like -name "abc*.txt".

DJOtaku 07-25-2012 12:42 PM

Hmm....that didn't appear to be the case last night. It appeared to catch all files that had AdobeJPEG in the filename.

All times are GMT -5. The time now is 12:40 PM.