简体   繁体   English

如何使用链接在Linux上创建目录的副本

[英]How to create a copy of a directory on Linux with links

I have a series of directories on Linux and each directory contains lots of files and data. 我在Linux上有一系列目录,每个目录包含大量文件和数据。 The data in those directories are automatically generated, but multiple users will need to perform more analysis on that data and generate more files, change the structure, etc. 这些目录中的数据是自动生成的,但是多个用户需要对该数据执行更多分析并生成更多文件,更改结构等。

Since these data directories are very large, I don't want several people to make a copy of the original data so I'd like to make a copy of the directory and link to the original from the new one. 由于这些数据目录非常大,我不希望有几个人制作原始数据的副本,所以我想复制一个目录并从新目录链接到原始数据。 However, I'd like any changes to be kept only in the new directory, and leave the original read only. 但是,我希望任何更改只保留在新目录中,并保留原始只读。 I'd prefer not to link only specific files that I define because the data in these directories is so varied. 我不想只链接我定义的特定文件,因为这些目录中的数据是如此多样化。

So I'm wondering if there is a way to create a copy of a directory by linking to the original but keeping any changed files in the new directory only. 所以我想知道是否有办法通过链接到原始目录来创建目录的副本,但只保留新目录中的任何更改的文件。

It turns out this is what I wanted to: 事实证明这就是我想要的:

cp -al <origdir> <newdir>

It will copy an entire directory and create hard links to the original files. 它将复制整个目录并创建指向原始文件的硬链接。 If the original file is deleted, the copied file still exists, and vice-versa. 如果删除原始文件,则复制的文件仍然存在,反之亦然。 This will work perfectly, but I found newdir must not already exist. 这将完美地工作,但我发现newdir必须不存在。 As long as the original files are read-only, you'll be able to create an identical, safe copy of the original directory. 只要原始文件是只读的,您就可以创建原始目录的相同安全副本。

However, since you are looking for a way that people can write back changes, UnionFS is probably what you are looking for. 但是,由于您正在寻找人们可以回写更改的方式,因此UnionFS可能就是您要寻找的。 It provides means to combine read-only and read-write locations into one. 它提供了将只读和读写位置组合成一个的方法。

Unionfs allows any mix of read-only and read-write branches, as well as insertion and deletion of branches anywhere in the fan-out. Unionfs允许任何混合的只读和读写分支,以及在扇出中的任何地方插入和删除分支。


Originally I was going to recommend this (I use it a lot): 本来我打算推荐这个(我经常使用它):

Assuming the permissions aren't an issue (eg only reading is required) I would suggest to bind-mount them into place. 假设权限不是问题(例如,只需要阅读),我建议将它们绑定到位。

mount -B <original> <new-location>
# or
mount --bind <original> <new-location>

<new-location> must exist as a folder. <new-location>必须作为文件夹存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM