Sample Header Ad - 728x90

Unmount all nested directories under path

2 votes
3 answers
421 views
**Let's consider these conditions:** - There's multiple **nested** mounts with a lot of folders/files in /mnt:
sdb      8:16   0   300G  0 disk
   ├─sdb1   8:17   0   256M  0 part /mnt/1/1
   ├─sdb2   8:18   0 199.7G  0 part /mnt/2
   └─sdb3   8:19   0   100G  0 part /mnt/3/1/2
- Let's pretend I don't know which device(s) (such as /dev/sdb) are mounted under /mnt (as I'd like to use this in a unattended script). - The path to mounted directories aren't known either. - not mounted error shouldn't stop the process **Here's what I've tried with illustration of the result:**
$ umount --all-targets --recursive /mnt; lsblk /dev/sdb
umount: /mnt: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part /mnt/1/1
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*/**; lsblk /dev/sdb
umount: /mnt/2/boot: not mounted
umount: /mnt/2/ostree: not mounted
umount: /mnt/3/1: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*?; lsblk /dev/sdb
umount: /mnt/1: not mounted
umount: /mnt/3: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part /mnt/1/1
├─sdb2   8:18   0 199.7G  0 part
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2

$ umount --all-targets --recursive /mnt/*/**?; lsblk /dev/sdb
umount: /mnt/2/boot: not mounted
umount: /mnt/2/ostree: not mounted
umount: /mnt/3/1: not mounted
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdb      8:16   0   300G  0 disk
├─sdb1   8:17   0   256M  0 part
├─sdb2   8:18   0 199.7G  0 part /mnt/2
└─sdb3   8:19   0   100G  0 part /mnt/3/1/2
Is there any good way of doing this without installing any extra packages?
Asked by GrabbenD (170 rep)
Sep 28, 2023, 09:19 AM
Last activity: Mar 29, 2024, 07:33 PM