I'm using centOS8 and I'm writing a batch script to delete data up to the previous day.
The structure of the folder that needs to be deleted is as follows.
root/data/year/month/day/uuid/time
For example:
root
└ data
└ ImportantFolder
└ 2020
└ 2021
└ 11
└ 12
└ 1
└ 2
└ 550e8400-e29b-41d4-a716-446655440000
└ 2243010332.d
The script runs every day at 2:00 AM and should only delete data up to the previous day.
For example, if today is January 1, 2022, folders up to December 31, 2021 should be removed.
It would be simple to remove only the files created more than a day ago in the data folder, but data that does not follow the year/month/day/.. structure in the data folder(like ImportantFolder above) should not be deleted, and only folder created after midnight should be kept. (The system works 24/7)
So, when the script is executed, I am thinking whether it is possible to get yesterday's date, decompose day, month, and year, and then delete it through a conditional statement. I'm new to shellscript so I don't know if this is possible. Can you help me with a better idea or how I can get and disassemble the previous day with a script?
You can try something like this:
find /root/data -type d -not -name "ImportantFolder" -mtime +1 -exec rm -rf {} \;
So, searching only directories, excluding the "ImportantFolder" (Not tested).
Here are some guidelines for you:
current_year=$(date +%Y)
will give you the current yearfor current_dir in /data/*/; do
for current_dir in /data/*/; do
... current_dir=$(basename "$current_dir")
if [ "$current_dir" -lt "$current_year" ]; then
if [ "$current_dir" -lt "$current_year" ]; then
... (remove it - or do some logging to start with to make sure you're on track) for current_dir2 in /data/"$current_dir"/*/; do
for current_dir2 in /data/"$current_dir"/*/; do
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.