I am confused. Linux filesystem is a tree structure, with the root node(starting node) as the root directory.
Now let's suppose I have a folder abc
at location /home/abc
and another folder xyz
at location /home/xyz
Folder xyz
consists of some other folders and files inside it. (ex def
and mno
are folders inside it)
/
├── home/
│ ├── abc
│ └── xyz
│ ├── def
│ └── mno
When I run the command
mount --rbind /home/xyz /home/abc
(rbind is recursively bind)
I see all the contents of the folder xyz
in abc
.
Now, when I just run the command
mount --bind /home/xyz /home/abc
I still see all the contents of xyz
in abc
.
Why is that?
--bind
to work just as similarly to --rbind
.
You've rightly observed that, with both
--bind
and--rbind
, you see directories under the bind mount.The difference is that, with
--rbind
but not with--bind
, you see the contents of other bind mounts under the bind mount.As applied to your example, suppose for simplicity that
/home/xyz/def
and/home/xyz/mno
are empty directories. Suppose further that you then use them as bind mounts, i.e., you use them as mount points inmount --bind
ormount --rbind
. This causes them to appear nonempty. Then, suppose you run:Then,
/home/abc/def
and/home/abc/mno
both exist, but they appear empty, because you usedmount --bind
, which is nonrecursive.In contrast, suppose you instead made
/home/abc
a bind mount by running this command:Then,
/home/abc/def
and/home/abc/mno
both exist and they appear nonempty--they have the contents of the/home/xyz/def
and/home/xyz/mno
bind mounts--because you usedmount --rbind
, which is recursive.Here's a fully worked-out example:
Read
man 8 mount
. It says (emphasis added):The reason being is bind and rbind solve different problems.
For example, my son wants to access my media database from windows across the country. So I grant him an ssh login, and tell him to install sshfs. Only sshfs in windows acts like it is in a chroot jail. So to give him access I do:
Now he can travers all the drives I have mounted for my share folder.
But suppose I want to make a backup of /share to a remote host, with out all the other drives mounted underneath. I can certainly tell rsync just to backup one file system. But then it will also skip the mount points. If I actually want to copy the mount points as well I should do something like:
Now I have a version of the share folder free of any mounts.
Most people assume that mount points are empty directories. But sometimes it is useful to populate files there. For example, if your website is stored on a separate drive, maybe you want a set of files that will redirect users to your backup site should your mount fail.