17

With the command adb pull /sdcard/ I can copy all the contents of the internal memory of my Android phone into my current local directory.

And adb pull /mnt/extSdCard/ does the same with the external SD card. But that command always copies everything, even files I already have stored locally.

Is there any way to copy only new and modified files? (files with a newer date)

Rohit Gupta
  • 1,795
  • 2
  • 14
  • 24
OMA
  • 363
  • 3
  • 5
  • 13

5 Answers5

10

As described by ss-3-1415926535897932384626433 there is no flag, but you have to get a list of files first and then check if your local files match. I wrote a little script for it:

#!/bin/sh

rfolder=/sdcard/DCIM/Camera
lfolder=Camera

adb shell ls "$rfolder" > android.files

ls -1 "$lfolder" > local.files

rm -f update.files
touch update.files

while IFS=  read -r q; do
  # Remove non-printable characters (are not visible on console)
  l=$(echo ${q} | sed 's/[^[:print:]]//')
  # Populate files to update
  if ! grep -q "$l" local.files; then         
    echo "$l" >> update.files
  fi  
done < android.files

script_dir=$(pwd)
cd $lfolder

while IFS=  read -r q; do
  # Remove non-printable characters (are not visible on console)
  l=$(echo ${q} | sed 's/[^[:print:]]//')
  echo "Get file: $l"
  adb pull "$rfolder/$l"
done < "${script_dir}"/update.files

Adjust the remote folder rfolder and the local folder lfolder to locations of your own choice.

Anne van Rossum
  • 211
  • 2
  • 7
9

adb-sync - small, yet powerfull python script that can do all your asked and more... https://github.com/google/adb-sync

  • 1
    Thanks for the mention. I had to make several changes to the source code to get it working for my use-case (invalid paths for Windows causing crash, Python version mismatch apparently, etc -- for details, see issues I commented in), but it ended up being the only way I was able to retrieve my files from a corrupted data partition. (the adb pull of the whole directory would crash on various files, and I didn't want to manually have to delete each one then restart the whole transfer -- with adb-sync [+my modifications] it would just fail that one file then continue) – Venryx Jul 22 '19 at 01:02
  • 2
    Also note that project has been deprecated and one should rather use its successor: better-adb-sync – Izzy Jan 18 '23 at 12:34
4

adb pull doesn't seem to provide a flag to pull selected files.

As a workaround, you can do this: Use adb shell [Unix shell command] to copy selected files to a temporary location and then pull all files from that location.

Update:
You can use cp -u [source] [destination] unix shell command to copy only modified files on subsequent run. You can also use -r flag to use it on subdirectories recursive, if its required.

iOS
  • 12,381
  • 13
  • 63
  • 103
  • Thanks for your answer. Copying all files to another location to then maybe update just one changed file is pretty slow. Too bad that adb doesn't provice in way to copy only newer files. – OMA Feb 28 '13 at 04:50
  • @OMA You can use the shell to get a list of new files, and then use adb pull on that list. – Matthew Read Feb 28 '13 at 16:35
  • @MatthewRead Any hints on how to do that? Thanks! – OMA Feb 28 '13 at 17:29
  • @OMA I am unable to understand your problem with this. Use both commands in one line or create alias (also add a command to purge temp location after operation). If there was a flag of adb pull for that, it'd work the same way. – iOS Mar 02 '13 at 10:42
2

Though not fully documented, starting with 27.0.1 aka platform tools r27 (not before) the following can be used:

adb push --sync local/ device/
# adb pull --sync -a device/. local/

(note the trailing dot on adb pull; without that you'd end up with local/device/*, see here; also the -a is needed only here to keep the timestamps of the files intact)

Not directly related to adb push: file permissions are not kept, which is probably due to the file system used. Files pushed to the internal SD card will always be set to 0660 and on the external card to 0771, which I could not even change using adb shell chmod on the device itself (though I received no error either on trying).

If you need more options as you might know them from rsync (e.g. --delete or --exclude), you can also take a look at the Python script better-adb-sync.

Update: running a few tests it seems --sync is simply ignored with adb pull even if no error is thrown: newer files on target have been overwritten! Not sure if implementation is planned, but with r27 being released in 2018 I'd not place a bet on that. With push it works as advertized, though.

Izzy
  • 91,166
  • 73
  • 343
  • 943
0

Because google's official adb pull was slow to support --sync, I found a big guy to modify the aosp adb source code. Add --sync option to adb pull, pulling only newer files. Compiled for windows.

Self-testing logic on win10 is no problem. Only pull updated files on the android phone, and do not delete win files that are not on the android phone.

https://github.com/freemedom/adb-pull-sync/raw/main/adb.zip

hrdom
  • 13
  • 3