简体   繁体   English

Linux:列出文件名(如果在日期间隔之间最后一次修改)

[英]Linux: List file names, if last modified between a date interval

I have 2 variables, which contains dates like this: 2001.10.10 我有2个变量,其中包含这样的日期:2001.10.10

And i want to use ls with a filter, that only list files if last modified were between the first and second date 我想将ls与过滤器一起使用,它仅列出文件,如果最后一次修改是在第一和第二个日期之间

The best solution I can think of involves creating temporary files with the boundary timestamps, and then using find : 我能想到的最好的解决方案包括使用边界时间戳创建临时文件,然后使用find

touch -t YYYYMMDD0000 oldest_file
touch -t YYYYMMDD0000 newest_file
find -maxdepth 1 -newer oldest_file -and -not -newer newest_file
rm oldest_file newest_file

You can use the -print0 option to find if you want to strip off the leading ./ from all the filenames. 您可以使用-print0选项来find是否要从所有文件名中删除./

If creating temporary files isn't an option, you might consider writing a script to calculate and print the age of a file, such as described here , and then using that as a predicate. 如果无法创建临时文件,则可以考虑编写脚本来计算和打印文件的生存期( 如此处所述) ,然后将其用作谓词。

Sorry, it is not the simplest. 抱歉,这不是最简单的。 I just now developed it, only for you. 我现在才开发它,仅适合您。 :-) :-)

ls -l --full-time|awk '{s=$6;gsub(/[-\.]/,"",s);if ((s>="'"$from_variable"'") && (s<="'"$to_variable"'")) {print $0}}';

The problem is, that these simple commandline tools doesn't handle date type. 问题是,这些简单的命令行工具无法处理日期类型。 So first we convert them to integers removing the separating "-" and "." 因此,首先我们将它们转换为整数,删除分隔的“-”和“”。 characters (by you is it ".", by me a "-" so I remove both, this can you see in 字符(您是“。”,我是“-”,所以我都删除了这两个字符,您可以在

gsub(/[-\.]/,"",s)

After the removal, we can already compare them with integers. 删除后,我们已经可以将它们与整数进行比较。 In this example, we compare them with the integers $from_variable and with $to_variable. 在此示例中,我们将它们与整数$ from_variable和$ to_variable进行比较。 So, this will list files modified between $from_variable and $to_variable . 因此,这将列出$ from_variable和$ to_variable之间修改的文件。

Both of "from_variable" and "to_variable" need to be environment variables in the form 20070707 (for 7. July, 2007). “ from_variable”和“ to_variable”都必须是环境变量,格式为20070707(适用于2007年7月7日)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM