简体   繁体   中英

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?

You can use the find command with the -mmin parameter:

This example finds all files in the current directory (and subdirectories) that are aged less than 5 minutes. You can remove the "-" to match your needs:)

find ./ -type f -mmin -5;

manual page for find

Create a reference file with date of 5 minutes ago with touch .

Then test the file is -o lder t han your reference file.

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' :

FILE1 -ot FILE2 True if file1 is older than file2.

Assuming "older than 5 minutes" means "modified 5 (or more) minutes ago, in Linux using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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