I wrote an archive script to remove files older that a certain date and all was working fine until I hit a directory that had not been used for a while. It moved the directory as well! Is there any way to move only the files and leave any empty directories?
I can't reference specific directories because occasionally a new one is added, but I can't delete an empty one because it may be written to again in the future.
Here is what I have to remove anything written before noon 2 days ago:
DoM=`date +%Y%m%d`
REF=.tmp.$$
ARCH=/opt/files/archive
TARGETDIR=/opt/files/data
WORKDIR=/opt/files/workdir
touch -t $(date -d "-2 day" +%Y%m%d)1200 $WORKDIR/$REF
ls -l $WORKDIR/$REF
if [ "$?" -ne 0 ]; then echo "touch command failed"; exit 1; fi
find $TARGETDIR/ -name \* ! -newer $WORKDIR/$REF -exec mv {} $WORKDIR \;
There was a/opt/files/data/outgoing directory that was empty and had a date older than what was being archived, so it got moved as well. Not what I wanted. I only wanted any files within the directories, not any of the directories removed.
I can't reference specific directories because occasionally a new one is added, but I can't delete an empty one because it may be written to again in the future.
Here is what I have to remove anything written before noon 2 days ago:
DoM=`date +%Y%m%d`
REF=.tmp.$$
ARCH=/opt/files/archive
TARGETDIR=/opt/files/data
WORKDIR=/opt/files/workdir
touch -t $(date -d "-2 day" +%Y%m%d)1200 $WORKDIR/$REF
ls -l $WORKDIR/$REF
if [ "$?" -ne 0 ]; then echo "touch command failed"; exit 1; fi
find $TARGETDIR/ -name \* ! -newer $WORKDIR/$REF -exec mv {} $WORKDIR \;
There was a/opt/files/data/outgoing directory that was empty and had a date older than what was being archived, so it got moved as well. Not what I wanted. I only wanted any files within the directories, not any of the directories removed.