如何以递归方式将目录复制到另一个目录并仅替换未更改的文件?

时间:2022-06-19 15:05:50

I am looking to do a specific copy in Fedora.

我希望在Fedora中做一个特定的副本。

I have two folders:

我有两个文件夹:

  • 'webroot': holding ALL web files/images etc

    'webroot':持有所有网络文件/图像等

  • 'export': folder containing thousands of PHP, CSS, JS documents that are exported from my SVN repo.

    'export':包含从我的SVN repo导出的数千个PHP,CSS,JS文档的文件夹。

The export directory contains many of the same files/folders that the root does, however the root contains additional ones not found in export.

导出目录包含许多与root相同的文件/文件夹,但是根目录包含导出中找不到的其他文件/文件夹。

I'd like to merge all of the contents of export with my webroot with the following options:

我想通过以下选项将导出的所有内容与我的webroot合并:

  1. Overwriting the file in webroot if export's version contains different code than what is inside of webroot's version (live)
  2. 如果导出版本包含的代码与webroot版本(实时)内的代码不同,则覆盖webroot中的文件
  3. Preserve the permissions/users/groups of the file if it is overwritten (the export version replacing the live version) *NOTE I would like the webroots permissions/ownership maintained, but with export's contents
  4. 如果文件被覆盖,则保留文件的权限/用户/组(导出版本替换实时版本)*注意我希望保持webroots权限/所有权,但使用导出的内容
  5. No prompting/stopping of the copy of any kind (ie not verbose)
  6. 没有提示/停止任何类型的副本(即不详细)
  7. Recursive copy - obviously I would like to copy all* files folders and subfolders found in export
  8. 递归复制 - 显然我想复制导出中找到的所有*文件文件夹和子文件夹

I've done a bit of research into cp - would this do the job?:

我对cp做了一些研究 - 这可以做到这一点吗?:

cp -pruf ./export /path/to/webroot

3 个解决方案

#1


24  

It might, but any time the corresponding files in export and webroot have the same content but different modification times, you'd wind up performing an unnecessary copy operation. You'd probably get slightly smarter behavior from rsync:

它可能,但是只要导出和webroot中的相应文件具有相同的内容但修改时间不同,您最终会执行不必​​要的复制操作。你可能会从rsync获得稍微聪明的行为:

rsync -pr ./export /path/to/webroot

Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details.

此外,如果您需要这样做,rsync可以通过SSH连接将文件从一个主机复制到另一个主机。此外,它还有许多选项可供您调整以调整其行为 - 请参阅手册页以获取详细信息。

EDIT: with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option.

编辑:关于你通过保留权限澄清你的意思:你可能想要省略-p选项。

#2


6  

  1. -u overwrites existing files folder if the destination is older than source
  2. 如果目标早于源,则-u将覆盖现有文件文件夹
  3. -p perserves the permission and dates
  4. -p持有权限和日期
  5. -f turns off verbosity
  6. -f关闭详细程度
  7. -r makes the copy recursive
  8. -r使副本递归

So looks like you got all the correct args to cp

所以看起来你得到了所有正确的args到cp

#3


5  

Sounds like a job for cpio (and hence, probably, GNU tar can do it too):

听起来像cpio的工作(因此,可能,GNU tar也可以这样做):

cd export
find . -print | cpio -pvdm /path/to/webroot

If you need owners preserved, you have to do it as root, of course. The -p option is 'pass mode', meaning copy between locations; -v is verbose (but not interactive; there's a difference); -d means create directories as necessary; -m means preserve modification time. By default, without the -u option, cpio won't overwrite files in the target area that are newer than the one from the source area.

如果你需要保留所有者,你当然必须以root身份进行。 -p选项是'pass mode',表示位置之间的复制; -v是冗长的(但不是交互式的;有区别的); -d表示根据需要创建目录; -m表示保留修改时间。默认情况下,如果没有-u选项,cpio将不会覆盖目标区域中比源区域中的文件更新的文件。

#1


24  

It might, but any time the corresponding files in export and webroot have the same content but different modification times, you'd wind up performing an unnecessary copy operation. You'd probably get slightly smarter behavior from rsync:

它可能,但是只要导出和webroot中的相应文件具有相同的内容但修改时间不同,您最终会执行不必​​要的复制操作。你可能会从rsync获得稍微聪明的行为:

rsync -pr ./export /path/to/webroot

Besides, rsync can copy files from one host to another over an SSH connection, if you ever have a need to do that. Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details.

此外,如果您需要这样做,rsync可以通过SSH连接将文件从一个主机复制到另一个主机。此外,它还有许多选项可供您调整以调整其行为 - 请参阅手册页以获取详细信息。

EDIT: with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option.

编辑:关于你通过保留权限澄清你的意思:你可能想要省略-p选项。

#2


6  

  1. -u overwrites existing files folder if the destination is older than source
  2. 如果目标早于源,则-u将覆盖现有文件文件夹
  3. -p perserves the permission and dates
  4. -p持有权限和日期
  5. -f turns off verbosity
  6. -f关闭详细程度
  7. -r makes the copy recursive
  8. -r使副本递归

So looks like you got all the correct args to cp

所以看起来你得到了所有正确的args到cp

#3


5  

Sounds like a job for cpio (and hence, probably, GNU tar can do it too):

听起来像cpio的工作(因此,可能,GNU tar也可以这样做):

cd export
find . -print | cpio -pvdm /path/to/webroot

If you need owners preserved, you have to do it as root, of course. The -p option is 'pass mode', meaning copy between locations; -v is verbose (but not interactive; there's a difference); -d means create directories as necessary; -m means preserve modification time. By default, without the -u option, cpio won't overwrite files in the target area that are newer than the one from the source area.

如果你需要保留所有者,你当然必须以root身份进行。 -p选项是'pass mode',表示位置之间的复制; -v是冗长的(但不是交互式的;有区别的); -d表示根据需要创建目录; -m表示保留修改时间。默认情况下,如果没有-u选项,cpio将不会覆盖目标区域中比源区域中的文件更新的文件。