Linux 如何处理挂载点中的现有文件?

sli*_*lim 57 linux mount

如果我尝试挂载一个已经有文件的文件夹,linux 是否会给我一条错误消息或继续显示已挂载的文件系统和文件夹中已有的文件?

Gil*_*il' 118

在目录上挂载文件系统后/mount-point,您将无法再/mount-point直接访问其下的文件。它们仍然存在,但/mount-point现在指的是已挂载文件系统的根目录,而不是用作挂载点的目录,因此无法访问该目录的内容,至少无法以这种方式访问​​。例如:

# 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
Run Code Online (Sandbox Code Playgroud)

有多种方法可以获得挂载的文件系统和已经存在的数据的合并视图,但是您需要一个称为联合文件系统的额外层。

在 Linux 下,有一种方法可以查看隐藏文件。您可以使用mount --bind获取挂载点所在文件系统的另一个视图。例如

mount --bind / /other-root-view
Run Code Online (Sandbox Code Playgroud)

您将看到根文件系统中的所有文件/other-root-view

# cat /other-root-view/etc/hostname 
darkstar
Run Code Online (Sandbox Code Playgroud)

特别是,/mount-point现在可以作为 访问/other-root-view/mount-point,并且由于/other-root-view/mount-point不是挂载点,您可以在那里看到其内容:

# 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
Run Code Online (Sandbox Code Playgroud)

  • 吉尔斯,当我需要获得一些保存在 NSF 挂载点下的星号录音时,这个答案刚刚救了我的屁股!我一直认为 --bind 与用户具有相同的观点。谢谢! (7认同)

Azz*_*Azz 38

它只会被挂载,文件消失,当文件夹被卸载时又回来了。

  • +1 当目录安装在“之上”文件时,这些文件根本不可见。它们永远不会真正消失,只是无法访问...... (20认同)
  • 它就像一个堆栈,如果你挂载其他东西,它会隐藏以前的内容。当您卸载时,之前的内容将再次可见。 (13认同)
  • 我对说“卸载文件夹时回来”的人如何在 3 分钟后说“我认为它们已被删除”感到困惑。值得庆幸的是,对于其他人来说,前者是这里的现实。 (6认同)
  • 你说的消失是什么意思?它们继续存在于服务器上,只是未显示或已删除? (2认同)