Operating System - Tru64 Unix
1752644 Members
5614 Online
108788 Solutions
New Discussion юеВ

Re: getting the latest file from list of files

 
AKB_1
Occasional Contributor

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.


2 REPLIES 2
Steven Schweda
Honored Contributor

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.
Dennis Handly
Acclaimed Contributor

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.