I want to archive the files above the mentioned date. I need to achieve two things,
I have sample files like this
ABCSMOKLO2Y_pm24hr_20180410.000006_4.csv
11randomletters_6randomalphanumeric_YYYYMMDD.randomnumbers_randomnum.csv
I want to tar files based on the date in the file name. Example, if I mention 10th April. Each and every date file should be tar on the specific date like this.
20180410.tar.gz
20180409.tar.gz
20180408.tar.gz
and if there is no file available on the specific date. I don't want to create a tar file for the date which has no file.
function archive_old_files(){
d=$(date -d"$DAYS_TO_ARCHIVE days ago" +%s)
echo "$d"
for i in {1..30}; do
dt="$(date -d@$((d - i * 86400)) +%Y%m%d)"
fn="$dt.tar.gz"
if [ "$dt" -lt "$d" ] || [ -f *"$dt"* ]; then
tar -czf "$DEST_ARCHIVE"/$fn "$SRC_DIR_ARCHIVE"/*$dt*
fi
done
}
Summary :
- I need to specify the directory in the for loop. How can I achieve
- Tar all csv files above the mentioned date with file check(files available on specific date or not).
If there is no file available on any date. My script creates empty date.tar.gz
.