2

On the left side of the Dolphin display, one of the categories shown is "Devices". Just as one would expect, clicking on any of the listed devices shows the files available from that device, e.g., a camera or a memory stick. if the device isn't read-only, you can even use the Dolphin display to add files to the device. But how can one get that same list of files (for any listed device) from the command line, using either ls or something similar to it? I would expect the method to be the same no matter what the nature of the device. And if the device is writable, how would you write to it from the command line?

Paul A.
  • 2,141

2 Answers2

4

With command line there is a small bit that you have to keep in mind - it has to be mounted ( in other words connected programmaticaly to the system; it may be physically connected like a USB drive to USB port, but that doesn't mean it is mounted ).

Checking if device is mounted is easy enough - lsblk, df commands or mount without any - flags/options.

Now, by default, Ubuntu's GUI such as Unity or Gnome automatically mount the devices you connect. With pure command line or a GUI that doesn't do this automatically (like openbox or blackbox ), you have to issue mount command or udiscksctl mount --block /dev/sd* where * is the letter for that device. I'd prefer the last one, since it automatically mounts to /media folder. With mount you have to specify the folder. Also with mount you have to know filesystem type, like NTFS (windows), ext4 (linux), FAT (many USB drives).

As for actual listing of the files, there is plenty of ways

  • find /directory/where/device/mounted -type f is a recursive way to list all files in all folders on that device.
  • ls /mount/folder
  • stat /mount/folder
  • dir which is same as ls
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
1

The phrase In UNIX, everything is a file can be applied here: Each physical drive plugged into your computer will be represented by one or more files inside /dev, as long as it is recognized by the system.

In order to be readable and writable, these devices need to be mounted somewhere. To get a list of all mounted devices, use the command mount. Here is an extract of my mount table:

/dev/sda1 on / type ext4 (rw,errors=remount-ro)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
/dev/sdc1 on /media/sebastian/some-id type ext4 (rw,nosuid,nodev,uhelper=udisks2)

If your device is not listed here, it isn't mounted yet. Most file managers auto-mount drives when they are plugged in, but if they aren't, or you aren't using a file manager, you can do this manually. First list the partitions you got with lsblk. This will give information like the following:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda      8:0    0 232.9G  0 disk 
├─sda1   8:1    0 224.8G  0 part /
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   8.1G  0 part [SWAP]
sdc      8:32   1   3.8G  0 disk 
└─sdc1   8:33   1   3.8G  0 part /media/sebastian/some-id

From here we can see that the USB stick I just plugged in is enumerated as /dev/sdc, and it has one partition with the number 1. This partition can be found at /dev/sdc1. As you noticed, it is already mounted, but let's just assume it isn't. Here are the steps to manually mount a drive:

  1. mkdir /mnt/somedir will create a directory with the name somedir (change name at one's leisure) inside /mnt. Here you will mount the drive.
  2. mount /dev/sdc1 /mnt/somedir will attempt to mount the partition /dev/sdc1 (my USB stick's only partition) in /mnt/somedir
  3. In case this fails, you may have to give optional arguments, because mount is quite smart, but can't always guess what to do. For this, best look at the options presented in man mount. An option you may need could be -t <type> to indicate the partition format, e.g. mount -t vfat /dev/sdc1 /mnt/somedir. If mount complains that you don't have the necessary permissions, add yourselves to the group plugdev or execute the mount command as root with sudo mount <...>

The partition should now be mounted, so again run mount to see the new entry. You can now jump into the mounted partition with cd /mnt/somedir. Now type ls to list the contents of the drive.

To get files from the drive to your computer, and vice versa, use the cp command:

cp <source> <destination>

e.g.

cp image1.png image2.png image3.png /home/username/Pictures

will copy the three files over to your Pictures folder.

To unmount a device after using it, use the umount command:

umount /dev/sdc1

or

umount /mnt/somedir

You can now safely delete the mount directory, but you don't have to.

s3lph
  • 14,314
  • 11
  • 59
  • 82
  • The specific case I'm having trouble with is an Android phone using mtp. It doesn't seem to show up in the output of mount, though it shows up twice under Dolphin: once as an mtp device and the other time as a camera. – Paul A. Nov 13 '15 at 23:44
  • Android phones are not mounted through a typical filesystem or as a USB storage device. Android phones use the wretched Microsoft media transfer protocol (thanks again windows for making things awful). The path to the device should be something like mtp://example but this will not display as a directory through the terminal just as a url for an apache server will not display this way unless you use something like ssh. However, you can also use ssh to access your android phone. – mchid Nov 14 '15 at 01:54
  • The items listed by Dolphin under Devices seem to include a lot of things -- androids, hard drives, memory sticks, cameras, Bluetooth, etc. All of them contain files that Dolphin can list. So what I'm looking for is a general way of listing such files from the command line -- which is the key to performing other operations on them. – Paul A. Nov 14 '15 at 02:12
  • File managers use interfaces like the GNOME Virtual File System, short gvfs, or (like Dolphin does) KIO to access those devices. These programs simulate a mounted device, but they translate file system commands (like ls, cp, mv, ...) into commands for the underlying protocol, like MTP for Android. – s3lph Nov 14 '15 at 12:58