Get the latest file from a remote server from an FTP in Unix

前端 未结 1 1692
囚心锁ツ
囚心锁ツ 2020-12-11 11:08

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

相关标签:
1条回答
  • 2020-12-11 12:04

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

    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).

    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
    

    Find the latest file:

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

    And download it:

    ftp -nv <<EOF
    open $hostname
    user $username $password
    binary
    cd $dir
    get $latest_file
    bye
    EOF
    
    0 讨论(0)
提交回复
热议问题