52

If I attempt to mount a folder that already has files in it, does linux give me an error message or go ahead and show both the mounted filesystem and the files that were already in the folder?

  • 2
    Could always try it out with some test files, no? – Chris Oct 18 '10 at 14:35
  • I would of if I could. It just worked out that I didn't have anything to test with. I tried unmounting and mounting the drive in question but the results were inconclusive because they both had the same files. – slim Oct 18 '10 at 14:39
  • Is there a way to make the folder unwriteable so that files can't exist there? – endolith Sep 26 '16 at 23:57
33

It will just be mounted, and the files disappear, coming back when the folder is umounted.

  • 1
    What do you mean by disappear? They continue to exist on the server and are just not shown or are the deleted? – slim Oct 18 '10 at 14:08
  • I'll go have a quick check, but I think they are deleted. – Azz Oct 18 '10 at 14:10
  • 17
    +1 The files are simply invisible while the directory is mounted "over" them. They never really go away, the are just inaccessible... – sleske Oct 18 '10 at 14:50
  • 10
    It works like a stack, if you mount something else, it hides the previous content. When you unmount, the previous stuff becomes visible again. – vtest Oct 18 '10 at 15:09
  • 4
    I'm confused by how someone who said "coming back when the folder is unmounted" was able to say 3 minutes later "I think they are deleted". Thankfully for everyone else, the former is the reality here. – underscore_d Oct 6 '15 at 0:24
112

When you mount a filesystem on a directory /mount-point, you can no longer access files under /mount-point directly. They still exist, but /mount-point now refers to the root of the mounted filesystem, not to the directory that served as a mount point, so the contents of this directory cannot be accessed, at least in this way. For example:

# touch /mount-point/somefile
# ls /mount-point/somefile
/mount-point/somefile
# mount /dev/something /mount-point
# ls /mount-point/somefile
ls: cannot access /mount-point/somefile: No such file or directory

There are ways to get a merged view of the mounted filesystem and the data that was already present, but you need an extra layer called a union filesystem.

Under Linux, there is a way to see the hidden files. You can use mount --bind to get another view of the filesystem where the mount point is. For example

mount --bind / /other-root-view

You'll see all the files in the root filesystem under /other-root-view.

# cat /other-root-view/etc/hostname 
darkstar

In particular, /mount-point will now be accessible as /other-root-view/mount-point, and since /other-root-view/mount-point is not a mount point, you can see its contents there:

# ls /mount-point/somefile
ls: cannot access /mount-point/somefile: No such file or directory
# ls /other-root-view/mount-point/somefile
/other-root-view/mount-point/somefile
  • 5
    Gilles, this answer just saved my butt when I needed to get some asterisk recordings that got saved underneath an NSF mount point! I always thought --bind had the same perspective as the user. Thank you! – andyortlieb Jul 19 '11 at 14:55
  • What about directories? If I mounted at /mount-point/1/ then mounted another filesystem on /mount-point/, can I still access /mount-point/1/? – CMCDragonkai Nov 18 '15 at 12:57
  • @CMCDragonkai Yes, indirectly by using a bind mount as described in my answer. – Gilles 'SO- stop being evil' Nov 18 '15 at 17:46
  • Giles, this is a brilliant technique and has helped me analyze what’s on my own system. It has also helped with another question, which is how to check usage of all of the root directories without traversing mount points. Solution: mkdir /r; mount --bind / /r; du -sh /r/*. Thanks – Manngo May 31 '16 at 23:52
  • @Manngo for future reference, this is not necessary. du -x (equivalent to du --one-file-system) would have done that without the need for --bind shenanigans. – Darael Feb 21 '17 at 12:57

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.