从Unix中的FTP获取远程服务器的最新文件

时间:2015-03-03 15:44:06

标签: unix ftp

我需要从Unix中的远程主机获取文件。我正在使用ftp命令。问题是我需要该位置的最新文件。我就是这样做的:

dir=/home/user/nyfolders
latest_file=$(ls  *abc.123.* | tail -1)
ftp -nv <<EOF
open $hostname
user $username $password
binary
cd $dir
get $latest_file
bye
EOF

但是我收到了这个错误:

(remote-file) usage: get remote-file [ local-file ]

我认为我试图从ftp命令中获取文件的方式不正确,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:5)

您不能在ftp命令脚本中使用shell功能,例如别名,管道,变量等。

ftp不支持使用任何语法的此类高级功能。

虽然,你可以做两步(在步骤之间使用shell功能)。

首先获取远程目录到本地文件(/tmp/listing.txt)的列表:

ftp -nv <<EOF
open $hostname
user $username $password
cd $dir
nlist *abc.123.* /tmp/listing.txt
bye
EOF

找到最新的文件:

latest_file=`tail -1 /tmp/listing.txt`

下载它:

ftp -nv <<EOF
open $hostname
user $username $password
binary
cd $dir
get $latest_file
bye
EOF