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

Peng Yu <[email protected]>
Newsgroups gmane.comp.gnu.findutils.bugs
Message-ID <CABrM6w=FZNRoi3zGLpVD4Ly0-oupzz-k-NgqG2Mv24h6H1DnTA@mail.gmail.com>
On Sun, Mar 28, 2021 at 4:31 AM Bernhard Voelker
<[email protected]> wrote:
>
> On 3/27/21 10:16 PM, James Youngman wrote:
> >  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
> __________________________^^______________^^
>
> Just to clarify further: the above pattern for -path is not the same
> as specifying '-name 0', because it would match any file or directory
> which ends on "0", like e.g. "dir0".
>
> The following is therefore closer to the original (yet still avoiding
> to spawn a separate process):
>
>   find . -depth \( -path '*/0/*' -o -path '*/0' \) -delete
> or
>   find . -depth \( -path '*/0/*' -o -name 0 \) -delete

The above seems to be the best answer for the specific case of delete
directories found by -name 0. It avoids calling external programs.

> As James mentioned, explicitly specifying -depth is not necessary as it
> is implied by -delete, still it comes handy when you first want to have
> a look which files would be deleted by exchanging -delete by -print;
> it would still enforce the depth-first method:
>
>   find . -depth \( -path '*/0/*' -o -name 0 \) -print

It is also good to have this to check what is to be deleted beforehand.

-- 
Regards,
Peng
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.