Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Archive files by date, but leave empty dir 1

Status
Not open for further replies.

prismtx

IS-IT--Management
Apr 9, 2001
59
US
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.
 
Why not just replace
Code:
find $TARGETDIR/ -name \* ! -newer $WORKDIR/$REF -exec mv {} $WORKDIR \;
with
Code:
find $TARGETDIR/ -type f -name \* ! -newer $WORKDIR/$REF -exec mv {} $WORKDIR \;
?

HTH
:)
 
You're right. I looked right past the obvious place - the find and was looking for a solution in the mv part of the command.

Thanks it worked great.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top