以下是一个Bash脚本示例,用于比较文件的最后修改日期。
#!/bin/bash
file1="/path/to/file1.txt"
file2="/path/to/file2.txt"
# 获取文件1的最后修改日期
file1_modified=$(stat -c %Y "$file1")
# 获取文件2的最后修改日期
file2_modified=$(stat -c %Y "$file2")
# 比较文件1和文件2的最后修改日期
if [ "$file1_modified" -gt "$file2_modified" ]; then
echo "文件1的最后修改日期较晚"
elif [ "$file1_modified" -lt "$file2_modified" ]; then
echo "文件2的最后修改日期较晚"
else
echo "文件1和文件2的最后修改日期相同"
fi
需要替换/path/to/file1.txt
和/path/to/file2.txt
为要比较的实际文件路径。脚本使用stat -c %Y
命令获取文件的最后修改日期,然后使用条件语句比较两个日期的大小。根据比较结果,脚本将输出相应的消息。
请记得在脚本文件上授予执行权限(使用chmod +x script.sh
命令),然后通过运行./script.sh
来执行脚本。