我想要移动超过 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
答案1
您不能在脚本中使用 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