0

When you plug a flashdrive into Ubuntu, it creates a directory in /media/<username> with the flash drive name as the mount point name. When you unmount this directory, the directory cleanly goes away.

When you do mount <x> on the command line you always require a mount point which must be an existing dir. How do you do it without creating the folder? There must be a way.

  • 1
    I suppose "use an existing directory" is not a very satisfying answer? You do need to have some directory to mount to. – ilkkachu Dec 6 '17 at 8:15
  • 1
    Oh, why must it be possible to mount without a mount point? – fpmurphy Dec 6 '17 at 8:20
2

You always need a directory to use a mount point on a Unix-like system. There is software, e.g. automount and udev, that will automatically create the directory for you , but a directory must exist for the mount to actually occur.

1

Mounting a device means making its filesystem available through a specific directory (mountpoint) attached to the tree rooted at /.

Therefore, you always need an already existing directory to use as a mountpoint. Without it, you would not be able to access the mounted device.

Note that the directory used as mountpoint doesn't need to be empty; however, its old contents would be made inaccessible after the mount. For this reason, and to avoid confusion, usually devices are mounted on empty directories.

@Kusalananda's answer provides a function that makes the creation of the mountpoint automatic and transparent to the user, but under the hood the directory must always exist.

1

No , it is not possible to mount a device without creating a mount point, " the mount command require a mount point" :

mount -t type device dir

This tells the kernel to attach the filesystem found on device (which is of type type) at the directory dir

man mount

0

There must be a way.

No, there is no way.

How do you do it without creating the folder?

There has to be some directory given to mount as a mount point.


Maybe you asked bad. But I personally don't see any logic in your question.

0
mymount () {
    local args=( "$@" )
    local dir="${args[-1]}"

    test -d "$dir" || mkdir -p "$dir" || exit 1
    command mount "$@"
}

On most Unices, with most file systems, the mount utility requires that the mountpoint be an existing directory.

With the bash shell function above, this directory is created if it does not already exist. The mountpoint is assumed to be the last argument on the command line when invoking the function. This allows you to mount a device anywhere without manually creating the mountpoint.

This is probably somewhat like whatever it is that creates the /media/username mountpoint on your system does.

Likewise, you could define myumount that unmounted a device and removed its mountpoint.

This obviously does not get around the fact that the directory can't be created by mount (or deleted by umount), at least not by the mount implementations that I know about, but it allows you to hide the creation of the directory in a function.


Unix philosophy: Do one thing and do it well.

mount requires a directory to mount something at. Creating this directory is the task for mkdir.

  • 4
    Your answer does not meet the OPs requirement, i.e. without creating a directory – fpmurphy Dec 6 '17 at 8:23
  • @fpmurphy1 Can't be done in the general case, which is what I wrote. I then created a function that encapsulates the creation of the directory, so that it doesn't need to be created manually. – Kusalananda Dec 6 '17 at 8:31
0

You left unclear what you want to achieve and why you can't create a mountpoint dir, making this is a typical XY question.

If your main issue is to temporarily mount a drive without having to create and clean up the mountpoint, then you may use /mnt which is designated to this purpose by the Filesystem Hierarchy Standard. It is discouraged to use /mnt for long-term/permanent mounting.

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.