Linux

How to adb pull files using a wildcard like asterisk *

Using adb pull with a wildcard character like the asterisk »*« is not possible at this point in time. However, there is a very easy and fast workaround using a for loop.

for file in $(adb shell ls /your/directory/structure/<fixed_name_part>*.jpg; do
    adb pull "$file"
done

What does this improve?

This solves the following problems:

  • not wanting to browse and list files in adb shell in advance if you already know the pattern you’re looking for (like a date-time format for photos or videos on your android phone)
  • copying multiple files manually or using a script to go through a list output you need to generate first
  • copying multiple files from a directory but omitting others

Many solutions just suggest to copy a whole directory, which, e.g. for photos and videos, is complete nonsense. Not only is this prone to crashes (e.g. if your HDD is full but you didn’t notice, boom your data is partially gone if you didn’t take precautions), it’s really only a use-case for massive backups. The much more common issue is that the user wants to copy just a few files, following a pattern, and leave other files on the device.

For instance, I use this to pull all photos from a certain date from my phone:

for file in $(adb shell ls /storage/0/emulated/DCIM/Camera/IMG_20250101*.jpg; do
    adb pull "$file"
done

This is the fastest way to pull files using a pattern in adb. You could make a script out of this, but really, a basic for loop in bash is something every Linux user should be capable of. If you look at this and you think it’s complicated, maybe go back to windows, because that’s a wildly inappropriate response. However, if you are such a noob, I will provide you with instructions to un-noob you further down the article.

How this script works

Now let’s have a look at the bash script in detail to understand how it works:

for file in $(adb shell ls /your/directory/structure/<fixed_name_part>*.jpg; do
    adb pull "$file"
done

In bash, a for loop is defined as:

for name [ [in [words …] ] ; ] do commands; done

You can look up how different loops work in the GNU bash ref manual here.

Double brackets [[ ... ]] are a modern standard syntax for conditions in bash. In the past, single brackets were the norm. Now, we use double brackets, because they offer more features and are easier to use. Both notations follow different rules, as in different operators are allowed within, such as:

Feature[ ... ] (Old)[[ ... ]] (New)
Operators-eq, -gt, -f, -d, !All from [ ... ], plus: &&, ||, == (wildcards), =~ (regex)
Spaces required?✅ Yes❌ No
Supports wildcard (*)?❌ No✅ Yes (== "*pattern*")
Supports regex (=~)?❌ No✅ Yes
Safer for variable expansion?❌ No (quotes needed)✅ Yes

Annotation: Backticks ` should not be used, instead $() is the modern notation.

But wait, where are the [[…]] in our example?

Now, to understand this, you have to understand different types of expressions in Bash. Bash scripting isn’t just about running commands—it involves tests, comparisons, and command substitutions, each with their own syntax.

In the above example, we used $(...), which is for command substitution—it runs a command and replaces it with the output (i.e. $(date) runs the date command → $(date) will then be replaced by the current date and time in the output, looking like “Tue Mar 5 14:30:00 UTC 2025”). And now [[...]] is used for conditional expressions, meaning it checks whether something is true or false.

Just remember: [[...]] for true/false and $(...) for commands.

Calling our index variable

Now, finally, we are just calling the variable file that we declared in the for loop, which again uses a dollar character ($), because that’s how you reference the value of a variable in bash.

do
    adb pull "$file"
done

$ tells the script to retrieve the value stored in the variable. Without it, we would just be referring to the name of the variable itself, not its value (i.e. Without the $, you’re not referencing the value of the variable file. Instead, you’re literally using the word file, which is just a string! This would try to pull a file named file, which doesn’t exist, and would result in an error message).

In our use case here, $file represents each file path and name as the loop iterates through them. The loop iterates through the list command output and with each iteration $file the next item in the list is called and passed to adb pull to actually pull that from the device.

Hope this helps! If you’re interested in basic Linux applications and commands, check out this article to improve your knowledge! Please, also consider reading the adb man.

You Might Also Like

No Comments

Leave a Reply