我有一个关于Linux和shell 脚本的问题,我知道它不能从创建日期获取文件,只能从修改日期获取文件,但是如果我在文件名上获得创建日期呢?
例如,现在有一个文件夹,其中包含一些文件:
ABCADD01-20230801
ABCADD01-20230802
ABCADD01-20230803
ABCADD01-20230804
ABCADD02-20230801
ABCADD02-20230802
ABCAEE01-20230801
ABCAEE01-20230802
ABCAEE01-20230803
ABCAFF01-20230801
ABCAGG01-20230802
我需要找到文件名是"ABCADD
"和创建日期是"2天前或更早",并压缩这些文件.今天是20230804
我有个主意是用两个for循环:
第一个循环查找包含"ABCADD
"的文件名,第二个循环查找创建日期为两天前的文件.
# Get the date for two days ago
two_days_ago=$(date -d "2 days ago" +%Y%m%d)
# Find the "ABCADD" files that were last modified two days ago, zip them
for file in $(find . -name 'ABCADD*' -type f ); do
for file in $(find . -name '*$two_days_ago' -type f ); do
echo "Zipping $file"
gzip "$file"
done
done
预期的最终结果应该是
ABCADD01-20230801
ABCADD01-20230802
ABCADD02-20230801
ABCADD02-20230802
但是,我不能得到"两天前或更早",只能得到具体的一天,这是20230802
,仍然有一些错误.有更好的办法吗?