I am trying to tar multiple files using the find command. I want to find all the files in the directory that contain a specific string in the file name and then tar those files. The string that I am want to find in the file name is the date.
For instance, I have a file name named ulog.20120914.log
What I'm doing right now is:
DAYTWOPREV=`date +%Y%m%d --date='2 days ago'`
function archive {
cd $1;
if [ ! -d archive ]; then
mkdir archive;
fi
TMPFILE=`mktemp`;
find . -maxdepth 1 -name "${DAYTWOPREV}*" -type f -print0 > $TMPFILE;
TARFILE=archive/${DAYTWOPREV}$2.tar;
if [ ! -e $TARFILE ]; then
echo tar cfT $TARFILE /dev/null;
tar cfT $TARFILE /dev/null;
fi
cat $TMPFILE | xargs -0r tar rf $TARFILE
cat $TMPFILE | xargs -0r rm -rf
rm -f $TMPFILE;
}