简体   繁体   English

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

[英]How can I recursively copy a directory into another and replace only the files that have not changed?

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仓库导出的数千个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) 如果导出版本包含的代码与webroot版本内的代码不同,则覆盖webroot中的文件(实时)
  2. 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 如果文件被覆盖,则保留文件的权限/用户/组(导出版本替换实时版本)*注意我希望保持webroots权限/所有权,但使用导出的内容
  3. No prompting/stopping of the copy of any kind (ie not verbose) 没有提示/停止任何类型的副本(即不详细)
  4. Recursive copy - obviously I would like to copy all* files folders and subfolders found in export 递归复制 - 显然我想复制导出中找到的所有*文件文件夹和子文件夹

I've done a bit of research into cp - would this do the job?: 我对cp进行了一些研究 - 这可以做到这一点吗?:

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

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. 它可能,但是只要exportwebroot的相应文件具有相同的内容但修改时间不同,您最终会执行不必​​要的复制操作。 You'd probably get slightly smarter behavior from rsync : 您可能从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. 此外,如果您需要这样做, rsync可以通过SSH连接将文件从一个主机复制到另一个主机。 Plus, it has a zillion options you can specify to tweak its behavior - look in the man page for details. 此外,它还有许多选项可供您调整以调整其行为 - 请查看手册页以获取详细信息。

EDIT : with respect to your clarification about what you mean by preserving permissions: you'd probably want to leave off the -p option. 编辑 :关于你通过保留权限澄清你的意思:你可能想要-p选项。

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

So looks like you got all the correct args to cp 所以看起来你得到了所有正确的args到cp

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. 如果你需要保留所有者,你当然必须以root身份完成。 The -p option is 'pass mode', meaning copy between locations; -p选项是“传递模式”,意味着在位置之间复制; -v is verbose (but not interactive; there's a difference); -v是冗长的(但不是交互式的;有区别的); -d means create directories as necessary; -d表示根据需要创建目录; -m means preserve modification time. -m表示保留修改时间。 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. 默认情况下,如果没有-u选项, cpio将不会覆盖目标区域中比源区域中的文件更新的文件。

暂无
暂无

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

相关问题 如何将整个嵌套目录中的头文件仅复制到另一个目录,在复制到新文件夹后保持相同的层次结构 - How can i copy only header files in an entire nested directory to another directory keeping the same hierarchy after copying to new folder 使用 make 仅复制已更改的文件 - Use make to only copy files that have changed 如何将文件名更改为数字的文件复制到同一目录中 - How do I copy files in the same directory with a number changed of filename 如何按文件类型递归查找文件并将它们复制到目录? - How to find files recursively by file type and copy them to a directory? 如何将 autoconf 构建复制到另一个目录以进行进一步测试? - How can I copy an autoconf build to another directory for further testing? 如果文件不在另一个目录中,则将文件复制到目录 - Copy files to directory if they are not in another directory Shell - 递归查找子目录并将包含的文件复制到另一个目录 - Shell - Recursively find sub-directories and copy containing files to another directory 如何使 shell 脚本递归扫描目录并根据名称模式将某些文件添加到 Git? - How can I make shell script to recursively scan a directory and add certain files based on their name patterns to Git? 我只需要在文本文件中获取包含其他目录中文件名的那些行 - I have to get only those lines in a text file that contain the filename of the files in another directory 如何将匹配的文件移动到另一个目录中? - How can I move matching files into another directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM