Todd
Todd

Reputation: 219

find -newer to detect only newly copied files

I am using the following code

if [ ! -f $time_mark ]
then
    touch $time_mark
fi

cp -f aaa.txt bbb.txt ccc.txt $file_dir
find $file_dir -newer $time_mark > file_list.txt
...

I am using find -newer to send only files are that copied later than $time_mark. But it turns out that if $time_mark does not exist for first time, it will execute touch $time_mark and start copying files, which can happen almost at the same time. This results in $time_mark and copied files having the same system time, and the whole concept of sending only files copied later than $time_mark doesn't work.

Is there some way to work around this problem?

Thanks

Upvotes: 0

Views: 459

Answers (2)

Gilbert
Gilbert

Reputation: 3776

Don't forget to use "cp -p" to preserve timestamps of the original files.

Upvotes: 1

sehe
sehe

Reputation: 393457

if [ -f $time_mark ]
then
    incremental="-newer $time_mark"
else
    incremental=""
    touch $time_mark
fi

...
find $file_dir $incremental > file_list.txt
...

Even better would seem to execute the touch conditionally (only if the prior run succeeded?)

I strongly suggest to look at rsync, rdiff-backup, or other (backup?) tools that grok incremental change detection.

As a very simple measure, since you seem to hint you want to copy these files (?) somewhere, simply copying with -pu (--preserve=mode,ownership,timestamps --update) could do the trick

Upvotes: 2

Related Questions