Home » Android » android – How to search a file with adb.exe?

android – How to search a file with adb.exe?

Posted by: admin April 23, 2020 Leave a comment

Questions:

Unix/Linux contains “find” command for searching files, but I do not know which command in android can be used to search for a file. Please give me an example.

How to&Answers:

As findand grep are not installed on devices I use, I am using the following:

adb shell ls -R / >tree.txt

After that I’m able to search in the file tree.txt on my pc using whatever software I like (for example notepad++ or grep).

Answer:

You may not find find and grep on all devices, so probably your safe bet is

   $ adb shell ls -lR | grep filename_you_want

on Linux/OSX, or

   > adb shell ls -lR | find "filename_you_want"

on Windows.

Answer:

find doesn’t work for me on my device. Doesn’t seem to be installed!

However this sort of works for me:

adb shell

(then in shell)

ls -laR | grep filename_you_want

Hope this helps.

Answer:

Here’s a tiny script that should work in zsh and bash. It simulates what you might see if you could run find / on your Android device:

adb shell ls -R / |
    while read line
    do
            line=$(echo $line | tr -d '\r')

            if [[ "$line" =~ ^/.*:$ ]]
            then
                    dir=${line%:}

            elif [[ "$line" = "opendir failed, Permission denied" ]]
            then
                    echo "find: $dir: Permission denied"

            else
                if [[ "$dir" = "/" ]]; then dir=""; fi

                echo "$dir/$line"
            fi
    done

Because it lists all the immediate child directories before descending into them, the output is a little out of order (for example /system is listed right away but /system/media doesn’t show up until much later). Depending on your needs, you could slap | sort -u at the end of the script, but it should be all right for all your basic greping needs.

Answer:

Some android devices don’t include include find or grep in $PATH. However they might include busybox. (In my experience, most of them can use busybox). So you might use them as follow:

adb shell

and then

busybox find . “file.txt”

or something like that

Hope it will help.

Answer:

The other answers didn’t work for me because they either didn’t provide the path to the file I was looking for or returned so many file names that it wasn’t useful.

For me the following worked using busybox:

adb shell busybox find / -name <your_file_name.here> 2>/dev/null

The / tells the find command to search from the root directory. But you could use . instead to search from current directory. Like this:

adb shell busybox find . -name <your_file_name.here> 2>/dev/null

The 2>/dev/null quiets permission denied responses, otherwise I get a lot of them and it clogs the screen.