adb pull multiple files

98,329

Solution 1

You can use xargs and the result of the adb shell ls command which accepts wildcards. This allows you to copy multiple files. Annoyingly the output of the adb shell ls command includes line-feed control characters that you can remove using tr -d '\r'.

Examples:

# Using a relative path
adb shell 'ls sdcard/gps*.trace' | tr -d '\r' | xargs -n1 adb pull
# Using an absolute path 
adb shell 'ls /sdcard/*.txt' | tr -d '\r' | sed -e 's/^\///' | xargs -n1 adb pull

Solution 2

adb pull can receive a directory name instead of at file and it will pull the directory with all files in it.

Pull all your gps traces in /sdcard/gpsTraces

adb pull /sdcard/gpsTraces/ . 

Example of adb pull and adb push of recursive directories:

C:\Test>adb pull /data/misc/test/ .
pull: building file list...
pull: /data/misc/test/test1/test2/test.3 -> ./test1/test2/test.3
pull: /data/misc/test/test1/test2/test.2 -> ./test1/test2/test.2
pull: /data/misc/test/test1/test2/test.1 -> ./test1/test2/test.1
pull: /data/misc/test/test1/test.3 -> ./test1/test.3
pull: /data/misc/test/test1/test.2 -> ./test1/test.2
pull: /data/misc/test/test1/test.1 -> ./test1/test.1
pull: /data/misc/test/test.3 -> ./test.3
pull: /data/misc/test/test.2 -> ./test.2
pull: /data/misc/test/test.1 -> ./test.1
9 files pulled. 0 files skipped.
0 KB/s (45 bytes in 0.093s)
C:\Test>adb push . /data/misc/test/
push: ./test1/test2/test.3 -> /data/misc/test/test1/test2/test.3
push: ./test1/test2/test.2 -> /data/misc/test/test1/test2/test.2
push: ./test1/test2/test.1 -> /data/misc/test/test1/test2/test.1
push: ./test1/test.3 -> /data/misc/test/test1/test.3
push: ./test1/test.2 -> /data/misc/test/test1/test.2
push: ./test1/test.1 -> /data/misc/test/test1/test.1
push: ./test.3 -> /data/misc/test/test.3
push: ./test.2 -> /data/misc/test/test.2
push: ./test.1 -> /data/misc/test/test.1
9 files pushed. 0 files skipped.
0 KB/s (45 bytes in 0.062s)

Solution 3

./adb pull /sdcard <-- fails

./adb pull /sdcard/ <-- works recursively - note the trailing slash

Tested with Nexus 5 and adb downloaded March 2014.

Solution 4

Parsing the output from 'ls' is generally a bad idea. Instead, use 'find'.

adb shell 'find /sdcard/ -name "gps*.trace" -print0' | xargs -0 -n 1 adb pull

Why you shouldn't parse the output of ls

Solution 5

I have created this for Windows boxes, It is very useful to transfer files using wildcards without mounting the filesystem. You can include this script somewhere in your path env.

adbpull.bat

@echo off
setlocal enabledelayedexpansion
if %1.==. (
    echo Wilcard parameter is required.
    goto end
)
for /F "tokens=* USEBACKQ" %%F in (`adb shell ls %1`) do (
    set text=%%F
    set mfile=!text:~0,-1!
    adb pull "!mfile!"
)
:end
endlocal

Example: adbpull /sdcard/DCIM/Camera/IMG_2016*

Share:
98,329
hsz
Author by

hsz

IntelliJ SDK, Kotlin, Java, TypeScript, NodeJS, security, ...

Updated on September 08, 2021

Comments

  • hsz
    hsz over 1 year

    What is the best way to pull multiple files using

    adb pull
    

    I have on my /sdcard/ 25 files with following name:

    gps1.trace
    gps2.trace
    ...
    gps25.trace
    

    Wildcard does not work:

    adb pull /sdcard/gps*.trace .
    
  • hsz
    hsz over 10 years
    Unfortunately I am using Windows with Cygwin. But I have to remember this one - very interesting solution !
  • Chris Stratton
    Chris Stratton over 10 years
    actually those are carriage returns which must be filtered, but right idea on an obscure issue
  • Ofir Luzon
    Ofir Luzon about 10 years
    @kakyo It does work for subfolders for me. check you have the latest ADB from google. (I'm using 1.0.29)
  • kakyo
    kakyo about 10 years
    Yes, I found that it works for non-empty folders in the end. I had some empty folders that didn't get pulled. Also, seems that "push" is non-recursive?
  • Ofir Luzon
    Ofir Luzon about 10 years
    @kakyo I've added the output of a recursive pull and push to my answer. You are correct about empty folders, they do not get pulled or pushed.
  • Admin
    Admin almost 10 years
    Oh, it didn't work. tar -tvf tells me that the result doesn't look like a tar archive. Oh well.
  • sschuberth
    sschuberth about 9 years
    Here's a slightly improved version that can handle spaces in file names: github.com/sschuberth/dev-scripts/blob/master/android/…
  • BrainSlugs83
    BrainSlugs83 almost 9 years
    FYI: Windows and DOS use "\r\n" for carriage-return / line-feed pairs (i.e. not "\n\r").
  • rupps
    rupps over 8 years
    in my case it failed trying to pull the (correctly obtained) filenames from the android's root directory, to solve it, I used: adb shell "ls -d /mnt/sdcard/Pictures/Screenshots/*" | tr '\r' ' ' | xargs -n1 adb pull
  • gronostaj
    gronostaj almost 8 years
    I had to use find instead of ls because paths starting with / didn't work and ones without / returned just filename, without full path. (Windows/msys)
  • wrkwrk
    wrkwrk over 7 years
    It's true that the newline in windows is \n\r, but there's no build-in "tr" or "xargs" command in batch.
  • Antonio
    Antonio about 7 years
    For me it worked recursively without the final slash, adb version 1.0.32
  • Stan
    Stan almost 7 years
    Beware: with ADB pull the files lose their original modified date.
  • FuriousGeorge
    FuriousGeorge over 6 years
    See my answer below for a slightly modified version that allows you to pull into remote directories and is slightly more terse
  • starfry
    starfry over 6 years
    I found your answer useful, but I wondered why you didn't just do tr -d '\r'.
  • Rusty Gear over 6 years
    @Stan: unless you use the "-a" option as in "adb pull [-a] <remote>... <local>", which according to the help preserves the file timestamp and mode.
  • Suncatcher
    Suncatcher almost 6 years
    It doesn't work saying tr is not recognized as an internal or external command
  • x'ES
    x'ES over 5 years
    Very strong way! But the problem is errors/warnings like "tar: removing leading '/' from member names" added to output file. Don't know how to eliminate this.
  • Bruno Alexandre Rosa
    Bruno Alexandre Rosa about 5 years
    tr is an application present in Linux or git bash.
  • user905686
    user905686 over 4 years
    You use wildcards for cp on Android and not for pull on the host, that's why it works. Nice alternative, though copying to /sdcard/ costs extra memory and leaks private files to sdcard.
  • Dan J
    Dan J over 3 years
    On my machine I needed to use the full path to adb, but otherwise this worked great!
  • Shayan
    Shayan almost 3 years
    I get 1.5KB file on the output.
  • Gary Wang
    Gary Wang 7 months
    Heads up! You need to redirect stderr to somewhere else or you might get a corrupted tar file! See askubuntu.com/a/170000 for reason, and at least test if the result file is not corrupted.