简体   繁体   中英

Looking for a way to copy missing files from one directory to another

I have two directory:

  1. /A which contains thousands of data files
  2. /A_BKP which has to contains backup of /A directory

I am looking for a way to copy the files which have not been copied as a part of backup from /A to /A_BKP .

A small dataset from /A directory -

-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

But the /A_BKP has already

-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

So want to just copy only those which are missing.

Use the --no-clobber (or -n ) option:

cp -t /A_BKP --no-clobber /A/*

If you get an "argument list too long" error, you might have to do

find /A -maxdepth 1 -mindepth 1 -type f -print 0 \
| xargs -0 cp -n -t /A_BKP

-n prevents copying if the destination file exists. As @Jetchisel comments, use -u to update the destination file if the source file is newer.

Iterate over files, check if exists. || is for test returning false , then it will copy.

for file in /A/*.DAT; test -f /A_BKP/`basename $file` || cp $file /A/BKP/; done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM