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?
2 Answers
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 fis a recursive way to list all files in all folders on that device.ls /mount/folderstat /mount/folderdirwhich is same asls
- 107,582
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:
mkdir /mnt/somedirwill create a directory with the namesomedir(change name at one's leisure) inside/mnt. Here you will mount the drive.mount /dev/sdc1 /mnt/somedirwill attempt to mount the partition/dev/sdc1(my USB stick's only partition) in/mnt/somedir- 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 groupplugdevor execute the mount command as root withsudo 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.
- 14,644
- 12
- 60
- 83