# Gphoto2
by Seth Kenlon

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.
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.
It's probably available from your repository.

On Fedora:

```
$ sudo dnf install gphoto2
```

On 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 sensitivty 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 in the list, 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.

![Allow access to your mobile](gphoto2-mtp-allow.jpg)

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 rather intuitive ``--abilities`` option.

```
$ 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 your querying as long as you only have one device attached.
If you have've 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)" \
--capture-image-and-download
```

## Files and folders

To interact with files 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 represent 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.

## 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 with gphoto2 will be different than anyone else's.
There are many opeartions listed in ``gphoto2 --help`` for you to explore.

Use gphoto2 and never struggle with transferring files from your device to your computer ever again!