When I connect a device (tablet, camera, flash drive, external harddrive) to my laptop with Linux I often type the following sequence of commands.$ ls /dev/sd*Let's say that my device is /dev/sdcoptional $ sudo mkdir /media/my_flashoptional $ sudo chmod 777 /media/my_flash$ sudo mount /dev/sdc /media/my_flashThen I will copy some files and$ sudo umount /dev/sdcThis sequence guarantees me that the files are copied and nothing bad will happen.Could you tell me how to automate this sequence of commands? So I will only plug the device, it will recognize to which directory it should be mounted, then I will make some file transfer and then it will signal me (in some way) I can unplug the device? Is there some settings in Linux OS for this?
Automatizing the sequence of mounting commands
mount
The system component that reacts to the connection of a removable device is Udev, as mentioned by SHW. Even the udev tutorial can be a little daunting; I'll show you a couple of examples.There are two steps involved: associating a device file (e.g. /dev/sdc) with the hardware device, and mounting the device to access the filesystem. Udev's job is the first step, though you can tell it to run an external command such as mount.For known removable devices, I like to use a dedicated device name under /dev/removable (that directory name is a personal convention). The following udev rules (to be placed in /etc/udev/rules.d/my_removable_disks.rules) create symbolic links with known names for two disks, both identified by a property of the filesystem on their partition 1:KERNEL==sd?, PROGRAM==/sbin/blkid -o value -s UUID %N1, RESULT==1234-5678, SYMLINK+=removable/fooKERNEL==sd?, PROGRAM==/sbin/blkid -o value -s LABEL %N1, RESULT==Joe's disk, SYMLINK+=removable/joeOlder versions of udev may need /udev/lib/vol_id -u %N1 (for the UUID, -l for the label) instead of the blkid call. There are more things you can match on, e.g. ATTRS{vendor}==Yoyodine, ATTRS{serial}==XYZZY12345PDQ97 (instead of PROGRAM==, RESULT==) to match a device's vendor and serial number.Then you can use a line like this in /etc/fstab:/dev/removable/joe  /media/joe  vfat  noauto,userIf you prefer an automatic mount, you can add something like , RUN=mkdir /media/foo && mount /dev/removable/foo /media/foo to the udev line. Don't forget to umount /media/foo before unplugging.