I would like to move files older than 30 days and copy them into a remote FTP server.
I've already written this script, that is able to connect via FTP.
Usually, to move files, I would run this line:
find ./logs/ -type f -mtime +30 -exec mv {} destination \;
The problem is FTP doesn't recognize that command. So I think I should loop through files and move only those that are older than 30 days. But I'm not an expert in bash.
Can anyone help me please?
#!/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
Best Answer
You cannot use shell commands (like
find
) in anftp
script.Though you can use a shell script to generate the
ftp
script.The above code will generate file
ftp.txt
with commands and pass that toftp
. The generatedftp.txt
will look like: