FTP文件传输,通过目录循环并复制旧文件

我想移动超过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 

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

尽pipe可以使用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