从 Unix 中的 FTP 从远程服务器获取最新文件 [英] Get the latest file from a remote server from an FTP in Unix

查看:20
本文介绍了从 Unix 中的 FTP 从远程服务器获取最新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get a file from a remote host in Unix. I am using the ftp command. The issue is I need the latest file from that location. This is how I am doing it:

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 命令中获取文件的方式不正确,有人可以帮帮我吗?

I think the way I am trying to get the file from within the ftp command is incorrect, can someone please help me out?

推荐答案

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

You cannot use shell features, like aliases, piping, variables, etc, in ftp command script.

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

The ftp does not support such advanced features using any syntax.

不过,您可以分两步完成(在步骤之间利用外壳功能).

Though, you can do it two-step (to make use of shell features between the steps).

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

First get a listing of remote directory to a local file (/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

这篇关于从 Unix 中的 FTP 从远程服务器获取最新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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