Reputation: 79
I have two directory:
/A
which contains thousands of data files/A_BKP
which has to contains backup of /A
directoryI 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.
Upvotes: 1
Views: 958
Reputation: 246807
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.
Upvotes: 3
Reputation: 3437
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
Upvotes: 1