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 format of the files we are looking at is MM_DD_YYYY_ALL.zip.
So far I have, with omissions in << >>:
export FILEPATTERN=_ALL.zip
for FILE in `curl -u << SERVER INFO >> 2> /dev/null | grep ${FILEPATTERN} | awk -F\ '{print $9}'`
do
...
# Do stuff with each file to determine most recent version.
...
done
The fact that the file name isn’t formatted YYYY_MM_DD seems to be the main reason this can’t be done with some quick trimming and calculations.
Is there an efficient way to pull the name of the most recent modified zip file from this list? Or is there some processing which can be done whilst the list is being generated?
Cheers.
You can sort the filenames in one shot with a multi-key
sortcommand and grab the last line withtailto get the latest file.You’ll need to specify
-t-to use a dash as sort’s field separator,-nto get a numeric sort, and list each field in the order of its priority. The format for a field specifier is: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
forloop above, you can use: