Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
centos7服务器里面有一个/user/local/a/文件夹,现在帮我写一个定时shell脚本,保留最近两天的文件,其他全部删除。定时频率为每天凌晨1点。
Private research annotation
Sign in to add a private research label or note.
- User
centos7服务器里面有一个/user/local/a/文件夹,现在帮我写一个定时shell脚本,保留最近两天的文件,其他全部删除。定时频率为每天凌晨1点。
- Assistant
您可以使用以下代码创建定时脚本:
bash#!/bin/bash # Define the folder to clean folder="/user/local/a/" # Define the number of days to keep files days_to_keep=2 # Get the current date in seconds current_date=$(date +%s) # Calculate the date in seconds for the number of days to keep keep_date=$((current_date - (days_to_keep * 24 * 60 * 60))) # Find all files in the folder that are older than the keep date files_to_delete=$(find $folder -type f -mtime +$days_to_keep) # Loop through each file and delete it for file in $files_to_delete; do rm -f $file done您可以将此脚本保存为
cleanup.sh并在您的crontab中设置定时任务。要打开crontab,请运行:codecrontab -e然后,在文件的末尾添加以下行:
javascript0 1 * * * /bin/bash /path/to/cleanup.sh这将在每天凌晨1点运行该脚本。