使用 Bash 脚本自动下载 FTP 中添加的新文件
Downloading New Files Added in FTP Automatically With Bash Script
我正在获取 FTP 使用以下脚本手动添加到路径的文件:
由于此路径中的文件每天都会加载,我希望此代码能够在特定时间每天 运行 并仅制作脚本,请记住只下载新文件而不是整个路径中可用的文件。
#!/bin/bash
主机=ftp.example.com
USER=ftp用户
密码="*****"
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd /path/to/file
mget *.html
bye
EOF
对于任何需要的人:
这已通过以下代码解决:
首先我连接到 FTP 服务器,然后我将加载文件的名称和时间保存在单独的文本文件中,然后将我的系统时间与加载文件的时间进行比较,因此使用此代码我可以自动下载新文件。
#!/bin/bash
use strict;
use warnings;
use diagnostics;
# Login credentials
user='*****' #Do not forget to enclose inside single or double quotes
pass='*****'
directory='/RemoteDirectory/'
host='IP'
# Set time
# perl -e 'print time' will return the current time in Unix epoch format
# So if you substract 43200, that should give you the current time minus 30 minutes in Unix epoch format:
time=$(perl -e 'print time-43200')
# Connect to host and download the listing of the remote directory
ftp -n $host <<END_GET_LIST
quote USER $user
quote PASS $pass
cd $directory
ls -l /LocalDirectory/ftpList.txt
quit
END_GET_LIST
# Disconnect from remote host
# Save the 6th, 7th, and 8th field of the directory listing (i.e. Aug 15 5:15) of each line into file Dates.txt
awk -F ' ' '{print ,,}' /LocalDirectory/ftpList.txt > /LocalDirectory/'Dates.txt'
# Save the 9th field of the directory listing (file name) of each line into file Files.txt
awk -F ' ' '{print }' /LocalDirectory/ftpList.txt > /LocalDirectory/'Files.txt'
linenum=0 #Auxiliary variable for sed.
while read list; do
linenum=$((linenum+1))
#Convert the modification datetime of each file to Unix's epoch
epoch=$(perl -MFile::stat -e "print stat('$list')->mtime")
if [ $epoch -gt $time ] ; then
file=$(sed -n "${linenum}p" Files.txt) #If the condition is satisfied, use sed to get the name of the file
#in the same line of Files.txt.
#Connect again and download file when the condition above has been satisfied.
ftp -n $host <<END_RETRIEVE
quote USER $user
quote PASS $pass
cd $directory
get $file
quit
END_RETRIEVE
fi
done < /LocalDirectory/'Dates.txt'
我正在获取 FTP 使用以下脚本手动添加到路径的文件: 由于此路径中的文件每天都会加载,我希望此代码能够在特定时间每天 运行 并仅制作脚本,请记住只下载新文件而不是整个路径中可用的文件。 #!/bin/bash 主机=ftp.example.com USER=ftp用户 密码="*****"
ftp -inv $HOST <<EOF
user $USER $PASSWORD
cd /path/to/file
mget *.html
bye
EOF
对于任何需要的人: 这已通过以下代码解决: 首先我连接到 FTP 服务器,然后我将加载文件的名称和时间保存在单独的文本文件中,然后将我的系统时间与加载文件的时间进行比较,因此使用此代码我可以自动下载新文件。
#!/bin/bash
use strict;
use warnings;
use diagnostics;
# Login credentials
user='*****' #Do not forget to enclose inside single or double quotes
pass='*****'
directory='/RemoteDirectory/'
host='IP'
# Set time
# perl -e 'print time' will return the current time in Unix epoch format
# So if you substract 43200, that should give you the current time minus 30 minutes in Unix epoch format:
time=$(perl -e 'print time-43200')
# Connect to host and download the listing of the remote directory
ftp -n $host <<END_GET_LIST
quote USER $user
quote PASS $pass
cd $directory
ls -l /LocalDirectory/ftpList.txt
quit
END_GET_LIST
# Disconnect from remote host
# Save the 6th, 7th, and 8th field of the directory listing (i.e. Aug 15 5:15) of each line into file Dates.txt
awk -F ' ' '{print ,,}' /LocalDirectory/ftpList.txt > /LocalDirectory/'Dates.txt'
# Save the 9th field of the directory listing (file name) of each line into file Files.txt
awk -F ' ' '{print }' /LocalDirectory/ftpList.txt > /LocalDirectory/'Files.txt'
linenum=0 #Auxiliary variable for sed.
while read list; do
linenum=$((linenum+1))
#Convert the modification datetime of each file to Unix's epoch
epoch=$(perl -MFile::stat -e "print stat('$list')->mtime")
if [ $epoch -gt $time ] ; then
file=$(sed -n "${linenum}p" Files.txt) #If the condition is satisfied, use sed to get the name of the file
#in the same line of Files.txt.
#Connect again and download file when the condition above has been satisfied.
ftp -n $host <<END_RETRIEVE
quote USER $user
quote PASS $pass
cd $directory
get $file
quit
END_RETRIEVE
fi
done < /LocalDirectory/'Dates.txt'