简体   繁体   English

使用 ls 命令列出超过 X 分钟的文件

[英]Listing files older than X minutes using it with ls command

if [[ $line == *SECURITYRPT*  &&  (((**If the file is older than 5minutes**))) ]]
then
     echo "$line"
else
     echo "file present not older than 2minutes"
fi

What condition to put after && to list files older than 5 minutes?&&之后放置什么条件来列出超过 5 分钟的文件?

You can use the find command with the -mmin parameter:您可以使用带有-mmin参数的find命令:

This example finds all files in the current directory (and subdirectories) that are aged less than 5 minutes.此示例查找当前目录(和子目录)中所有时间少于 5 分钟的文件。 You can remove the "-" to match your needs:)您可以删除“-”以满足您的需要:)

find ./ -type f -mmin -5;

manual page for find find手册页

Create a reference file with date of 5 minutes ago with touch .使用touch创建一个日期为 5 分钟前的参考文件。

Then test the file is -o lder t han your reference file.然后测试该文件t -o您的参考文件更旧。

ageref="$(mktemp)"
trap 'rm -f -- "$ageref"' EXIT

if touch -d '-5mn' -- "$ageref" && [[ "$line" == SECURITYRPT && "$file" -ot "$ageref" ]]; then
 : # Do watever
fi

See help test | grep -F -- '-ot'查看help test | grep -F -- '-ot' help test | grep -F -- '-ot' : help test | grep -F -- '-ot'

FILE1 -ot FILE2 True if file1 is older than file2. FILE1 -ot FILE2如果 file1 早于 file2,则为真。

Assuming "older than 5 minutes" means "modified 5 (or more) minutes ago, in Linux using bash:假设“超过 5 分钟”意味着“在 5(或更多)分钟前修改,在 Linux 中使用 bash:

if (($(date +%s) - $(stat -c %Y "$file") > 300)); then
    echo "$file is older that five minutes"
fi

The condition is (($(date +%s) - $(stat -c %Y "$file") > 300)) and that expression can be used in compound logical expressions.条件是(($(date +%s) - $(stat -c %Y "$file") > 300))并且该表达式可用于复合逻辑表达式。

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

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