如何将文件复制到多个子目录linux

时间:2017-03-13 10:43:03

标签: linux copy

我有一个文件需要复制唯一的目录调用测试 目录结构如下

/contentroot/path/a/x/test
/contentroot/path/a/y/test
/contentroot/path/a/z/test
--------------------------

如上所述我有超过250个组合测试目录

我已尝试下面的命令(使用星号),但它只是直接复制一个测试并给出问题(cp:省略目录)

cp myfile.txt /contentroot/path/a/*/test

任何帮助

2 个答案:

答案 0 :(得分:1)

也许是for循环?

for FOLDER in /contentroot/path/a/*/test; do
    cp myfile.txt $FOLDER
done

答案 1 :(得分:0)

您可以将echo的输出作为输入提供给xargs。然后xargs将运行三次cp命令,并附加从echo传送给它的下一个目录路径。

xargs命令的-n 1选项是这样的,每次运行时它只会一次将其中一个参数附加到cp。

echo /contentroot/path/a/x/test /contentroot/path/a/y/test /contentroot/path/a/z/test | xargs -n 1 cp myfile.txt
  

警告!首先,这将覆盖文件(如果存在),并且应该测试任何bash命令并在跑步者风险中使用! ;)

相关问题