Android, ADB, a useful script for copying files
By request, here is the script I use to copy multiple files from my
android phone to my debian box, over the usb cable using ADB (Android
Debug Bridge)
Prerequisites:
- adb. I am using android 4.1.2, which requires adb 1.0.31
- busybox (I think you need root to install busybox, not sure)
- turn on usb debugging (settings > developer options) ... and make sure
to turn it off afterwards, as it is a security risk. I recommend
installing a reminder app for this purpose
First, make sure you can use adb! Run "adb shell" from your debian box;
you should get a prompt that looks something like:
shell@android:/ $
Getting adb working is beyond the scope of this email.
Here is the main script I use, which has a few different modes:
#!/bin/bash
shopt -s extglob
[[ $pt = +([0-9]) ]] || pt=5555
echo "Forwarding port $pt" >&2
adb forward tcp:$pt tcp:$pt
case ${0##*/} in
Adb-cat) nc localhost $pt;;
Adb-tar) nc localhost $pt | tar xf -;;
Adb-ztar) nc localhost $pt | tar zxf -;;
Adb-shell)
echo "tar cf - | nc -l -p $pt" >&2
adb shell
;;
esac;
I hardlinked the script to several different executable names: Adb-cat,
Adb-tar, Adb-ztar, and Adb-shell
From the first terminal I run Adb-shell, which gives me an android
prompt, with the port forwarded. Then I run a command on android to send
the files I want, e.g.
tar cf - some.pdf files.jpg I want to copy.jpg | nc -l -p 5555
...it should sit there waiting.
Then I open a second terminal in debian, and from there, run:
Adb-tar
This does the same port forward, and listens on the port for the
transmission coming from android, then pipes it through tar to extract
the files.
Alternately, you could run Adb-cat >archive.tar to store as an archive
on the debian side.
Similarly, if you want to compress the files before transmission, add a
z to the tar params on android, and run Adb-ztar (or Adb-cat
>archive.tgz) Of course this is not such a good idea if sending images,
videos, or pdfs, as they are already compressed and running them through
gzip is only going to waste processor time and not actually save bandwidth.
If you want to use a different port, run it like this:
env pt=4800 Adb-shell
env pt=4800 Adb-tar
Hope this helps! Questions welcome
~David.
Reply to: