Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When mounting a filesystem image via mount -o loop, is there any mechanism to prevent the same file to be mounted a second time?

share|improve this question
up vote 2 down vote accepted

There is no specific command that may stop the file to be mounted several times with different mountpoints, but you may use this script to not mount it if it is already mounted:

mount |grep -qF "$1" || mount "$1" $2 -o loop

the first parameter is a file to mount, and second is a mount point to use.

share|improve this answer
Hrm, so that's suboptimal... test $(losetup -j "$1" | wc -l) -eq 0 && mount "$1" $2 -o loop will check for a loopdevice even if it's unmounted by the way. – Tobias Kienzler Nov 2 '12 at 8:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.