0

On my Mac, I screwed up my android SDK installation. I was looking to clean it out by finding all references to "android".

The issue is, when I run the command find / -name "*android*" I get quite an extensive list.

Instead of this list:

/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

I would like to get:

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

It doesn't search the subdirectories if the directory matches.

  • @Goro how? I need it to search the subdirs but only if it doesn't match – Isaac Sep 13 '18 at 0:26
  • @Goro I thought the question outlined it fairly clearly, I want the result to, no matter how deep, stop searching the subdirectories of paths that match *android* – Isaac Sep 13 '18 at 0:37
  • eg: /usr/local/var/lib/android-sdk/platforms wouldn't be searched because /usr/local/var/lib/android-sdk already matches *android* – Isaac Sep 13 '18 at 0:39
  • If you want to find all the files except the ones that have *android* in it, then your command should be find / ! -name "*android*" – user88036 Sep 13 '18 at 0:50
  • @Goro No, I want to find folders that match *android*, but I don't want to see the child folders of these folders – Isaac Sep 13 '18 at 0:58
4

If you only need directories:

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

Coments on code:

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

Simplified:

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

If you also want files:

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

Caveat: this will report all files found and then directories. It is not clear if if a file named "android" is found that the corresponding directory should be avoided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.