0

I am trying to copy the files from source to Target folder which has been modified between 2 dates,

#!/usr/bin/ksh

source=/home/Amal/DELTA/SOURCE

target=/home/Amal/DELTA/TARGET

cd $source

find ./ -type f -newermt "2021-07-08 00:00:00" ! -newermt "2021-09-18 23:59:55" -printf "%h:%p:\0" |

while IFS=":" read path file; do

cp "$file" "$target"/"$path"

done

NOTE: Source and directory is having same directory structure already. I just need to copy the files as same as SOURCE directory.

When i execute this script no changes in TARGET folder.

N0rbert
  • 97,162
  • 34
  • 239
  • 423
Amala
  • 1
  • 1
  • You are piping null-delimited data into the loop, but afaik ksh's `read` expects newline-delimited data by default – steeldriver Sep 22 '21 at 10:02
  • @Steeldriver - Even after removing the \0 from the find command no changes. The files are not copied to TARGET. – Amala Sep 22 '21 at 11:24
  • @Amala you would need to **replace** `\0` with `\n` in the `printf` - or switch to bash where `read -d ''` can be used to read null-delimited "lines" – steeldriver Sep 22 '21 at 12:41
  • @steeldriver - Thank you so much. Replacing \0 with \n worked. – Amala Sep 22 '21 at 13:09

1 Answers1

0

One way could be to use rsync with the --include-from-file option. rsync is a versatile file copy utility that will by default copy directory structures. With the --include-from-file option, you can point rsync to a list, a text file, that determines the files that should be included in the copy. Such list can be created with the find command.

So first run the find to locate the desired files and list them in a text file. Then use rsync to only copy these files over. Easy to script once you got it working, obviously.

vanadium
  • 82,909
  • 6
  • 116
  • 186
  • rsync is not available in our application server(Linux Server). – Amala Sep 22 '21 at 11:19
  • That is quite important information that you may want to add to your question. Use "edit". I will leave the answer up because a vast majority of users will be able to install and use rsync. – vanadium Sep 22 '21 at 12:37