39

I'm attempting to use the adb pull command to copy only certain files (jpg) to my macbook. I tried "adb pull sdcard/mydir/*.jpg" but it apparently doesn't interpret wildcards. How can I get only the jpg files copied over? I have rooted the phone if that helps.

user1133275
  • 2,642
  • 27
  • 31
Jack BeNimble
  • 35,733
  • 41
  • 130
  • 213

3 Answers3

75

You can move your files to other folder and then pull whole folder.

adb shell mkdir /sdcard/tmp
adb shell mv /sdcard/mydir/*.jpg /sdcard/tmp # move your jpegs to temporary dir
adb pull /sdcard/tmp/ # pull this directory (be sure to put '/' in the end)
adb shell mv /sdcard/tmp/* /sdcard/mydir/ # move them back
adb shell rmdir /sdcard/tmp # remove temporary directory
Michał Zieliński
  • 1,345
  • 11
  • 13
  • This doesn't work for me. I try copying TWRP backups with `adb pull /sdcard/TWRP/ TWRP` and it creates a 1kb `.twrps` file and nothing else. – Celeritas Oct 12 '16 at 03:44
  • Why to move files to another folder and then pull them? I only pulled the files from original folder (with "/" in the end) and it worked. – Mario Mey Jul 03 '17 at 20:44
  • The move lets you single out .jpg files. Since shell mv allows wildcards in filepaths but pull doesn't, you can be as specific as you want with the move then do the general pull before moving things back. – NJTabit Dec 19 '17 at 23:51
  • This one is a good idea to single out certain files. To make this process easier, one can install the ES-File to move and select files to another folder, and it would make one's life easier, and then just call adb pull command as listed by Michal to get all the files. – g5thomas Jun 22 '18 at 19:07
15

Pull multiple files using regex:

Create pullFiles.sh:

#!/bin/bash
HOST_DIR=<pull-to>
DEVICE_DIR=/sdcard/<pull-from>
EXTENSION=".jpg"

for file in $(adb shell ls $DEVICE_DIR | grep $EXTENSION'$')
do
    file=$(echo -e $file | tr -d "\r\n"); # EOL fix
    adb pull $DEVICE_DIR/$file $HOST_DIR/$file;
done

Run it:

Make it executable: chmod +x pullFiles.sh

Run it: ./pullFiles.sh

Notes:

  • as is, won't work when filenames have spaces
  • includes a fix for end-of-line (EOL) on Android, which is a "\r\n"
Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • 2
    On Linux, the osx fix needs to changed to tr -d "\r" – Jonathan Aug 22 '14 at 16:28
  • 1) This seems to miss cases where there is a space in the filename. 2) The grep expression is underspecified (the . isn't being used correctly and it should end with a $ to match the wildcard *.txt) – Erick Wong May 18 '15 at 01:48
  • @ErickWong That was a small snippet that worked for me, and I thought it would be nice to share it! It does *NOT* cover all cases of course, but feel free to edit it, to include the ending char `$`, and files with spaces! – Paschalis May 19 '15 at 14:31
  • Worked for me only after removing the ending dollar sign from `$EXTENSION$` – dafnahaktana Oct 24 '17 at 10:43
  • @dafnahaktana The second denotes the ending char. It was working fine for me, but I guess in single quotes should be safer. I updated the answer. – Paschalis Oct 24 '17 at 18:20
0

As to the short script, the following runs on my Linux host

#!/bin/bash
HOST_DIR=<pull-to>
DEVICE_DIR=/sdcard/<pull-from>
EXTENSION="\.jpg"

while read MYFILE ; do
    adb pull "$DEVICE_DIR/$MYFILE" "$HOST_DIR/$MYFILE"
done < $(adb shell ls -1 "$DEVICE_DIR" | grep "$EXTENSION")

"ls minus one" lets "ls" show one file per line, and the quotation marks allow spaces in the filename.

Werner
  • 1