开发者

Retrieving the last modified file in a directory over FTP using a bash script with curl

I'm writing a bash script and one of the tasks which needs performing is to connec开发者_运维技巧t 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 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`


Edit: Sorry I just realised that the files you need were on the remote FTP server. I had thought they were local, and you were hoping to upload to FTP. So everything below is irrelevant.

Typically I do something like: ls -1rt /path/to/zips/*.zip | tail -n1

This is not always a good idea, spaces in file names etc. But it will return the most recent file name in the directory.

There's also find. You can specify a date range, and name. Depending on what you are doing, you might opt to scan a directory every x minutes for files created in the last x minutes. This has the advantage that it will pick up multiple new files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜