Linux命令cp产生省略的目录

4
我想在网站的100个不同目录中放置一个名为“test1.html”的文件。
目录结构如下: /home/domain.com/public_html/(每个目录的domain.com名称都有所更改,因此我使用*。) 文件在这里:/root/test1.html
我已尝试通过root帐户使用命令cp test1.html /home/*/public_html/,但是提示错误信息。
cp: omitting directory `/home/domain1.com/public_html/'
cp: omitting directory `/home/domain2.com/public_html/'

等等。

如何将一个文件放置在所有域目录中?

这是Centos 5.9操作系统。

3个回答

3

尝试:

for dest in /home/*/public_html/
do 
   cp test1.html $dest
done

由于您正在编写用户拥有的目录,因此请注意umask设置-它控制文件的权限。您可以使用cp -p保留test1.html上的确切权限。


1
谢谢您的回复,但是对我来说没有任何意义。您能否请以更简单的方式写出来? - user2986500

0

试试这个:

cd /home/; find . -name public_html -type d | xargs -I {} cp /root/test1.html {};

谢谢,但是不起作用。[root@uk ~]#cd /home/; find . -name public_html -type d | xargs -i {} cp /root/googleba3771cddb278c04.html {}; xargs:{}:没有这样的文件或目录。 - user2986500
该死,是大写字母I。已编辑回复。 - Harry

-1
应该这样写:cp -R test1.html /home/*/public_html/ -R 表示递归,如果没有它,它就不会操作所有目录。

cp 不支持多个目标目录,这就是为什么其他答案使用循环或 xargs 的原因,以便可以多次调用 cp - John Kugelman
那些其他的答案对我没用,但这个有用。 - Your Code Made Me Suicidal
听起来你正在从一个或多个目录中复制,此时 -R 是正确的答案。在这个问题中,OP 正试图复制到一堆目录中。-R 对此没有帮助。 - John Kugelman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接