8

I know that by default, perhaps creating a directory would be a bad idea. However, I believe there isn't any 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?

2

4 Answers 4

1

mount doesn't create directories, because that's not its job. There isn't any 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.

3
  • 3
    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, 2017 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, 2017 at 14:44
  • I think we are better off with mount creating its directories (--mkdir). This creates some nice guarantees such as directory exists only if the mount was successful. It does not need sophisticated options, and when you think about designing it you should not worry about giving it every feature but it is necessary to know and understand what you want out of it.
    – KalEl
    Sep 5 at 21:47
18

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
  • 1
    This doesn't work on my system...
    – Attie
    Jun 29, 2017 at 16:33
  • FYI, I thought that the . between x-mount and mkdir was supposed to be a , - but it's not. Aug 12, 2019 at 15:38
  • 4
    manual says the x should be uppercase i.e. X-mount.mkdir, dunno if that matters
    – xeruf
    Dec 25, 2020 at 13:05
  • 1
    @xeruf: Covered in Marcin Depa's "answer" (though it ought to be converted to a comment). Mar 1 at 5:40
1

mount from util-linux now supports -m/--mkdir as a shortcut for X-mount.mkdir mentioned in few other answers. The option was implemented in 2021.

E.g. in Kubuntu 22.10 there is util-linux 2.38 from 2022-02-17 and you can use the option:

mount -m /dev/something /mountpoint/to/be/created

(Note: umount later does not remove any directory. The mountpoint will survive umount.)

0

As Larssend's answer, note that the lowercase option is deprecated. It should be X-mount.mkdir

The documentation says:

The option is also supported as x-mount.mkdir, notation is deprecated since v2.30.

Improved command:

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

You must log in to answer this question.

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