0

I am writing a bash script to look for a file which's file name is most recently and file name like:

...
Wed_Dec_30_16:47:41_PHT_2015.zip
Wed_Dec_30_16:00:41_PHT_2015.zip
Wed_Dec_30_16:00:41_PHT_2015.zip
Thu_Dec_31_16:49:14_PHT_2015.zip
...

I'm search all .zip file use find command and i thought maybe i need a array like:

readarray -t lines <<< "$(find . -type f -name '*.zip' -exec basename {} \;|tr -d '[.zip]'|tr '_' ' ')"
len=${#lines[@]}
for ((i=0;i<len;i++)); do
    echo ${lines[$i]}
done

And, i got all of file name (with date time) like this in terminal. it looks completly:

Wed Dec 30 16:47:41 PHT 2015
Wed Dec 30 16:00:35 PHT 2015
Thu Dec 31 12:14:13 PHT 2015
Thu Dec 31 12:19:34 PHT 2015
Wed Dec 30 16:49:14 PHT 2015
Thu Dec 31 13:01:11 PHT 2015
Mon Dec 28 12:35:44 PHT 2015
...

But, i'm not sure how can i compare those many string as i use date.

And absolutely I'm not sure it's a really and complate way to revert the file whichs string like Wed Dec 30 16:49:14 PHT 2015 to Wed_Dec_30_16:49:14_PHT_2015.zip

Thanks for help.

Edit:

I fix my script, and i comvert 'string' to 'date' which is seconds level:

declare -a my_array
len=${#lines[@]}
for ((i=0;i<$len;i++));do
    my_array[$i]=`date -d "${lines[$i]}" +%s`
done

and choose the max one:

max=${my_array[0]}
for((i=0;i<$len;i++));do
    if [ ${my_array[$i]} -gt $max ];then max=${my_array[$i]};fi
done
echo $max

terminal:

1451465261
1451462435
1451535253
1451535574
1451465354
1451538071
1451277344
1451538071 <== max one

So, should i revert it like: Wed_Dec_30_16:00:41_PHT_2015.zip?

Does anyone have a simple way?

0

With gnu sort having input.txt as input (explain):

cat input.txt | sort -t _ -k6n,6.4 -k2M -k3n -k4.1n,4.2 -k4.4n,4.5 -k4.7n,4.8

Add --debug flag to see fields used for sorting. Useful when you have to tweak substrings:

Wed_Dec_30_16:00:41_PHT_2015.zip
                        ____
    ___
        __
           __
              __
                 __
  • It's exactly what i need. 'sort' is a simple way to solve this problem. – Se ven Jan 19 '16 at 9:32
0

I know you have your data in an array , but I tested with a file. Shouldn't matter - logic is the same.
Just convert to seconds since epoch, test which epoch number is biggest, and remember that number - thats your most recent date. Then obviously you need to reconcile that string against your file structures to get the specific file you were looking for.

file: dates.txt

Wed Dec 30 16:47:41 PHT 2015
Wed Dec 30 16:00:35 PHT 2015
Thu Dec 31 12:14:13 PHT 2015
Thu Dec 31 12:19:34 PHT 2015
Wed Dec 30 16:49:14 PHT 2015
Thu Dec 31 13:01:11 PHT 2015
Mon Dec 28 12:35:44 PHT 2015

file: find_latest.sh

#!/usr/bin/env bash

newest=0

while read line
do
    i=$(awk '{print $1" "$2" "$3" "$4" "$6}' <<<$line)
    epoch=$(date --date="$i" +'%s')
    if [ "$epoch" -gt "$newest" ]
    then
        newest=$(date --date="$i" +"%s")
    fi

done < dates.txt

echo "newest is: "$(date --date="@${newest}")

result:

newest is: Thu Dec 31 13:01:11 NZDT 2015
  • Thanks, it works completely. it help me solve the problem and make my code more simplify, no redundance. thank you. – Se ven Jan 19 '16 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.