I\'m writing a bash script and one of the tasks which needs performing is to connect to an FTP server via curl and find the name of the last modified .zip file.
The name
You can sort the filenames in one shot with a multi-key sort
command and grab the last line with tail
to get the latest file.
You'll need to specify -t-
to use a dash as sort's field separator, -n
to get a numeric sort, and list each field in the order of its priority. The format for a field specifier is:
-k, --key=POS1[,POS2] start a key at POS1 (origin 1), end it at POS2
(default end of line)
So for the year, field 3, you'll need to list it with its 4-character width as -k3,4
.
If you sort by the year, month, and day fields in that order, you'll end up with a list that has all the files in date order.
So instead of the for
loop above, you can use:
FILE=`curl -u << SERVER INFO >> 2> /dev/null | grep ${FILEPATTERN} | awk -F\ '{print $9}'
| sort -n -t- -k3,4 -k1,2 -k2,2 |tail -1`