列出特定月份修改的文件
我正在努力列出在特定月份(例如二月)修改的文件。
以下是几次不成功的尝试:
- 我尝试创建临时文件并将其时间戳设置为下个月的第一次和目标月份的第一次,并在
find
,如下所示:
find -newer "$from" ! -newer "$to"
这列出了修改过的文件时间间隔 ($from
, $to
],但我想要时间间隔 [$from
, $to)(否则,下个月第一秒创建的文件将会出现误报)。列出二月份修改的文件是另一个问题,因为这需要将其中一个时间戳设置为二月份中最大的时间戳,但是二月份的天数根据是否是闰年而变化,这需要额外的检查。
- 如果我使用
ls
,我在解析时会遇到很多复杂的情况,因为用户名或组可能包含空格。
有没有一种简单且相对便携的方法来执行此操作(因此它适用于任何月份,无论文件名等如何)?
I am struggling with listing files modified in a specific month (for example, in February).
Here are several unsuccessful attempts:
- I tried creating temporary files and setting their timestamp to the first time in the next month and the first time in the target month and use
-newer
infind
, like this:
find -newer "$from" ! -newer "$to"
This lists files modified in the time interval ($from
, $to
], but I would like the time interval [$from
, $to
) (otherwise, there would be false positives on files created on the first second in the next month). Listing files modified in February is another problem, since this would require to set one of the timestamps to the greatest one still in February, but the number of days in February varies depending on whether it is a leap year or not, which requires extra checking.
- If I use
ls
, I encounter a lot of complication when parsing, because of the possibility that user names or groups contain whitespace.
Is there an easy way and relatively portable way for doing this (so it works for any month, regardless of file names, etc.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
日期
允许您可以轻松生成用于此类目的的时间戳:幸运的是,
find
中可以使用相同的语法:将查找 2010 年 3 月修改的所有文件
。请参阅
-newerXY
中的选项a href="https://www.man7.org/linux/man-pages/man1/find.1.html" rel="noreferrer">查找
的手册页 。date
allows you to easily generate timestamps for purposes like that:Fortunately, the same syntax is possible in
find
:will find all files modified in March 2010.
See the option
-newerXY
infind
's man page.好吧,我可以创建具有 2 月最小时间戳和最大时间戳的文件,以及在每个方向上刚刚超过 2 月的文件。
然后像这样使用 GNU 'find' 似乎只显示时间戳为二月的文件。
我不知道这些参数对于其他版本的“find”的可移植性如何。
Well, I can create files that have the minimum timestamp and the maximum timestamp in February, and files that are just beyond February in each direction.
Then using GNU 'find' like this seems to show just the files whose timestamp is in February.
I don't know how portable these arguments are to other versions of 'find'.
添加到 Pumbaa80 的答案:
在我的预生产环境中,find 不支持 -newermt。
我所做的是:
find
、ls
等)生成上个月最后一秒的时间戳,这月
从第 1 点开始迭代列表,并将每个文件的时间戳与第 2 点开始的时间戳进行比较
Adding to Pumbaa80's answer:
In my pre-production environment, find does not support -newermt.
What I did instead was:
find
,ls
etc.)Generate the timestamps of the last second of last month and this month
Iterate over the list from point 1 and compare the timestamp of each file with the timestamps from point 2
解决方法可能是使用
-printf
选项来find
:我不认为
find
可以过滤-printf result 本身,也不能过滤日期元素。
编辑或者如果您确实想要类似
ls
的输出:A workaround could be to use the
-printf
option tofind
:I don't think
find
can filter the-printf
result itself, nor can it filter on date elements.edit or if you really want the
ls
-like output: