Media Transfer Protocol (MTP) is the usual method for copying multiple files over from an Android
device to a Linux computer. The underlying ext filesystem on an Android device is exposed through
MTP. MTPFS implemented in Golang works well to turn this into a standard file system:
hanwen/go-mtpfs: Mount MTP devices over FUSE.
FUSE is an interface that allows unprivileged users to create a virtual file system and export it to the Linux kernel, making it indistinguishable from other filesystems. This is great for a quick back-up of all the files on an Android device in the “Internal storage” section.
Install libusb-dev which contains the libmtp header files:
$ sudo apt install libusb-1.0-0-dev
Clone MTP FS implementation in Golang:
$ git clone git@github.com:hanwen/go-mtpfs.git
Build the Golang application.
Create a mountpoint for the MTP device:
$ sudo mkdir /mnt-mtp
$ sudo chown 1000:1000 /mnt-mtp
Connect an Android device and set the connection mode to File transfer / Android Auto
Start the MTP filesystem:
./go-mtpfs /mnt-mtp
Confirm that the MTP filesystem was mounted properly: (I am checking whether files transferred from a Sony Camera using the Imaging Edge Mobile application show up properly.)
$ ls -1 /mnt-mtp/Internal\ shared\ storage/DCIM/Imaging\ Edge\ Mobile/ | head -1
D8C02177.JPG
The FUSE filesystem can be unmounted as usual using:
$ umount /mnt-mtp
MTP is quiet slow the first time that it enumerates files in a directory. For a directory containing 1400 files, it took ~15 seconds to list the whole directory on a 3 year old Android device. Interestingly, this list seems to be cached because the second list completed instantly.
# ~ [0s]
$ ls -1 /mnt-mtp/Internal\ shared\ storage/DCIM/Camera | head -3
thumbnails
IMG_63.jpg
IMG_52.jpg
IMG_22.jpg
# ~ [14s]
$ ls -1 /mnt-mtp/Internal\ shared\ storage/DCIM/Camera | wc -l
1467
# ~ [0s]
Edit: For reference, I used rsync on the FUSE MTP file system to transfer a large set of files
to an SSD. This is how long it took: (This is fully limited by the phone’s flash storage and the MTP
software overhead, because the SSD transfer times for the destination are certainly higher than 5
Mbps.)
sent 17.43G bytes received 28.51K bytes 5.16M bytes/sec
total size is 17.46G speedup is 1.00
# ~ [56m 20s]