Since I've had similar requirements and questions in the past I wanted to share my solution approach here.
For testing here, I've created a data structure with test/<nameFolderByDate>
. If listing the file statistics via stat
, some meta data including access, modification and change time is given.
stat -c "%y %s %n" test/*
2020-12-03 14:11:38.788388456 +0100 4096 test/20200101
2020-12-03 14:11:41.288389539 +0100 4096 test/20200201
2020-12-03 14:11:42.984390273 +0100 4096 test/20200301
2020-12-03 14:09:14.747326011 +0100 4096 test/20200401
As one can see the timestamps might (sometimes) not fit to the structure <nameFolderByDate>
.
Therefore I've created a small script which sets the modification time (mtime
) to the date within the name. I use this approach under other circumstances too.
cat cleanupData.sh
#!/bin/bash
echo ""
echo "Set modification time to date in name"
for FILENAME in test/*; do
echo ${FILENAME}
NAMETIME=$(echo ${FILENAME} | cut -d "/" -f 2)
echo ${NAMETIME}
TIMESTAMP=$(date --date="${NAMETIME}" "+%Y%m%d%H%M")
echo ${TIMESTAMP}
touch -m -a -t ${TIMESTAMP} ${FILENAME}
done
exit
After normalizing the data structure the result with stat
is
./cleanupData.sh
Set modification time to date in name
test/20200101
20200101
202001010000
test/20200201
20200201
202002010000
test/20200301
20200301
202003010000
test/20200401
20200401
202004010000
stat -c "%y %s %n" test/*
2020-01-01 00:00:00.000000000 +0100 4096 test/20200101
2020-02-01 00:00:00.000000000 +0100 4096 test/20200201
2020-03-01 00:00:00.000000000 +0100 4096 test/20200301
2020-04-01 00:00:00.000000000 +0200 4096 test/20200401
as expected in this case and find
can be used as required, i.e.
find test -mtime +300
test/20200101
test/20200201
I will leave other parts of creating a fully functional script to the audience.
Thanks also to