What is the Command Line Equivalent of "Safely Remove Drive"?

What is the command line equivalent of the Nautilus feature called "Safely Remove Drive". Specifically, I am removing a USB flash drive.

1

5 Answers

The udisks command is most likely what you are looking for.

While sudo unmount /dev/sdXY will work, udisks can do this without root level (sudo) permissions.

If you have a drive /dev/sdXY, mounted, where X is a letter representing your usb disk and Y is the partition number (usually 1), you can use the following commands to safely remove the drive:

udisks --unmount /dev/sdXY
udisks --detach /dev/sdX

For a practical example, if I have the partition /dev/sdb1 mounted, I would run this to unmount and detach it:

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

If your drive is not mounted, or was never mounted, simply use the second command:

udisks --detach /dev/sdb

I originally found this through this question: .

Using udisks2:

In the newer ubuntu distributions (I'm unsure of when the switch occurred), udisks2 is installed instead of udisks.

Mirroring the commands above, to unmount and detach a disk with udisks2:

udisksctl unmount -b /dev/sdXY
udisksctl power-off -b /dev/sdX

Example if my drive is /dev/sdb1:

udisksctl unmount -b /dev/sdb1
udisksctl power-off -b /dev/sdb

Similarly to above, power-off can be used to detach the drive even if there are no partitions mounted, or no partition was ever mounted:

udisksctl power-off -b /dev/sdb
13

The actual equivalent to Nautilus Mount/Unmount operation is gvfs-mount -m -d /dev/ice /some/directory and gvfs-mount -u /some/directory. This uses the same API that Nautilus uses, GIO virtual file system (gvfs), which provides different tools to use several services as mount points, such smb, NFS, FTP, block devices, etc.

To identify which device you need to unmount just use gvfs-mount -l which should be enough.

This solution has the peculiarity that it doesn't require for elevated permissions, since everything is managed by the umount/gvfsd/polkit services, which further resemblances the similarity with Nautilus behavior.

2

Once you know the device, possibly using the df info as in @rcpao answer, the best way to "eject" the disk is, imho, using the same command that the graphical interface is using:

udisksctl unmount --block-device /dev/sdc1

I have a script to do a backup to a disk that I know will mount under /media/romano/movlin, and after the backup I do:

sync
udisksctl unmount -b $(mount | grep movlin | cut -d" " -f1)

Here, mount | grep movlin | cut -d" " -f1 will extract the device that is mounted under the label "movlin", (would be /dev/sdc1 in that case), and then it unmounts it.

5
  1. df to find the mount point of your flash drive.

    rcpao@bun:~$ df
    Filesystem 1K-blocks Used Available Use% Mounted on
    /dev/mapper/ubuntu--vg-root 1916153032 658404668 1160390336 37% /
    none 4 0 4 0% /sys/fs/cgroup
    udev 16438692 4 16438688 1% /dev
    tmpfs 3289976 2156 3287820 1% /run
    none 5120 0 5120 0% /run/lock
    none 16449860 18768 16431092 1% /run/shm
    none 102400 48 102352 1% /run/user
    /dev/sda1 240972 98990 129541 44% /boot
    /dev/sdc1 60915712 20992 60894720 1% /media/rcpao/SD024-64GB
  2. Unmount using either /dev/sdc1 or /media/rcpao/SD024-64GB.

    rcpao@bun:~$ sudo umount /dev/sdc1
    [sudo] password for rcpao:
    rcpao@bun:~$

    or

    rcpao@bun:~$ sudo umount /media/rcpao/SD024-64GB
    [sudo] password for rcpao:
    rcpao@bun:~$
  3. You should be able to see the flash drive's eject icon disappear in nautilus as soon as umount finishes.

6

eject from the eject package:

sudo eject /dev/sdX

appears to umount all partitions, and put the device in a state that you must remove and reattach it to remount.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like