0

I have stored a couple of file names in a text file by the name new1.txt in my home directory, now i'm using a loop to list all the files in the directory they are present in. I am able to write the command to perform this while read in; do ls -lrt /newusr/home/logs/"$in"; done < new1.txt

These logs were generated yesterday, what I want is to find all the files that were generated after 2pm yesterday, I tried inserting a grep statement but i think i wrote it wrong
while read in; do ls -lrt /prdusr/rhd/prdoper/opLogDir/"$in"; done < new1.txt | grep "Dec 18 {14-23}". Is there anything wrong with the syntax or any other way i can achieve this.

Abal
  • 3

2 Answers2

3

First create a reference timestamp file with the correct mtime timestamp:

touch -d 2019-12-18T14:00 timestamp

Then parse your file and for each file test whether the file is newer than that timestamp file we just created:

while IFS= read -r name; do
    if [[ /prdusr/rhd/prdoper/opLogDir/$name -nt timestamp ]]; then
        printf 'Updated: %s\n' "/prdusr/rhd/prdoper/opLogDir/$name"
    fi
done <new1.txt

This uses the -nt file test in bash to check the modification timestamp (note that bash doesn't perform this test with a sub-second accuracy).

Using POSIX tools:

touch -d 2019-12-18T14:00 timestamp

while IFS= read -r name; do
    if [ -n "$( find "/prdusr/rhd/prdoper/opLogDir/$name" -newer timestamp )" ]
    then
        printf 'Updated: %s\n' "/prdusr/rhd/prdoper/opLogDir/$name"
    fi
done <new1.txt

This would do the test using find instead, which would output the found path if the file at hand was modified after the timestamp file, and the shell would detect the output as a non-empty string and call printf.

Kusalananda
  • 333,661
0

Using grep to filter timestamps for a range of accepted times is not easy, as regular expressions are designed for text patterns, not numerical comparisons (see e.g. this and other, similar questions).

In your particular case, you could modify the statement as in

grep -E "Dec 18 (1[4-9]|2[0123])"

but note that this requires GNU grep and "extended regular expressions" syntax. The syntax you used would have searched for the literal string {14-23} to occur in your ls output line.

Also, as a general note, parsing the output of ls is strongly disrecommended as there are many pitfalls if your filenams contain unusual characters or your locale settings lead to different timestamp formats etc. The approach you chose, e.g. entirely relies on ls's habit to display the timestamp as Mon DD HH:MM for modification times that occured in the past 1/2 year or so. It is safer to use, e.g. find with the -mtime option (possibly together with -daystart) instead.

AdminBee
  • 22,803