Where do the files go if you mount a drive to a folder that already contains files?
Just "shadowed" and will be there again when unmounted. :)
In fact the files are "there" intact and if you need to reach them right away, w/o unmounting, this can be worked-around with so-called bind
mount:
mount --bind /Original/FS/Mount/Point /Somewhere/Else
It works (so) because when you ask kernel to mount a filesystem to some mountpoint, kernel treats that mountpoint as a "view port" to filesystem you're mounting, so it's expected you shall see mounted FS content there.
But this is not the only way how those FSes "layers" can be combined to single view. There's so called "union mount" approach (it's funny to know that this "a central concept in Plan 9", BTW). On Linux you could use Aufs, which never made its way into mainline kernel, or, currently (since 3.18), OverlayFS — it did.
The newly mounted filesystem is like an overlay which hides part of the initial filesystem. There are trick to access the files, like the bind mount as described by poige. On Linux I personally like the fact that you can mount a filesystem more than once on different mount points. So you can perfectly well mount the root filesystem on /
as well as on /mnt
. This comes in handy while debugging lost space / lost files / counting disk usage / ...
Gotcha's:
- The old files are still there and using up space. It is a common mistake to have 'missing' disk space in files hidden by a mount. E.g. when you accidentally wrote large files in a directory and at some time decided to mount a filesystem on it. E.g. accidentally starting a large database before the logging filesystem was properly mounted ...
- When a program has a file open which is subsequently hidden by a newly mounted filesystem, the program won't care about it and successfully keeps using the 'hidden' file until it closes it. From that moment, that file becomes invisible to the process until the new filesystem is unmounted and the underlying directory shows up again.