我在目录中有一组文件。我需要查找一组特定日期的文件(例如,如果我需要1月16日至1月20日的文件)。我尝试使用ls -ltr | grep <date>
,但是要完成选择文件需要太多步骤。有没有更简单的方法来完成这项工作。谢谢!
答案 0 :(得分:20)
我不确定你的目录中有多少个文件,但是这样的事情应该非常快:
ls -al | awk '$6 == "Jan" && $7 >= 16 && $7 <= 20 {print $9}'
在我的系统上,我看到以下内容略微修改了日期:
pax> ls -al | awk '$6 == "Jan" && $7 >= 16 && $7 <= 29 {print $9}'
kids_shares.ods
our_savings.gnumeric
photos
pax> ls -ald kids_shares.ods our_savings.gnumeric photos
-rw-r--r-- 1 pax pax 51005 Jan 29 19:39 kids_shares.ods
-rw-r--r-- 1 pax pax 2275 Jan 28 14:48 our_savings.gnumeric
drwxrwxrwx 130 pax pax 4096 Jan 29 21:47 photos
您可以看到日期与给定文件匹配。
需要注意的一件事:如果文件是最近的,它将在第8列中有时间。超过某个年龄,ls
开始将年份放在那里。我有一个模糊的回忆,它是在六个月大的边界附近,但我不是绝对肯定的。你也需要照顾它。
答案 1 :(得分:11)
使用find(1)
代替,这将更容易。
find . -mtime -$start -mtime +$end
答案 2 :(得分:7)
最好使用find
。首先创建两个具有特定时间的临时文件以进行搜索。 touch
可以执行此操作:
touch -t "201301160001" ./start
touch -t "201301202359" ./finish
然后运行find
:
find /path/to/files -type f -newer ./start ! -newer ./finish