BASH脚本:复制包含在不同父文件夹中的许多文件(相同名称) [英] BASH SCRIPT: copy many files (same name) contained in different father folder
问题描述
我试图将Jaypal的回答变成bash脚本(请参阅此处)通过这种方式:
I tried to turn into a bash script the Jaypal answer (see here) in this way:
#!/bin/bash
pwd
mkdir -v ../g
shopt -s globstar
for file in ./**/file.txt
do
echo "will copy $file"
cp -v --parents "$file" ../g
done
但是它不起作用!(仅创建空文件夹g)
可能是什么问题?
注意:脚本位于f文件夹中
But it doesn't work! (It only creates the empty folder g)
What could the problem be?
note: the script is in f folder
谢谢!:)
推荐答案
回答标题问题:
source_dir=/some/dir
target_dir=/some/other/dir
file_name=myfile.txt
cd $target_dir
while read path; do
mkdir -p $(dirname $path)
cp $source_dir/$path $target_dir/$path
done < <(cd $source_dir; find . -name $file_name -type f)
这会将在 $ source_dir
下找到的所有 $ file_name
复制到 $ tagert_dir
this duplicates all $file_name
found under $source_dir
into $tagert_dir
关于第二个问题(为什么您的代码不起作用)也许您没有bash 4+,所以globstar无法正常工作?也许它返回的文件的相对路径有所不同(我以为是,但是由于bash 3.2而无法检查)
Regarding your second question (why your code doesn't work) perhaps you don't have bash 4+ and so globstar is not working? Or maybe it returns something different that the relative path to the file (I thought, but I can't check since I have bash 3.2)
这篇关于BASH脚本:复制包含在不同父文件夹中的许多文件(相同名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!