Linux regularly deletes files older than 30 days


You can use the find command to
find path -mtime + days -type f -name "filename" -exec rm -rf {} \;

 

find /tmp -mtime +30 -type f -name "*" -exec rm -rf {} \;
/tmp -- set the search directory;
-mtime +30 -- set the modification time to 30 days ago;
-type f --Set the search type to file;
-name "*" --Set the file name, wildcards can be used;
-exec rm -rf --Delete operation after search;
 {} \; --fixed writing

 

The other method is similar:
#find /tmp -mtime +30 -type f | xargs rm -rf

 

Can you write this command into a script,
cleandata.sh
find /tmp -mtime +30 -type f -name "*" -exec rm -rf {} \;

 

Configure executable
chmod u+x ./cleandata.sh

 

Configure to crontab
crontab -e
0 0 * * * /home/username/cleandata.sh > /dev/null 2>&1

Automatic execution at 0:00 every day

The first * indicates the minute in the time. Value range: 0-59
The second * indicates the hour in the time. Value range: 0-23
The third * indicates the day of the month. Range: 1-31
The fourth * indicates the month of the year, range of values: 1-12
The fifth * indicates the day of the week, starting from Sunday, the value is 0 ~7, 0, 7 all mean Sunday

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326539434&siteId=291194637