如何在Linux中将文件复制到多个目录?


您是否需要在Linux或Unix环境下将一个文件复制到多个不同的目录?如果是,本文将指导您如何在Linux/Ubuntu中将文件复制到多个目录。

使用cpxargs

要在Linux/Ubuntu中将文件复制到多个目录,请使用以下命令:

$ echo dir1 dir2 dir3 | xargs -n 1 cp file1

在上面的命令中,我们将file1复制到dir1、dir2和dir3目录。该命令的示例如下:

$ echo Music Videos Desktop | xargs -n 1 cp httpstat.py

在上面的命令中,我们将httpstat.py文件复制到Music、Videos和Desktop目录。要验证上述命令,请使用以下命令:

$ cd Music
~/Music$ ls

示例输出应如下所示:

httpstat.py

另一个验证上述命令的示例如下所示:

$ cd Desktop
~/Desktop$ ls

示例输出应如下所示:

httpstat.py

使用cpfind

要在Linux/Ubuntu中将文件复制到多个目录,请使用以下命令:

find dir1 dir2 dir3 -type d -exec cp file1 {} \;

上述命令查找dir1、dir2和dir3目录,并将file1复制到这些目录。示例如下:

$ find Music Desktop -type d -exec cp httpstat.py {} \;

在上面的命令中,我们将httpstat.py文件复制到Music和Desktop目录。要验证上述命令,请使用以下命令:

$ cd Desktop
~/Desktop$ ls

示例输出应如下所示:

httpstat.py

另一个验证上述命令的示例如下所示:

$ cd Music
~/Music$ ls

示例输出应如下所示:

httpstat.py

在本文中,我们学习了如何在Linux中将文件复制到多个目录。在我们的后续文章中,我们将分享更多基于Linux的技巧和提示。敬请关注!

更新于:2020年1月28日

566 次浏览

启动你的职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.