Linux 用于查找早于 2010 年 1 月 1 日的文件的 Shell 脚本

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

Shell script to find files older than 1st Jan 2010

linuxshell

提问by fixxxer

I'm looking for a script that find all files older than 1st Jan 2010. The following isn't working for me -

我正在寻找一个脚本来查找早于 2010 年 1 月 1 日的所有文件。以下对我不起作用 -

date-of-my-file = $(date -r /my-file +%F)
if [ $date-of-my-file -nt "2010-01-01" ]
then
    echo "Yes"
else
    echo "No"
fi

Any help will be appreciated. P.S: touch command is not working on my box with deprecated Linux.

任何帮助将不胜感激。PS:touch 命令在我的机器上不适用于已弃用的 Linux。

采纳答案by davidp

Here's a one-liner:

这是一个单行:

find <dir> -not -newermt 2010-01-01

This finds all files modified before the specified date. Do a man findand look at the options for -newerXYif you want to use something besides modified date.

这将查找在指定日期之前修改的所有文件。如果您想使用修改日期之外的其他内容,请执行man find并查看-newerXY的选项。

回答by Brad

Just use the "find" command. There are several flags that allow you to filter file by last modification time, such as -newer, -mmin and -mtime. Do a "man find" for more details.

只需使用“查找”命令。有几个标志允许您按上次修改时间过滤文件,例如 -newer、-mmin 和 -mtime。执行“man find”以获取更多详细信息。

回答by RusAlex

you need to use shell commands find with newer option.

您需要使用带有较新选项的 shell 命令 find 。

it works like: you create a file with timestamp 1.1.2010 and then compare all files with this timestamp

它的工作原理如下:您创建一个时间戳为 1.1.2010 的文件,然后将所有文件与此时间戳进行比较

touch -t 01010000 /tmp/timestamp<br>
find ~ -newer /tmp/timestamp