Linux 如何精确复制文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19434921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:07:08  来源:igfitidea点击:

How to duplicate a folder exactly

linuxbashunixubuntu

提问by TopGunCoder

I am trying to copy a filesystem for a device I am programming for. After so much time trying to figure out why the filesystem I was installing wasn't working I found out that cpdidn't get the job done. I used du -sto check the size of the original filesystem and the one that I copied with cp -r, as it turns out they differ by about 150 bytes.

我正在尝试为我正在编程的设备复制文件系统。经过这么多时间试图弄清楚为什么我正在安装的文件系统无法正常工作后,我发现cp没有完成工作。我过去常常du -s检查原始文件系统的大小和我用 复制的文件系统的大小,cp -r结果发现它们相差大约 150 个字节。

Something is telling me that symbolic links or some sort of kernel objects aren't being copied correctly.

有些东西告诉我符号链接或某种内核对象没有被正确复制。

Is it possible to copy a folder/file system exactly? If so how would I go about it?

是否可以准确复制文件夹/文件系统?如果是这样,我将如何处理?

采纳答案by damienfrancois

Another popular option is to use tar c source | (cd target && tar x ). See this linuxdevcenter.com article.

另一个流行的选择是使用tar c source | (cd target && tar x ). 请参阅这篇 linuxdevcenter.com 文章。

回答by seanmcl

Rsyncis the best way to copy a file system. They are myriad argumentsthat let you control exactly what is copied.

Rsync是复制文件系统的最佳方式。它们是无数参数,可让您准确控制复制的内容。

回答by Gilles Quenot

Try doing this the straightforward way :

尝试以直接的方式执行此操作:

cp -a src target

from man cp

man cp

    -a, --archive
           same as -dR --preserve=all

It preserve rights, symlinks...

它保留权利,符号链接......

回答by janos

The most accurate way I know of copying files is with cpio:

我所知道的最准确的复制文件的方法是cpio

cd /path/to/source
find . -xdev -print0 | cpio -oa0V | (cd /path/to/target && cpio -imV)

Not really easy to use, but this is veryprecise, preserving timestamps, owners, permissions, special files.

不是很容易使用,但它非常精确,保留了时间戳、所有者、权限、特殊文件。

回答by Chetabahana

Here I tried all the code in my Linux. Seems Rsyncproposed by @seanmclas the right one while others failed to keep ownersand/or some special filesor a denied result. The exact code is:

在这里,我尝试了 Linux 中的所有代码。似乎@seanmcl提议的Rsync是正确的,而其他人未能保留所有者和/或某些特殊文件拒绝结果。确切的代码是:

$ sudo rsync -aczvAXHS --progress /var/www/html /var/www/backup

Just remember to use just the directory name and not put a slash(/) or a wildcard(/*) at the end of sourceand targetname otherwise the hidden files right below the source are not copied.

请记住只使用目录名称,而不是名称和目标名称的末尾放置斜杠( /) 或通配符( /*),否则不会复制源正下方的隐藏文件。