Linux (Ubuntu): safely remove the USB flash drive through the command line

It would be great if your shell script helps you safely remove the flash drive after shutdown. I did a lot of homework before posting a question asking for a solution that is expected to satisfy the following needs:

  • Suppose a flash drive is installed as /media/A along with many other flash drives, and the code can selectively delete /media/A without interfering with other drives. For example, I can pass /media/A as an argument.
  • Light solution. Installing another 100 MB package to complete the task leads to madness.
  • I know a few commands like hdparm , except that their documents are hard to read. If you can offer me a solution, I hope that he can also offer an explanation of the parameters, etc.
  • Best of all, if the code can handle all types of flash drives (for example, my old one, bought in 2003 from 32 MB) (and portable disks are optional).
  • It should achieve the exact effect by right-clicking on the disk → clicking "safely remove the disk". A security risk will prevail, otherwise you can simply disconnect the drive, which usually does not hurt.
+45
ubuntu usb
Nov 05 2018-12-12T00:
source share
1 answer

You can do this with udisks. it is not installed by default, but simple enough to install (the package is installed as the mega-size after installation) ...

 sudo apt-get install udisks 

After installation, you can disconnect the USB drive with the following commands ...

 sudo udisks --unmount /dev/sdb1 sudo udisks --detach /dev/sdb 

The first line disables it. Like any other partition, you can still remount it at this point. The second line separates it. After that, it disconnects and you need to remove / reinsert it to reconnect.

To clarify, sdb is the device, and sdb1 / 2/3 / etc are partitions on the device. In addition, before attempting to disconnect the device, you must disable all mount points. Usually there is only one with a USB stick / drive, but this is a warning you should be aware of nonetheless.

Getting the device name from the mount point will require you to pull it out of mount or something like that.

+47
Nov 05 '12 at 1:27
source share



All Articles