Back up your phone's storage with this Linux utility

Take as many shots as you want; gphoto2 makes transferring photos from your device to your Linux computer quick and easy.
77 readers like this.
How to turn a Raspberry Pi into an eBook server

One of the great failings of mobile devices is how difficult it can be to transfer data from your device to your computer. Mobile devices have a long history of this. Early mobiles, like Pilot and Handspring PDA devices, required special synchronization software (which you had to do religiously for fear of your device running out of batteries and losing all of your data forever). Old iPods required a platform-specific interface. Modern mobile devices default to sending your data to an online account so you can download it again on your computer.

Good news—if you're running Linux, you can probably interface with your mobile device using the gphoto2 command. Originally developed as a way to communicate with digital cameras back when a digital camera was just a camera, gphoto2 can talk to many different kinds of mobile devices now. Don't let the name fool you, either. It can handle all types of files, not just photos. Better yet, it's scriptable, flexible, and a lot more powerful than most GUI interfaces.

If you've ever struggled with finding a comfortable way to sync your data between your computer and mobile, take a look at gphoto2.

Install gPhoto2

Chances are your Linux system already has libgphoto2 installed, because it's a key library for interfacing with mobile devices. You may have to install the command gphoto2, however, which is probably available from your repository.

On Fedora or RHEL:

$ sudo dnf install gphoto2

On Debian or Ubuntu:

$ sudo apt install gphoto2

Verify compatibility

To verify that your mobile device is supported, use the --list-cameras piped through less:

$ gPhoto2 --list-cameras | less 

Or you can pipe it through grep to search for a term. For example, if you have a Samsung Galaxy, then use grep with case sensitivity turned off with the -i switch:

$ gphoto2 --list-cameras | grep -i galaxy
  "Samsung Galaxy models (MTP)"
  "Samsung Galaxy models (MTP+ADB)"
  "Samsung Galaxy models Kies mode"

This confirms that Samsung Galaxy devices are supported through MTP and MTP with ADB.

If you can't find your device listed, you can still try using gphoto2 on the off chance that your device is actually something on the list masquerading as a different brand.

Find your mobile device

To use gPhoto2, you first have to have a mobile device plugged into your computer, set to MTP mode, and you probably need to give your computer permission to interact with it. This usually requires physical interaction with your device, specifically pressing a button in the UI to permit its filesystem to be accessed by the computer it's just been attached to.

Screenshot of allow access message

If you don't give your computer access to your mobile, then gPhoto2 detects your device, but it isn't unable to interact with it.

To ensure your computer detects the device you've attached, use the --auto-detect option:

$ gphoto2 --auto-detect
Model                       Port
Samsung Galaxy models (MTP) usb:002,010

If your device isn't detected, check your cables first, and then check that your device is configured to interface over MTP or ADB, or whatever protocol gPhoto2 supports for your device, as shown in the output of --list-cameras.

Query your device for features

With modern devices, there's usually a plethora of potential features, but not all features are supported. You can find out for sure with the --abilities option, which I find rather intuitive.

$ gphoto2 --abilities
Abilities for camera            : Samsung Galaxy models (MTP)
Serial port support             : no
USB support                     : yes
Capture choices                 : Capture not supported by driver
Configuration support           : no
Delete selected files on camera : yes
Delete all files on camera      : no
File preview (thumbnail) support: no
File upload support             : yes

There's no need to specify what device you're querying as long as you only have one device attached. If you have attached more than one device that gPhoto2 can interact with, though, you can specify the device by port, camera model, or usbid.

Interacting with your device

If your device supports capture, then you can grab media through your camera from your computer. For instance, to capture an image:

$ gphoto2 --capture-image

To capture an image and immediately transfer it to the computer you're on:

$ gphoto2 --capture-image-and-download

You can also capture video and sound. If you have more than one camera attached, you can specify which device you want to use by port, camera model, or usbid:

$ gphoto2 --camera "Samsung Galaxy models (MTP)" \

Files and folders

To interact with files on your device intelligently, you need to understand the structure of the filesystem being exposed to gPhoto2.

You can view available folders with the --get-folders option:

$ gphoto2 --list-folders
There are 2 folders in folder '/'.                                             
 - store_00010001
 - store_00020002
There are 0 folders in folder '/store_00010001'.
There are 0 folders in folder '/store_00020002'.

Each of these folders represents a storage destination on the device. In this example, store_00010001 is the internal storage and store_00020002 is an SD card. Your device may be structured differently.

Getting files

Now that you know the folder layout of your device, you can ingest photos from your device. There are many different options you can use, depending on what you want to take from the device.

You can get a specific file, providing you know the full path:

$ gphoto2 --get-file IMG_0001.jpg --folder /store_00010001/myphotos

You can get all files at once:

$ gphoto2 --get-all-files --folder /store_00010001/myfiles

You can get just audio files:

gphoto2 --get-all-audio-data --folder /store_00010001/mysounds

There are other options, too, and most of them depend on what your device, and the protocol you're using, support.

Uploading files

Now that you know your potential target folders, you can upload files from your computer to your device. For example, assuming there's a file called example.epub in your current directory, you can send the file to your device with the --upload-file option combined with the --folder option to specify which storage location you want to upload to:

$ gphoto2 --upload file example.epub \
--folder store_00010001

You can make a directory on your device, should you prefer to upload several files to a consolidated location:

$ gphoto2 --mkdir books \
--folder store_00010001
$ gphoto2 --upload-file *.epub \
--folder store_00010001/books

Listing files

To see files uploaded to your device, use the --list-files option:

$ gphoto2 --list-files --folder /store_00010001
There is 1 file in folder '/store_00010001'
#1     example.epub 17713 KB application/x-unknown
$ gphoto2 --list-files --folder /store_00010001/books
There is 1 file in folder '/store_00010001'
#1    example0.epub 17713 KB application/x-unknown
#2    example1.epub 12264 KB application/x-unknown

Exploring your options

Much of gPhoto2's power depends on your device, so your experience will be different than anyone else's. There are many operations listed in gphoto2 --help for you to explore. Use gPhoto2 and never struggle with transferring files from your device to your computer ever again!

What to read next
Seth Kenlon
Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time.


I use the same functions with nextcloud

Nextcloud is a great solution. I was looking for something a little more direct, which is why I chose a local command, but Nextcloud - especially if you're running it locally or at least on your local network, is a great way to ingest and sort photos, and of course is open source.

In reply to by Geh Kah (not verified)

Won't work on my system, I can only presume it's the "usb_storage" module, as none of the modules it suggests as preventing gphoto2 from claiming the device are running.

Needs further investigation, or it's somehow Gnome3-dependent.

Gave it another test under Mint 20.0, still sees something else as locking the device. I mentioned it might be Gnome3-specific because I use Cinnamon, not Gnome. It does recognize the phone (sees a Moto E5 as a Moto G, but I expect the internals are pretty much the same).

In reply to by sethkenlon

I find it increasingly more difficult to work with IOS devices. There seems to be more and more stops built in than ever before. Gphoto2, stops numerous times and declares bad files, even though they are not. Reset everything and it continues for short spurts, then the problems begin again. Gphoto2, is a good option to use.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.