0

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
3

You cannot use shell commands (like find) in an ftp script.

Though you can use a shell script to generate the ftp script.

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

The above code will generate file ftp.txt with commands and pass that to ftp. The generated ftp.txt will look like:

open host
user user pass
put logs/first.log first.log
put logs/second.log second.log
put logs/third.log third.log
...
bye
  • Hey @martin, I want to copy files into the FTP. So find command should executes "mv {} ...". I'll try, but I didn't understand this solution. Why is different from mine? thanks – Mich Dart Mar 17 '15 at 13:09
  • "copy files into the FTP": That's "upload" right? For upload, there's put command. There's no mv command in ftp. Why is different: Because your solution cannot work, all the code between << EOF and EOF is ftp script. And there's no find command in ftp. My solution uses shell commands (including find) to generate an ftp.txt file with all commands. – Martin Prikryl Mar 17 '15 at 13:16
  • I have updated the answer with more explanation. – Martin Prikryl Mar 17 '15 at 13:19
  • Ok, I understood. It's a good solution. And it works! One additional question: It seems the remote FTP server does not accept "put" (I've updated the first post). What do you think? – Mich Dart Mar 17 '15 at 14:00
  • So please remote the part on "passive" mode form your post. It's irrelevant to the titular question. – Martin Prikryl Mar 17 '15 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.