bash 如何使用 ls 在 Unix 中查找特定日期集的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14702783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:18:24  来源:igfitidea点击:

How to use ls to find files for a specific set of dates in Unix

bashunixcommand

提问by Shash

I have a set of files in a directory. I need to find files for a set of specific dates (For example if i need files from 16th Jan to 20th Jan). I tried using ls -ltr | grep <date>but it is taking too many steps to accomplish selecting the files. Is there any easier way to get this done. Thanks!

我在一个目录中有一组文件。我需要查找一组特定日期的文件(例如,如果我需要从 1 月 16 日到 1 月 20 日的文件)。我尝试使用,ls -ltr | grep <date>但完成选择文件需要太多步骤。有没有更简单的方法来完成这项工作。谢谢!

回答by paxdiablo

I'm not sure how many files you have in yourdirectory but something like this should be blindingly fast:

我不确定您的目录中有多少文件,但这样的事情应该非常快:

ls -al | awk ' == "Jan" &&  >= 16 &&  <= 20 {print }'

On my system, I see the following with dates slightly modified:

在我的系统上,我看到以下日期略有修改:

pax> ls -al | awk ' == "Jan" &&  >= 16 &&  <= 29 {print }'
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

You can see that the dates match for the given files.

您可以看到给定文件的日期匹配。

One thing to watch out for: if the file is recent, it will have a time in column 8. Beyond some age, lsstarts putting the year in there. I have the vague recollection that it's somewhere around the six-month-old border but I'm not absolutely certain. You'll need to cater for that as well.

需要注意的一件事:如果文件是最近的,它会在第 8 列中显示时间。超过某个年龄,ls开始将年份放在那里。我模糊地记得它在六个月大的边界附近的某个地方,但我并不确定。你也需要照顾到这一点。

回答by wich

Use find(1)instead, that will be much easier.

使用find(1)替代,这将容易得多。

find . -mtime -$start -mtime +$end

回答by Steve

You're better off using find. Start by creating two temp files with specific times to search between. touchcan do this:

你最好使用find. 首先创建两个具有特定时间搜索的临时文件。touch可以这样做:

touch -t "201301160001" ./start
touch -t "201301202359" ./finish

Then run find:

然后运行find

find /path/to/files -type f -newer ./start ! -newer ./finish