Re: getting the latest file from list of files (268 Views)
Reply
Occasional Contributor
AKB_1
Posts: 4
Registered: ‎09-15-2008
Message 1 of 3 (268 Views)

getting the latest file from list of files

Hi,

I have a requirement as below ( OS: Hp Tru64 UNIX 5.1b): I have to get *_pos_pay.txt list of files and from the list I should get the latest file and cp it into *_pos_pay.txt_ but if the latest file is payroll_pos_pay.txt, I shouldn't copy it instead I have to get the next latest file and copy it as above.

My code is as below:
======================
file= ls -lat *pos_pay.txt | cut -c55-75 | head -2
file1= ls -lat *pos_pay.txt | cut -c55-75 | head -1
stampyyyymmdd=`date +"%Y%m%d"`
count=`ls $file |grep -c payroll_pos_pay.txt`
echo $count
if [[ ${count} > 0 ]]; then
sfile= $file | cut -c21-39
cp $sfile $sfile_$stampyyyymmdd
exit;
else
cp $file1 $file1$stampyyyymmdd
exit;
fi
==================================
The command - {sfile= $file | cut -c21-39}

is taking the contents of the file in $file but I am trying to get the original file name by using the above command and then use the variable sfile in the cp command. Please advise.

Thanks
Aish.


Honored Contributor
Steven Schweda
Posts: 9,096
Registered: ‎02-23-2005
Message 2 of 3 (268 Views)

Re: getting the latest file from list of files

Because there's nothing Tru64-specific about
this, you might try asking in an HP-UX forum.
There seem to be more people there who like
to write scripts for other people.
Acclaimed Contributor
Dennis Handly
Posts: 25,283
Registered: ‎03-06-2006
Message 3 of 3 (268 Views)

Re: getting the latest file from list of files

If you use a real shell and awk, you can make your script much simpler:
file1=$(ls -lat *pos_pay.txt | fgrep -v payroll_pos_pay.txt | \
head -1 | awk '{print $NF}')

stampyyyymmdd=$(date +"%Y%m%d")
cp $file1 $file1$stampyyyymmdd

You may want to check if $file1 is null.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.