UNIX / BASH:列出特定月份修改的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5289420/
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-17 23:36:40  来源:igfitidea点击:

UNIX / BASH: Listing files modified in specific month

bashlistfileunixtimestamp

提问by eold

I am struggling with listing files modified in specific month (for example, February). Here are several unsuccessful attempts:

我正在努力处理在特定月份(例如二月)修改的列表文件。以下是几次不成功的尝试:

1) 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 in find, like this:

1)我尝试创建临时文件并将它们的时间戳设置为下个月的第一次和目标月份的第一次,并在查找中使用 -newer,如下所示:

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 extra 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.

这列出了在时间间隔 ($from, $to] 中修改的文件,但我想要时间间隔 [$from, $to) (否则,在下个月的第一秒创建的文件会出现误报) . 列出二月份修改的文件是额外的问题,因为这需要将时间戳设置为二月份最大的时间戳,但二月份的天数取决于是否为闰年,这需要额外检查.

2) If I use lsI see a lot of complication when parsing, because of the possibility that user name or group contain whitespace.

2)如果我使用ls我在解析时看到很多复杂性,因为用户名或组可能包含空格。

Is there an easy way and relatively portable way for doing this (so it works for any month, regardless of file names, etc.)?

是否有一种简单且相对便携的方法来执行此操作(因此它可以在任何月份使用,而不管文件名等如何)?

回答by user123444555621

dateallowsyou to easily generate timestamps for purposes like that:

date允许您轻松地为以下目的生成时间戳:

date -d "01-Mar-2011 -1 sec" # last second of Feb-2011

Fortunately, the same syntax is possible in find:

幸运的是,相同的语法可以用于find

month="Mar-2010"
find . -newermt "01-$month -1 sec" -and -not -newermt "01-$month +1 month -1 sec"

will find all files modified in March 2010

将找到 2010 年 3 月修改的所有文件

回答by Mike Sherrill 'Cat Recall'

Well, I can create files that have the minimum timestamp and the maximum timestamp in February, andfiles that are just beyond February in each direction.

好吧,我可以创建在二月具有最小时间戳和最大时间戳的文件,以及在每个方向上刚好超过二月的文件。

$ touch -t 201102010000.01 from
$ touch -t 201102282359.59 to
$ touch -t 201103010000.01 march
$ touch -t 201101312359.59 january

$ ls -l
total 0
-rw-r--r-- 1 mike None 0 Feb  1 00:00 from
-rw-r--r-- 1 mike None 0 Jan 31 23:59 january
-rw-r--r-- 1 mike None 0 Mar  1 00:00 march
-rw-r--r-- 1 mike None 0 Feb 28 23:59 to

Then using GNU 'find' like this seems to show just the files whose timestamp is in February.

然后像这样使用 GNU 'find' 似乎只显示时间戳在二月的文件。

$ find -newermt '2011-02-01' ! -newermt '2011-03-01' -print
./from
./to

I don't know how portable these arguments are to other versions of 'find'.

我不知道这些参数对于其他版本的“查找”的可移植性如何。

回答by Jonathan Müller

Adding to Pumbaa80's answer:

添加到 Pumbaa80 的答案:

In my pre-production environment, find does not support -newermt.

在我的预生产环境中,find 不支持 -newermt。

What I did instead was:

我所做的是:

  1. Get a list of all possible files (via find, lsetc.)
  2. Generate the timestamps of the last second of last month and this month

    LAST_MONTH=$(date -d "01-Jun-2015" -1 sec +%s)
    THIS_MONTH=$(date -d "31-Jul-2015" +%s)
    
  3. Iterate over the list from point 1 and compare the timestamp of each file with the timestamps from point 2

    for file in $LIST_OF_FILES
    do
        TIMESTAMP=$(stat -c"%Y" $file)
        if (( $LAST_MONTH < $TIMESTAMP ))
        then
            if (( $TIMESTAMP < $THIS_MONTH ))
            then
            echo "Your code here"
            fi
        fi
    done
    
  1. 获取的所有可能的文件列表(通过findls等等)
  2. 生成上月和本月最后一秒的时间戳

    LAST_MONTH=$(date -d "01-Jun-2015" -1 sec +%s)
    THIS_MONTH=$(date -d "31-Jul-2015" +%s)
    
  3. 从点 1 迭代列表并将每个文件的时间戳与点 2 的时间戳进行比较

    for file in $LIST_OF_FILES
    do
        TIMESTAMP=$(stat -c"%Y" $file)
        if (( $LAST_MONTH < $TIMESTAMP ))
        then
            if (( $TIMESTAMP < $THIS_MONTH ))
            then
            echo "Your code here"
            fi
        fi
    done
    

回答by mvds

A workaround could be to use the -printfoption to find:

解决方法可能是使用以下-printf选项find

find -printf "%Cm %p\n"| egrep ^02 |cut -b4-

I don't think findcan filter the -printfresult itself, nor can it filter on date elements.

我认为find不能过滤-printf结果本身,也不能过滤日期元素。

editor if you really want the ls-like output:

编辑或者如果你真的想要ls-like 输出:

find -printf "%Cm " -ls | egrep ^02 |cut -b4-