将文件从一个特定子目录复制到其他特定子目录

时间:2017-10-02 22:16:36

标签: bash find xargs cp

我无法找到这个问题的好答案,所以请耐心等待,因为这是一个非常简单的问题。

我在特定目录中有文件,需要在指定时间使用cron将它们复制到其他特定目录中。像这样:

/var/www/html/files/client1/1.txt
/var/www/html/files/client2/1.txt
/var/www.html/files/client3/1.txt 
... and so on for many directories

每个文件" 1.txt"特定于客户端,因此必须专门复制到目标文件夹。

/var/www/html/desintation/client1/1.txt
/var/www/html/desintation/client2/1.txt
/var/www/html/desintation/client3/1.txt
...and so on...

如果你只是想把我指向一个可以开始的地方,那对我来说很酷。但我的问题基本上是,如何确保client1文件始终在client1目标中?因为在我看来喜欢通配符,我无法解释这个问题,并且文件有可能最终出现在错误的目录中。

1 个答案:

答案 0 :(得分:0)

假设您在/ var / www / html / files目录中,可以考虑以下内容:

find . -name 1.txt -exec cp \"{}\" \"/var/www/html/destination/{}\" \;

“find.-name 1.txt”位将找到当前目录中名为1.txt的文件的相对路径列表:

./client1/1.txt
./client2/1.txt
./client3/1.txt

“exec”部分将使用它来执行如下命令:

cp "./client1/1.txt" "/var/www/html/destination/./client1/1.txt"
cp "./client2/1.txt" "/var/www/html/destination/./client2/1.txt"
cp "./client3/1.txt" "/var/www/html/destination/./client3/1.txt"

您可以通过添加回显来进行实验,以查看实际运行它时将输出的命令:

find . -name 1.txt -exec echo cp \"{}\" \"/var/www/html/destination/{}\" \;
相关问题