Re: best way to find and delete (may contains directories)

James Youngman <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <CAGwX764_ZBSnQQPuuDRbBPoT=FpGMeQXC7LGzViRScceas5qPQ@mail.gmail.com>
On Sat, Mar 27, 2021 at 5:50 PM Weatherby,Gerard <[email protected]>
wrote:

> find. -name 0 -exec rm -fr {} \;


-exec rm is very likely a better choice in a lot of ways than xargs here,
but using -exec .... {} \; introduces a lot of overhead because it only
processes one file at a time.     This would be substantially more
efficient:

find. -name 0 -exec rm -fr -- {} \+



The "--" is probably superfluous in this case but it is generally good
practice.  It's there in case the rm command likes (like GNU rm) to
recognize options which come after arguments.   This is an example of this
in action:

$ rm -fr */0/1.txt -i
rm: remove regular empty file 'x/0/1.txt'? n
rm: remove regular empty file 'y/0/1.txt'? n
rm: remove regular empty file 'z/0/1.txt'? n

 Personally, I would probably use -delete to avoid the overhead of -exec
entirely (the explicit -depth is essentially only there for documentation):

find . -depth \( -path '*0/*' -o -path '*0' \) -delete

James.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.