4

I know that by default, perhaps creating a directory would be a bad idea, however I believe there's no reason not to allow mount to create directories with an option.

Is there a reason for the missing feature, or has it simply not been implemented yet?

1

mount doesn't create directories because that's not its job. There is no need for it to do so, it's not a missing feature.

If you're going to ask for such a feature, then you need to think about the behaviour as well... For example, does it work like mkdir or like mkdir -p? At this point it's easier to just use mkdir in the occasional scenario that you want to make a directory.

2
  • 1
    I was going to write an answer like this ("do one thing and do it well") but then I realized mount nowadays can create loop devices. It works like losetup when it needs to, despite "that's not its job". Jun 29 '17 at 14:15
  • @KamilMaciorowski agreed, though I think that line is a little more blurry, as automating losetup allows you to mount filesystem images (rather than just block devices) with a single command, which is only a small extension of its purview. Tough to call...
    – Attie
    Jun 29 '17 at 14:44
11

In Linux, you can tell mount to create the target directory using an option (x-mount.mkdir). For example:

mount /dev/sdb1 -t ext4 -o rw,x-mount.mkdir /newdir/mntpoint

tells mount to create /newdir/mntpoint if it does not exist. The behavior is identical to mkdir -p, so if /newdir doesn't exist, it will create it for you. If either /newdir or /newdir/mntpoint exists, it will not fail -- it creates directories as needed.

4
  • This doesn't work on my system...
    – Attie
    Jun 29 '17 at 16:33
  • FYI, I thought that the . between x-mount and mkdir was supposed to be a , - but it's not. Aug 12 '19 at 15:38
  • @larssend answer worked perfectly in my situation. The only one I was able to find that did work! Dec 4 '19 at 15:11
  • manual says the x should be uppercase i.e. X-mount.mkdir, dunno if that matters
    – Xerus
    Dec 25 '20 at 13:05

Your Answer

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

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