简体   繁体   English

如何在bash中以递归方式将同名文件从一个目录结构复制到另一个目录结构?

[英]How can I recursively copy same-named files from one directory structure to another in bash?

I have two directories, say dir1 and dir2 , that have exactly the same directory structure. 我有两个目录,比如dir1dir2 ,它们具有完全相同的目录结构。 How do I recursively copy all the *.txt files from dir1 to dir2 ? 如何以递归方式将所有*.txt文件从dir1复制到dir2

Example: 例:

I want to copy from 我要复制

dir1/subdir1/file.txt
dir1/subdir2/someFile.txt
dir1/.../..../anotherFile.txt

to

dir2/subdir1/file.txt
dir2/subdir2/someFile.txt
dir2/.../..../anotherFile.txt

The .../... in the last file example means this could be any sub-directory, which can have sub-directories itself. 最后一个文件示例中的.../...表示这可以是任何子目录,它本身可以有子目录。

Again I want to do this programmatically. 我想再次以编程方式执行此操作。 Here's the pseudo-code 这是伪代码

SRC=dir1
DST=dir2
for f in `find ./$SRC "*.txt"`; do
   # $f should now be dir1/subdir1/file.txt
   # I want to copy it to dir2/subdir1/file.txt
   # the next line coveys the idea, but does not work
   # I'm attempting to substitute "dir1" with "dir2" in $f,
   # and store the new path in tmp.txt
   echo `sed -i "s/$SRC/$DST/" $f` > tmp.txt
   # Do the copy
   cp -f $f `cat tmp.txt`
done

You can simply use rsync . 您可以简单地使用rsync This answer is based from this thread . 这个答案来自这个主题

rsync -av --include='*.txt' --include='*/' --exclude='*' dir1/ dir2/

If you only have .txt files in dir1, this would work: 如果您在dir1中只有.txt文件,这将起作用:

cp -R dir1/* dir2/

But if you have other file extensions, it will copy them too. 但是,如果您有其他文件扩展名,它也会复制它们。 In this case, this will work: 在这种情况下,这将工作:

cd /path/to/dir1
cp --parents `find . -name '*.txt'` path/to/dir2/

暂无
暂无

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

相关问题 Bash:递归复制命名文件,保留文件夹结构 - Bash: Copy named files recursively, preserving folder structure 通过bash将文件从一个目录排序并复制到另一个目录 - Sort and copy files from one directory to another via bash Bash/unix,如何将所有内容(文件/子目录)从一个文件夹复制到另一个文件夹并保持文件夹结构 - Bash/unix, how to copy all contents (files/subdirs) from one folder to another and keep folder structure 如何在bash中使用相同的名称但扩展名相同的目录复制多个文件? - How can I copy multiple files in the same directory with different names but same extension in bash? 如何使用 bash 将多个版本号不同的文件从一个目录复制到另一个目录? - How to copy multiple files with varying version numbers from one directory to another using bash? BASH:将所有文件和目录复制到同一父目录中的另一个目录中 - BASH: Copy all files and directories into another directory in the same parent directory 将名称错误的文件从一个深层嵌套的目录、内容地址重命名,复制到 Bash one-liner 中的另一个扁平子目录? - Copy badly-named files from one deeply-nested dir, content-address rename them, to another flattened sub-directory, in a Bash one-liner? 如何递归复制文件,重命名它们但在 Bash 中保持相同的扩展名? - How to copy files recursively, rename them but keep the same extension in Bash? 如何使用bash脚本将S3中的文件从文件夹递归复制到另一个文件夹? - How to copy files in S3 from folder to another recursively using bash script? 在Bash中将目录结构从一个目录复制到另一个目录 - Copying directory structure from one directory to another in Bash
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM