Bash

FTP文件傳輸,遍歷目錄並複制舊文件

  • August 25, 2020

我想移動超過 30 天的文件並將它們複製到遠端 FTP 伺服器。

我已經編寫了這個腳本,它可以通過 FTP 連接。

通常,要移動文件,我會執行這一行:

find ./logs/ -type f -mtime +30 -exec mv {} destination \;

問題是 FTP 無法辨識該命令。所以我認為我應該遍歷文件並只移動那些超過 30 天的文件。但我不是 bash 方面的專家。

任何人都可以幫助我嗎?

#!/bin/bash

HOST=xxx             #This is the FTP servers host or IP address.
USER=xxx                     #This is the FTP user that has access to the server.
PASS=xxx              #This is the password for the FTP user.

# Call 1. Uses the ftp command with the -inv switches. 
#-i turns off interactive prompting. 
#-n Restrains FTP from attempting the auto-login feature. 
#-v enables verbose and progress. 

ftp -inv $HOST << EOF

# Call 2. Here the login credentials are supplied by calling the variables.

user $USER $PASS

pass

# Call 3. I change to the directory where I want to put or get
cd /

# Call4.  Here I will tell FTP to put or get the file.
find ./logs/ -type f -mtime +30 -exec echo {} \;
#put files older than 30 days

# End FTP Connection
bye

EOF

您不能在腳本中使用 shell 命令(如find) 。ftp

雖然您可以使用 shell 腳本來生成ftp腳本。

echo open $HOST > ftp.txt
echo user $USER $PASS >> ftp.txt
find ./logs/ -type f -mtime +30 -printf "put logs/%f %f\n" >> ftp.txt
echo bye >> ftp.txt

ftp < ftp.txt

上面的程式碼將生成ftp.txt帶有命令的文件並將其傳遞給ftp. 生成的ftp.txt將如下所示:

open host
user user pass
put logs/first.log first.log
put logs/second.log second.log
put logs/third.log third.log
...
bye

引用自:https://serverfault.com/questions/676067