How to delete files from seven days ago using a shell s…

You can use the following shell script to delete files older than seven days:

#!/bin/bash

# 指定要删除的目录
directory="/path/to/directory"

# 计算七天前的时间戳
seven_days_ago=$(date -d "-7 days" +%s)

# 遍历目录中的文件
for file in "$directory"/*
do
    # 获取文件的修改时间
    modify_time=$(stat -c %Y "$file")

    # 比较修改时间和七天前的时间戳
    if [ "$modify_time" -lt "$seven_days_ago" ]
    then
        # 删除文件
        rm "$file"
        echo "已删除文件: $file"
    fi
done

Please replace the /path/to/directory in the script with the path to the directory containing the files to be deleted. Save the script to a file (e.g. delete_old_files.sh) and set the file permissions to be executable.

When running the script, it will go through all files in the specified directory and compare the modification time of each file with a timestamp from seven days ago. If the modification time is earlier than the timestamp from seven days ago, the file will be deleted and the filename will be output on the console.

bannerAds