Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Operating System - Tru64 Unix
Showing results for 
Search instead for 
Did you mean: 

getting the latest file from list of files

Occasional Contributor

getting the latest file from list of files


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
cp $file1 $file1$stampyyyymmdd
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.


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.