linux中实现将一个文件同时复制到多个目录中

 

001、echo + xargs + cp实现

[root@pc1 data]# ls      ## 测试目录及文件a.txt为测试文件, test01~04为测试目录,均为空目录
a.txt  test01  test02  test03  test04
[root@pc1 data]# tree     ## 利用tree命令查看文件结构
.
├── a.txt
├── test01
├── test02
├── test03
└── test04

4 directories, 1 file
[root@pc1 data]# echo ./test01 ./test03 | xargs -n 1 cp -v a.txt  ## 使用echo + xargs + cp实现复制到多个目录
‘a.txt’ -> ‘./test01/a.txt’
‘a.txt’ -> ‘./test03/a.txt’
[root@pc1 data]# tree    ## 利用tree命令查看复制效果
.
├── a.txt
├── test01
│   └── a.txt
├── test02
├── test03
│   └── a.txt
└── test04

4 directories, 3 files

 

002、利用循环结构实现

[root@pc1 data]# ls 
a.txt  test01  test02  test03  test04
[root@pc1 data]# tree        ## 复制前文件结构
.
├── a.txt
├── test01
├── test02
├── test03
└── test04

4 directories, 1 file
[root@pc1 data]# for i in ./test02 ./test03; do cp a.txt $i; done  ## 利用for循环实现复制多个目录
[root@pc1 data]# tree             ## 查看复制效果
.
├── a.txt
├── test01
├── test02
│   └── a.txt
├── test03
│   └── a.txt
└── test04

4 directories, 3 files

 

 003、利用数组循环结构

[root@pc1 data]# ls
a.txt  test01  test02  test03  test04
[root@pc1 data]# tree      ## 查看复制前文件结构
.
├── a.txt
├── test01
├── test02
├── test03
└── test04

4 directories, 1 file
[root@pc1 data]# dirs=("./test01" "./test04")     ## 将目的目录保存在数组中
[root@pc1 data]# for i in ${dirs[@]}; do cp a.txt $i; done  ## 利用for循环结构实现复制
[root@pc1 data]# tree       ## 查看复制效果
.
├── a.txt
├── test01
│   └── a.txt
├── test02
├── test03
└── test04
    └── a.txt

4 directories, 3 files

 

 

参考:

001、https://www.yisu.com/zixun/670252.html

002、https://blog.csdn.net/weixin_39704727/article/details/116839392

 

posted @ 2022-10-15 19:41  小鲨鱼2018  阅读(1217)  评论(0编辑  收藏  举报