23

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?

5
  • 4
    Ouch ... I learned something today (thanks to you). And I recommend a simple mv save/ ../some/safer/location prior to such a "generic" delete command (... but of course, prior to your post I'd have done the same check and ran into the same trouble!). Now go find a good "undelete" for the filesystem the files were on ^^ Jan 21, 2016 at 14:50
  • 3
    My pain is your prevention.
    – Otheus
    Jan 21, 2016 at 15:16
  • 1
    1000 thanks to you. Code (often?) works in mysterious ways... Jan 21, 2016 at 16:52
  • 3
    related: unix.stackexchange.com/questions/87258/…
    – Lesmana
    Jan 21, 2016 at 19:57
  • @lesmana I upvoted your answer there. sadly, my version of find doesn't give me such a nice warning :(
    – Otheus
    Jan 22, 2016 at 10:14

1 Answer 1

29

-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).

2
  • What's the security issue here with rm?
    – jrw32982
    Jan 26, 2016 at 21:10
  • @jrw32982, see edit with link to the GNU find manual Jan 27, 2016 at 11:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .