Score:0

Find files modified between 2 dates from Source directory(Recursively) and copy it into the Target directory with same directory structure

ma flag

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.

hr flag
You are piping null-delimited data into the loop, but afaik ksh's `read` expects newline-delimited data by default
Amala avatar
ma flag
@Steeldriver - Even after removing the \0 from the find command no changes. The files are not copied to TARGET.
hr flag
@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"
Amala avatar
ma flag
@steeldriver - Thank you so much. Replacing \0 with \n worked.
Score:0
cn flag

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.

Amala avatar
ma flag
rsync is not available in our application server(Linux Server).
vanadium avatar
cn flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.