3

My system has a USB, an sd card and an SSD connected. I mounted all the devices USB(/dev/sdb1), SSD(/dev/sda1) and sd card(/dev/mmcblk1p1) under /mnt. Is there any way to unmount all the devices connected at /mnt?

(I can do this by performing grep to lsblk/df/mount output and unmounting individual one but I am looking for another easy solution OR by performing 3 times umount /mnt)

Update 2

Here is output of lsblk(multiple mountpoints)

lsblk
NAME         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda            8:0    0 232.9G  0 disk 
└─sda1         8:1    0 232.9G  0 part /mnt
sdb            8:16   1  14.7G  0 disk 
└─sdb1         8:17   1  14.7G  0 part /mnt

and after performing recursive umount

sudo umount --recursive /mnt
lsblk
NAME         MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
sda            8:0    0 232.9G  0 disk
└─sda1         8:1    0 232.9G  0 part /mnt
sdb            8:16   1  14.7G  0 disk
└─sdb1         8:17   1  14.7G  0 part
5
  • Can you post the output of mount and the output (if any) of sudo umount --recursive /mnt
    – j-money
    Feb 8, 2019 at 10:59
  • @j-money I have added an update to the question Feb 8, 2019 at 14:24
  • Better question. Why are you mounting so many partitions to a single point? This sounds like a bad idea....
    – j-money
    Feb 8, 2019 at 15:55
  • @j-money, I agree with what you are saying. But, let's say some (bad)programs are mounting these devices under the same tree(/mnt) and In my program, when I want to mount my device at /mnt I want to unmount all the existing ones. Hence I want to know is there any easy way of doing. Feb 10, 2019 at 4:40
  • Get rid of the program. In this obscure situation, the results are as expected. I do not think there is a way to unmount all mounted points
    – j-money
    Feb 10, 2019 at 9:56

4 Answers 4

6
-A, --all-targets
  Unmount all mountpoints in the  current  namespace  for  the  specified
  filesystem.   The filesystem can be specified by one of the mountpoints
  or the device name (or UUID, etc.).  When this option is used  together
  with  --recursive,  then  all  nested  mounts within the filesystem are
  recursively unmounted.  This option is only supported on systems  where
  /etc/mtab is a symlink to /proc/mounts.

Is that maybe what you want? From man 8 umount.

2
  • This does not work.
    – mook765
    May 9, 2020 at 7:19
  • 4
    umount -A --recursive <path> works. Dec 30, 2020 at 17:46
3

This one-liner works for me:

while [[ $(findmnt /mnt) != "" ]]; do sudo umount /mnt; done

Explanation:

If the command findmnt /mnt produces non-empty output, something is mounted under /mnt. The test checks if the output is empty or not and if the output is not empty, we run umount /mnt once. If findmnt /mnt produces empty output, nothing is mounted under /mnt anymore and we are done.

If you run as root you can remove sudo from the line. If you run as normal user, you need sudo for the umount-command, but you need to enter your password only once.

4
  • Seems like it does not find any mounted directories under /mnt
    – alper
    Dec 25, 2021 at 11:19
  • @alper I don't know what you are trying to do, this one-liner is designed to check if anything is mounted on /mnt and if so, to unmount it. If you talk about a different mount-point such as /mnt/sub, then you have to adapt the command accordingly.
    – mook765
    Dec 25, 2021 at 11:48
  • I have a folder in my home directory. /home/user/mount_folder and there are some mounted directories 2 depth under it, /home/user/mount_folder/*/mounted_i // Seems like it unable to findmnt them. As I understand it requires exact folder path of the mounted directory
    – alper
    Dec 25, 2021 at 12:35
  • 1
    @alper Yes, you have to use the correct path to the mountpoint. Feel free to take a look at man findmnt.
    – mook765
    Dec 25, 2021 at 12:49
0

Try with lazy unmount:

sudo umount -l /mnt
7
  • This still doesn't unmount all the /mnt mount points. It just unmounts the last mounted device. Feb 8, 2019 at 9:13
  • Sorry, try "sudo umount -R /mnt" instead. Feb 8, 2019 at 9:50
  • This also works same, just unmounted the last mounted device. The other devices are still mounted. Also, I didn't find the '-R' option in the umount manual. Feb 8, 2019 at 10:48
  • 2
    please fix your answer as umount -l /mnt is not what op wants or needs.
    – j-money
    Feb 8, 2019 at 10:58
  • 1
    The easy solution is to intelligently mount your partitions not to the same mount point
    – j-money
    Feb 8, 2019 at 17:09
0

You can umount the device itself, wildcards allowed.

umount /dev/sda1 /dev/sdb? /dev/sdc*
1
  • You'd need to know in which order the partitions have been mounted.
    – mook765
    May 9, 2020 at 6:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .