查找目录,如果匹配则不递归

查找目录,如果匹配则不递归

在我的 Mac 上,我搞砸了 Android SDK 安装。我想通过查找所有对“android”的引用来清理它。

问题是,当我运行该命令时find / -name "*android*",我得到了相当广泛的列表。

而不是这个列表:

/usr/local/var/lib/android-sdk
/usr/local/var/lib/android-sdk/platforms/android-23
/usr/local/var/lib/android-sdk/platforms/android-24
/usr/local/var/lib/android-sdk/platforms/android-25
/usr/local/var/lib/android-sdk/platforms/android-26
/usr/local/var/lib/android-sdk/platforms/android-19
/usr/local/var/lib/android-sdk/platforms/android-28
/usr/local/var/lib/android-sdk/platforms/android-N
/usr/local/var/lib/android-sdk/system-images/android-23
/usr/local/var/lib/android-sdk/system-images/android-19
/usr/local/var/lib/android-sdk/system-images/android-N
...
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-15
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-16
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-18
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-20
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-21
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-19
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-10
/Library/Frameworks/Xamarin.Android.framework/Versions/5.1.2-1/lib/mandroid/platforms/android-17

我想得到:

/usr/local/var/lib/android-sdk
/Library/Frameworks/Xamarin.Android.framework

如果目录匹配,它不会搜索子目录。

答案1

如果您只需要目录:

find . -type d -iname '*android*' -a \( -prune  \)

代码评论:

.             # Present directory
-type d       # Only directories
-iname        # Instead of `-name` to also match "Android" (ignore case).
'*android*'   # Pattern of name to search
-a            # Force the execution of next option on matching names.
              # probably redundant as all options are evaluated with an
              # implicit and in find
              # but: makes the intent explicitly clear.
\( -prune  \) # Remove the whole directory from further examination.
              # braces also optional (as it is only one command).

简化:

find . -type d -iname '*android*' -prune

如果您还想要文件:

find . -iname '*android*' -a \( -prune  \)

注意:这将报告找到的所有文件,然后报告目录。目前还不清楚如果发现名为“android”的文件是否应该避开相应的目录。

相关内容