Copy and Delete Question

I have a script to tar up some older files. It did a find and mv, but I had to change it to preserve the directory structure because some of the directories now have duplicate nameed files. I changed it to a find and copy, then added a find and remove.

Is there a better way so I don't have to do 2 finds? I tried to do the tar in the find, but it ignored the directory structure and overlaid the duplicate named files.

/opt/applic/AAA/logs/ and /opt/applic/BBB/logs/ each have a file called output.log

TARGETDIR=/opt/applic//logs/
# WORKDIR/REF is a reference file dated to be the oldest file we will keep
find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\
! -newer $WORKDIR/$REF -exec cp -p --parents {} $WORKDIR \;
find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\* ! -newer $WORKDIR/$REF -exec rm -f {} \;
tar cvf ARCH/logs_xyz.tar $WORKDIR

You can have multiple -exec commands in the same find. Or you could -exec a simple script which copies and removes only if the copy was successful.

find $TARGETDIR/ -type f -maxdepth 1 -iname \*.log\* ! -newer $WORKDIR/$REF \
  -exec sh -c 'cp -p --parents {} $WORKDIR && rm -f {}' \;

That worked after I removed the \ after $REF.

I had tried to do multiple -exec but could not get the code to recognize it correctly. Your solution worked great!

Thanks.