Finding the maximum timestamp in a folder

I've the files in a directory in the following format having date +%Y%m%d%H
YR_MNTH_2013061205
YR_MNTH_2013060107
and i need the latest file i.e; YR_MNTH_2013061205 to be moved to another folder

#!/bin/ksh 
# Ksh 88 Version
for test_time in YR*
do
 ---
done 

How can i achieve that !
Thank You .

Hi,

Could you please provide a few more sample file names? Do all the files start with YR_MNTH or do you have other files like YR_ABCD* and so on?

Can we use "" as delimiter to manipulate and get the maximum timestamp? Will there be always 3 fields considering "" as delimiter (i.e., NO file of the format YR_2012030407 or YR_MNTH_ABC_2012040806)

Hello Smile689,

Could you please try the following code hope it wil be helpful for you.

 
$ cat delete_latest_file.ksh

####Script starts from here#####
value=`ls -ltr | grep "YR_MNTH_*" | grep -v "grep" | awk '{print$9}' | tail -1`
 
###in case you want to see which file is going to move as follows###
echo $value
 
###moving the file ###
mv ${value} /home/test_user
 
####Script ended here#####

Could you please try this and let me know if I can help further on same.

Thanks,
R. Singh

1 Like