Unix 'find' without descending into matche

2019-09-02 19:57发布

I was trying to remove all git files from a repository with this:

find . -name ".git*" -exec rm -rf {} \;

However, rm warns that files could not be deleted (because their parent directory has already been deleted).

Is there a way to get find to stop recursing when it finds a matching directory?

E.g. find...

/.gitmodules
/.git/stuff
/.git/.gitfile

... produces

/.gitmodules
/.git

标签: unix find
1条回答
小情绪 Triste *
2楼-- · 2019-09-02 20:17

Use -depth:

find . -depth -name ".git*" -exec rm -rf {} \;

This would allow you to process the files or subdirectories first before their parent directories.

查看更多
登录 后发表回答