53

When using the mount utility, if you specify a directory that does not exist, that is considered an error. Is there any option I can use so that mount points get automatically created if they don't already exist?

  • 9
    Strange that the mount utility doesn't do this by default. I'm sick of typing: mkdir /media/USB, mount /dev/sdb1 /media/USB; umount /dev/sdb1, rmdir /media/USB. – Zaz Sep 14 '14 at 21:29
  • Yep, I too wish this was a feature. – Sridhar Sarnobat Jan 8 '17 at 4:33
  • Yeah, I understand it not doing it by default, similar to "mkdir /one/two/three" won't get created if "/one/two" doesn't exist, but shouldn't there at least be an option to force it with mount? – UnsettlingTrend Mar 30 '17 at 14:59
  • 1
    HERE IS THE ANSWER: askubuntu.com/questions/88523/… - instructions for installing pmount to prevent this from happening. – SDsolar Nov 16 '17 at 22:25
  • I tried pmount, doesn't seem to work without manually running the command with the device. found this, seems to work great (16.04, headless) serverfault.com/a/767079/453980 mounts on boot, and handles hot(un/)plugging – travis h. Jan 27 '18 at 18:07
47

try this:

if you want to create a Mount Point called DISK1, then type the following command in the terminal :

sudo mkdir /media/DISK1
  • 1
    Yeah, I was hoping for an option to mount, but this is fine, I guess. – Paul Dec 17 '11 at 21:20
  • Pulling out the drive then reinserting it will create /media/DISK2 unless you install pmount. – SDsolar Nov 16 '17 at 22:22
7

No, the bare mount utility do not offer such an option.

It is done when mounting from a file manager like Nautilus, though.

  • 2
    how can Nautilus do it then? – Andrea Borga May 4 '13 at 7:58
  • 2
    @AndreaBorga it checks if the directory exists before mounting. If the mountpoint doesn't exist as a directory, creates it and then mounts. – omninonsense Apr 2 '15 at 10:41
2

Install the pmount utility and let it handle /media/user/ mount points automatically. No more worries about how and where /media/user/ devices mount and by which names.

sudo apt-get update
sudo apt-get install pmount

Then whenever you insert a removable device, it will automatically be mounted under /media/username/, by name.

For example: My USB stick is named "SYSBKP" so it automatically mounts as /media/pi/SYSBKP

It also handles unexpected ejects. And remounts. Silently.

  • Generally, it will pop up file manager to show you what you just mounted.

Behind the scenes it makes sure the buffers are always flushed just in case the USB is pulled without warning. No more need for sync;sync;sync like the old days.


FYI, I installed from a 14.04 LTS disk and let it upgrade me to 16.04 LTS when it offered. On a fresh SSD drive - I let it take the entire space.

In any case, pmount is a very important piece of software to have if you use /media/username/ for removable devices.

If you don't use it, the default action is to create new mount points for each insertion, with a digit appended to the name for each one. Even normal ejects cause this behavior. You end up with mount points like /media/username/SYSBKP, /media/username/SYSBKP1, /media/username/SYSBKP2, etc.- and you don't know which one is the active one.

This means you can't write scripts that assume the mount point name (unless you include all the mount commands in the script). Should you choose the wrong one it is treated as a local directory.

pmount makes sure your USB drive it has its own assigned mount point under /media/username/. In this case, it is always /media/pi/SYSBKP

It also works when you put in another USB flash drive with the same name. So it is not going by UUID.

I not only use this for flash drives, but also for fulll-size powered backup drives, for rsync backups, and have full confidence that the scripts will work without modification.

It is a simple, reliable solution to creating mount points.


Note that I have personally tested it with NTFS, FAT32 and the various EXT formatted drives. If the drive contains multiple partitions it will mount regular file systems individually by name, avoiding the swap partition. This is all visible in nautilus, and is alluded to in more detail in this Q&A:

https://raspberrypi.stackexchange.com/questions/82347/win32diskimager-not-enough-space-on-disk-size-62652416-sectors-available-623/82367#82367


I see in comments there is an issue with the NFS+ Time Machine drives.
It may or may not work automatically

How to access Time Machine Drives in Linux (macworld.com)

EXCERPT:

It turns out Apple does a couple slick things with the file system to make incremental backups work, including hard linking to directories, which isn't allowed in Linux. So for anyone that needs to access their Time Machine from something other than its associated Mac, here's how you do it... (see linked article for the rest)

  • 1
    "This is built into Ubuntu 16.04 LTS" It is? It's not in my 16.04 VM. packages.ubuntu.com/search?keywords=pmount says its in universe, and packages in universe are not part of the default install. – muru Aug 1 '17 at 3:00
  • I added install instructions now, muru. TNX – SDsolar Nov 16 '17 at 22:23
  • somehow even after installing pmount when i insert a time machine backup on a drive, it doesn't mount automatically. I've seen many posts reporting that normally it auto mounts. – gideon Apr 15 '18 at 13:42
  • The issue with Time Machine is that it is HFS+ file system. pmount will work with NTFS, FAT32, EXT2-4 for sure. Here are instructions for using the regular mount command for Time Machine: hints.macworld.com/article.php?story=20080623213342356 – SDsolar Apr 15 '18 at 17:33
  • btw, uninstalling pmount will not solve the "sometimes it will, but sometimes it won't" problem with Time Machine drives. I've edited the answer to include info on this issue.. Thank you, gideon, for bringing this up. – SDsolar Apr 15 '18 at 17:46

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.