![](/img/trans.png)
[英]How to copy files with a particular extension from one directory to another in linux
[英]Looking for a way to copy missing files from one directory to another
我有兩個目錄:
/A
包含數千個數據文件/A_BKP
必須包含/A
目錄的備份我正在尋找一種方法將未作為備份的一部分復制的文件從/A
復制到/A_BKP
。
/A
目錄中的一個小數據集 -
-rw-rw-r-- 1 testu testu 6434570 Jun 5 14:11 ACCOUNT_1622902306293.DAT
-rw-rw-r-- 1 testu testu 2891626 Jun 5 14:11 PRODUCT_1622902306293.DAT
-rw-rw-r-- 1 testu testu 56736348 Jun 5 14:11 EXECUTION_1622902306293.DAT
-rw-rw-r-- 1 testu testu 30209979 Jun 5 14:11 ORDER_VERSION_1622902306293.DAT
-rw-rw-r-- 1 testu testu 114 Jun 5 14:11 1622902306293.DAT
-rw-rw-r-- 1 testu testu 3905808 Jun 5 14:31 ACCOUNT_1622903506439.DAT
-rw-rw-r-- 1 testu testu 1712506 Jun 5 14:31 PRODUCT_1622903506439.DAT
-rw-rw-r-- 1 testu testu 55188313 Jun 5 14:31 EXECUTION_1622903506439.DAT
-rw-rw-r-- 1 testu testu 26857690 Jun 5 14:31 ORDER_VERSION_1622903506439.DAT
-rw-rw-r-- 1 testu testu 114 Jun 5 14:31 1622903506439.DAT
但是/A_BKP
已經
-rw-rw-r-- 1 testu testu 26857690 Jun 5 14:31 ORDER_VERSION_1622903506439.DAT
-rw-rw-r-- 1 testu testu 114 Jun 5 14:31 1622903506439.DAT
所以只想復制那些丟失的。
使用--no-clobber
(或-n
)選項:
cp -t /A_BKP --no-clobber /A/*
如果您收到“參數列表太長”錯誤,您可能必須這樣做
find /A -maxdepth 1 -mindepth 1 -type f -print 0 \
| xargs -0 cp -n -t /A_BKP
-n
如果目標文件存在,則阻止復制。 正如@Jetchisel 評論,如果源文件較新,請使用-u
更新目標文件。
遍歷文件,檢查是否存在。 ||
用於test
返回false
,然后它將復制。
for file in /A/*.DAT; test -f /A_BKP/`basename $file` || cp $file /A/BKP/; done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.