shell对比文件夹(文件)创建时间

  1. 判断一个文件夹中的所有文件是否是最近5分钟内创建的
#!/bin/bash

folder_path="path/to/folder"
current_time=$(date +%s)
five_minutes_ago=$((current_time - 300))  # 300秒即5分钟

if [ -d "$folder_path" ]; then
    for file in "$folder_path"/*; do
        if [ -f "$file" ]; then
            file_creation_time=$(stat -c %Y "$file")  # 获取文件的创建时间,以秒为单位

            if [ "$file_creation_time" -ge "$five_minutes_ago" ]; then
                echo "$file 是最近5分钟内创建的。"
            else
                echo "$file 不是最近5分钟内创建的。"
            fi
        fi
    done
else
    echo "文件夹不存在。"
fi
  1. 判断一个文件是否是最近5分钟内创建的
#!/bin/bash

file_path="path/to/xxx.jar"
current_time=$(date +%s)
five_minutes_ago=$((current_time - 300))  # 300秒即5分钟

if [ -f "$file_path" ]; then
    file_creation_time=$(stat -c %Y "$file_path")  # 获取文件的创建时间,以秒为单位

    if [ "$file_creation_time" -ge "$five_minutes_ago" ]; then
        echo "文件是最近5分钟内创建的。"
    else
        echo "文件不是最近5分钟内创建的。"
    fi
else
    echo "文件不存在。"
fi

  1. 检查指定的文件夹是否在过去的5分钟内生成
#!/bin/bash

# 指定要检查的文件夹路径
target_folder="/path/to/your/folder"

# 获取文件夹的创建时间
folder_creation_time=$(stat -c %Y "$target_folder")

# 获取当前时间
current_time=$(date +%s)

# 计算时间差(以秒为单位)
time_difference=$((current_time - folder_creation_time))

# 300秒(5分钟的秒数)
five_minutes_in_seconds=300

if [ "$time_difference" -lt "$five_minutes_in_seconds" ]; then
  echo "文件夹在过去的5分钟内生成。"
else
  echo "文件夹不在过去的5分钟内生成。"
fi

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值