Capture time of File last created.

Hi ..

I have a list of filenames in a particular file. All thse files get generated in the same directory. No w i want to find out which of thse got generated last and capture that time .
Have written a while loop but seems getting struck ...

while read line
do
if [ -e "${line}" ]
then
rvst_capt_time= `ls -lrt $line | awk {'print$7'}`

# here the logic for comparing the modified tome of files should be thr and store into a variable the time of file which gets created last .

fi
fi
done < file_list

Any suggestions appreciated :slight_smile:

Hi,

I would try something like this:

#!/usr/bin/env bash

rvst_capt_time=-1
rvst_capt_file=""

while read line
do
if [ -e "${line}" ]
then
last_time=$(stat -c%Y "$line")
if (( last_time > rvst_capt_time)); then
  rvst_capt_time="$last_time"
  rvst_capt_file="$line"
fi
fi
done <file_list
printf "%s %s\n" "$rvst_capt_file" "$(stat -c%y $rvst_capt_file | sed 's/\..*$//')"

Regards,
Birei

hi

Getting this error

./test.sh: line 12: stat: command not found
./test.sh: line 12: stat: command not found
./test.sh: line 12: stat: command not found
./test.sh: line 12: stat: command not found
./test.sh: line 12: stat: command not found
./test.sh: line 20: stat: command not found

havent haed of the command stat .. though its there on my man page .

I am using Sun slolaris and Korn shell

Hi,

Ok. I'm afraid 'stat' doesn't work in Solaris like in my box. It's not my SO so I can't test it.

I tell this because searched for it and got this page:

Solaris on x86 - Reg: stat command in solaris 10

Perhaps you could try substituting '/opt/sfw/bin/stat' in case of 'stat' in the script, I don't know. Or wait if other person is able to give you another solution.

Regards,
Birei

./test.sh: line 12: /opt/sfw/bin/stat: No such file or directory
./test.sh: line 12: /opt/sfw/bin/stat: No such file or directory
./test.sh: line 12: /opt/sfw/bin/stat: No such file or directory
./test.sh: line 12: /opt/sfw/bin/stat: No such file or directory
./test.sh: line 12: /opt/sfw/bin/stat: No such file or directory
./test.sh: line 20: stat: command not found

can u pls expalin what stat is ecactly doing so that we can get an alternate commad.

You could try something like this:

perl -lne'
  push @_, (stat)[9];
  print ~~localtime(
    (sort { $b <=> $a } @_)[0]
    ) if eof
  ' infile   

Hi,

From the 'man' page:
stat - display file or file system status

Code examples:

stat -c%Y infile

outputs 1287241215

and

stat -c%y infile

outputs 2010-10-16 17:00:15.000000000 +0200

In the script I try to read time modifications in seconds of all files of the input, and select which has the biggest number.

Regards,
Birei

Thanks Birei .. Seems I have cracked it using awk i mentioned above .. will post you the contents shortly