工作中有遇到要把乙個檔案拷貝到n個資料夾下,但是cp又沒有這樣的命令,怎麼辦,這時需要編寫乙個指令碼,首先做實驗如下:
[root@host1 ~]# mkdir test[root@host1 ~]# cd test
[root@host1 test]# mkdir -p test_123 test_abc test_xyz testlog
[root@host1 test]# cd testlog/
[root@host1 testlog]# echo "test log" > test.log
[root@host1 testlog]# cat cp.sh
#!/bin/bash
log=test.log
for dir in `find /root/test -name "test_*"`
docp $log $dir
done
[root@host1 testlog]# bash -x cp.sh
+ log=test.log
++ find /root/test -name 'test_*'
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_xyz
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_abc
+ for dir in '`find /root/test -name "test_*"`'
+ cp test.log /root/test/test_123
[root@host1 testlog]# cat /root/test/test_123/test.log
test log
使用指令碼寫的命令,是可以強制覆蓋原先有的檔案的,而沒有提示,我們驗證下:
[root@host1 testlog]# echo "test log once" > test.log[root@host1 testlog]# bash cp.sh
[root@host1 testlog]# cat /root/test/test_123/test.log
test log once
但是如果我們在命令列使用cp命令,則會提示是否需要覆蓋:
[root@host1 testlog]# cp test.log /root/test/test_123/cp:是否覆蓋"/root/test/test_123/test.log"? y
解決這個問題的方法很簡單,只需在前面加上"\":
[root@host1 testlog]# echo "test log once again" > test.log[root@host1 testlog]# \cp test.log /root/test/test_123/
[root@host1 testlog]#
[root@host1 testlog]# cat /root/test/test_123/test.log
test log once again
這樣就可以解決每次覆蓋敲y的煩惱了,是不是很棒^_^
當然還可以取消別名或者修改別名,但是比較麻煩,用完之後還要恢復回來,如果忘記修改回來,對以後的工作可能會造成麻煩哦!
lnux複製檔案到多個目錄
linux中常用cp命令進行複製。但是一次只可以複製到乙個資料夾內。今天剛好有需要將同一批檔案複製到多個資料夾內。可以用cp echo 和xargs同時使用滿足要求。一 將某個指定檔案拷貝到多個目錄 echo home dir1 home dir2 home dir3 xargs n 1 cp v ...
Linux複製檔案到多個目錄
linux中常用cp命令進行複製。但是一次只可以複製到乙個資料夾內。今天剛好有需要將同一批檔案複製到多個資料夾內。可以用cp echo 和xargs同時使用滿足要求。echo home dir1 home dir2 home dir3 xargs n 1 cp v home dir file dir...
centos複製目錄下的檔案到另一目錄下
指令名稱 cp copy 功能介紹 將乙個檔案複製至另乙個檔案,或將數個檔案複製至另一目錄。語法格式 cp options source dest cp options source.directory 常用引數說明 a 盡可能將檔案狀態 許可權等資料都照原狀予以複製。r 若 source 中含有目...