3

I have a directory in linux which has a list of log files where log files get auto generated if some job runs. Each log file gets appended with the timestamp like "JobName_TimeStamp"

UPDATED:

job_2014-05-28_15:05:26.log
job_2014-05-28_15:06:58.log
job_2014-05-28_15:07:02.log
job_2014-05-28_15:07:57.log
job_2014-05-28_15:08:00.log
job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log

job1_2014-05-28_15:08:11.log
job1_2014-05-28_15:08:12.log
job1_2014-05-28_15:08:13.log
job1_2014-05-28_15:08:14.log
job1_2014-05-28_15:08:22.log

I wanted to run a linux command to list all files greater than a particular timestamp?

For Example 1 : I will pass two parameters , If the TimeStamp given is "2014-05-28_15:08:00" and Job Name is "job"

I should get the list as

job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log

Example 2 : I will pass two parameters , If the TimeStamp given is "2014-05-28_15:08:11" and Job Name is "job1"

I should get the list as

job1_2014-05-28_15:08:12.log
job1_2014-05-28_15:08:13.log
job1_2014-05-28_15:08:14.log
job1_2014-05-28_15:08:22.log

Any solutions?

  • I have updated the question – Kabilan May 28 '14 at 11:41
  • 1
    If the filenames reflect the true timestamps of the files, then a better alternative may be to use that property directly i.e. something like find -type f -newermt '2014-05-28 15:08:11' – steeldriver May 28 '14 at 13:21
2

An awk (mawk 1.3.3) solution:

ls | awk -F'[_.]' '{printf "%s_%s\n", $2, $3}' | \
awk ' $0 > "2014-05-28_15:08:00" {print}'

Gives:

2014-05-28_15:08:01
2014-05-28_15:08:09
2014-05-28_15:08:10
2014-05-28_15:08:11
2014-05-28_15:08:12
2014-05-28_15:08:13
2014-05-28_15:08:14
2014-05-28_15:08:22
2

A perl solution:

$ perl -nle '
    BEGIN {$t = "2014-05-28_15:08:00"}
    if (/_(.*?)\./) {
        print if $1 gt $t;
    }
' file
job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log

Update

A simple perl script:

#!/usr/bin/perl

use strict;
use warnings;

my $t = $ARGV[0];
my $l = qr/$ARGV[1]/;

while(<DATA>) {
    if (/${l}_/) {
        if (/_(.*?)\./) {
            print if $1 gt $t;
        }
    }
}

__DATA__
job_2014-05-28_15:05:26.log
job_2014-05-28_15:06:58.log
job_2014-05-28_15:07:02.log
job_2014-05-28_15:07:57.log
job_2014-05-28_15:08:00.log
job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log
  • I would be happy if the code is in linux command :) – Kabilan May 28 '14 at 10:28
2

The below code worked:

 for i in myjob*; do
           if [[ "myjob_2014-05-28_15:08:00.log" < "$i" ]]; then
                  echo $i
           fi
 done
1

sort can do the majority of the work here:

jobname=job
timestamp=2014-05-28_15:08:00
param=${jobname}_${timestamp}.log
printf %s\\n "${param}" ./* |
sort -t_ -r -k1,1 -k2,2n -k3,3n |
sed -n "\|./${jobname}_|{/${param}/q;p}"

###OUTPUT:

./job_2014-05-28_15:08:22.log
./job_2014-05-28_15:08:14.log
./job_2014-05-28_15:08:13.log
./job_2014-05-28_15:08:12.log
./job_2014-05-28_15:08:11.log
./job_2014-05-28_15:08:10.log
./job_2014-05-28_15:08:09.log
./job_2014-05-28_15:08:01.log

And the second example:

jobname=job1
timestamp=2014-05-28_15:08:11
param=${jobname}_${timestamp}.log
printf %s\\n "${param}" ./* |
sort -t_ -r -k1,1 -k2,2n -k3,3n |
sed -n "\|./${jobname}_|{/${param}/q;p}"

###OUTPUT:

./job1_2014-05-28_15:08:22.log
./job1_2014-05-28_15:08:14.log
./job1_2014-05-28_15:08:13.log
./job1_2014-05-28_15:08:12.log
0

You can use -cnewer with find:

find /mylogdir -name "job_*" -cnewer /mylogdir/job1_2014-05-28_15:08:00.log

Of course, this is finding files that were actually modified or had their metadata modified after job1_2014-05-28_15:08:00.log and not based on the file names. In your case this should work out just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.