59

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?

slim
  • 847
  • 3
  • 9
  • 14
  • 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

2 Answers2

122

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
Gilles 'SO- stop being evil'
  • 69,786
  • 21
  • 137
  • 178
  • 7
    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
  • @Darael `mount --bind` makes a difference: if a filesystem is mounted on a non-empty directory in `/`, then `du -x /` will not count what's in that directory, because it's hidden by the mounted filesystem. With `mount --bind` you can get an unobstructed view of a filesystem. – Gilles 'SO- stop being evil' Feb 21 '17 at 13:00
  • @Gilles That's a very good point, though in general for "how to check usage of all the root directories without traversing mount points" I'd imagine it's unlikely there's much if anything being shadowed by mounts. – Darael Feb 21 '17 at 13:01
  • What if the `/` filesystem is almost full, you mount some `/mount-point/` with many files that disappear, and you keep filling up the `/`. Will the disappeared files make space for the new files, and consequently become overwritten? – DrBeco Mar 18 '23 at 02:39
  • @DrBeco If my bookshelf is full, and I hang a drape on it, does it make room for more books? Of course not. – Gilles 'SO- stop being evil' Mar 18 '23 at 09:30
  • Your analogy makes sense. Thanks for having the time to reply. – DrBeco Mar 21 '23 at 14:14
40

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

Azz
  • 4,695
  • 2
  • 26
  • 23
  • 2
    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
  • My bad, they go away while it's mounted, but come back when it's umounted. – Azz Oct 18 '10 at 14:11
  • 21
    +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
  • 13
    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
  • Is this the same if I remount instead of explicitly mounting? – CMCDragonkai Aug 03 '15 at 10:43
  • 6
    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 06 '15 at 00:24