Why did find with -delete erase the files in my /save/ directory when find without delete was not able to locate them?

I want to delete all the files in the current directory tree, except those in save. I ran this command:

 find . ( -name save -prune ) -o -type f -ls | grep /save/

and it found none. But when I ran this command:

 find . ( -name save -prune ) -o -type f -delete

All those files in /save/ were gone. What am I missing?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

-delete implies -depth that doesn’t work with -prune (-depth starts with the leaves). There’s a warning about that in the manual of the GNU version (-delete is a FreeBSD extension now also supported by GNU find and a few other implementations).

info -- find -delete

The use of the ‘-delete’ action on the command line automatically
turns on the ‘-depth’ option (*note find Expressions::). This can
be surprising if you were previously just testing with ‘-print’, so
it is usually best to remember to use ‘-depth’ explicitly.

info -- find -prune

Because ‘-delete’ implies ‘-depth’, using ‘-prune’ in combination
with ‘-delete’ may well result in the deletion of more files than
you intended.

Here, you’ve got the option of either using rm instead:

find . -name save -prune -o -type f -exec rm -f {} +

(potentially unsafe if there are directory writeable by others in there, as one could make you delete files outside the current directory tree by replacing directories with symlinks while you run that command).

A safer alternative:

find . -name save -prune -o -type f -execdir rm -f -- {} ;

That doesn’t have the problem mentioned above but means running one rm per file. The -- is necessary for the FreeBSD implementation, not the GNU one that prefixes file names with ./.

Alternatively, as suggested by Costas:

LC_ALL=C find . ! -name save ! -path '*/save/*' -type f -delete

(but that still needlessly descends into save directories)

The LC_ALL=C is there so * matches any sequence of bytes (even those that don’t form valid characters in the current locale). Note that it will affect the language of error messages (English instead of the language of the user).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x