使用带curl的bash脚本通过FTP检索目录中最后一个修改过的文件 [英] Retrieving the last modified file in a directory over FTP using a bash script with curl

查看:223
本文介绍了使用带curl的bash脚本通过FTP检索目录中最后一个修改过的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个bash脚本,其中一个需要执行的任务是通过curl连接到FTP服务器,并找到最后修改的.zip文件的名称。



我们正在查看的文件的名称格式是 MM_DD_YYYY_ALL.zip



到目前为止,我在<<< >>< / code>:

  export FILEPATTERN = _ALL.zip 

对于`curl -u<<< SERVER INFO>> 2 - ; / dev / null | grep $ {FILEPATTERN} | awk -F\'{print $ 9}'`
do
...
#用每个文件的东西来确定最新版本。
...
完成

文件名不是格式化 YYYY_MM_DD 似乎是通过一些快速修剪和计算无法完成的主要原因。



是有没有一种有效的方法从这个列表中提取最近修改的zip文件的名称?或者是有一些处理可以在列表生成时完成?



干杯。

解决方法

您可以使用多键 sort 命令对文件名进行一次排序,并使用 tail 来获取最新的文件。



您需要指定 -t - 来使用短划线作为排序的字段分隔符, -n 以获得数字排序,并按优先级顺序列出每个字段。字段说明符的格式是:
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ - $,$ key $ POS $ [,POS2]在POS1原产地1),结束于POS2
(默认行尾)

year,field 3,you'll need to list it with its 4-character width as -k3,4



如果按照该顺序按年份,月份和日期字段进行排序,那么您最终会得到一个包含日期顺序所有文件的列表。



因此,您可以使用:

  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`


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 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`

这篇关于使用带curl的bash脚本通过FTP检索目录中最后一个修改过的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆