Danny
Danny

Reputation: 23

Scripting a file move on an FTP Server

I'm attempting to move multiple files on an FTP server to a different directory on the same server. So far, I've written a bash script that will login and retrieve any new files in the remote directory but the ftp command doesn't support a 'mv' command. Essentially the script would download the new file(s) and then once downloaded move the file(s) to a different directory on the same server. Please Note that the filenames will be different every time so the use of wildcards is important here.

Before you answer please note that this needs to be automated so using a GUI like Filezilla wouldn't help me as I would have to login to various ftp sites and move the files manually, also, keep in mind that I'm unable to ssh into any of the servers as they are managed by other company's and ftp access is all I'm able to get. Last thing, I won't know what the file names are so using a wildcard would be helpful.

Any help or guidance is truly appreciated.

Thank you!

Upvotes: 2

Views: 3308

Answers (2)

chunhunghan
chunhunghan

Reputation: 54367

full script to achieve move more than one file
1. get file list from ftp server with mls command
2. generate to do list file
2.1 get file
2.2 rename (move file)
3. execute ftp command with to do list file

#!/bin/sh
clear
# change local directory
cd [local-directory]

#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls abc*.* list.txt 
quit
EOF

# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
    echo >>todo.lst cd /OUT
    echo >>todo.lst get $N
    echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt

echo >>todo.lst quit

# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst

# cleanup
rm todo.lst

Upvotes: 0

Roger Sinasohn
Roger Sinasohn

Reputation: 483

Perhaps the rename command in ftp could work for you?

rename [from [to]]
       Rename the file from on the remote machine, to the file to.

I gave it a bash with an old file I had sitting on a server and it seemed to do what you want:

ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||55572|)
150 Accepted data connection
-rw-r--r--    1 sinasohn   sinasohn           21 Mar 31 16:37 tmp/testfile01
226-Options: -a -l 
226 1 matches total

ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||64715|)
150 Accepted data connection
226-Options: -a -l 
226 0 matches total

ftp> rename tmp/testfile01 tmp2/testfile01
350 RNFR accepted - file exists, ready for destination
250 File successfully renamed or moved

ftp> ls tmp/test*
229 Entering Extended Passive Mode (|||56698|)
150 Accepted data connection
226-Options: -a -l 
226 0 matches total

ftp> ls tmp2/test*
229 Entering Extended Passive Mode (|||50239|)
150 Accepted data connection
-rw-r--r--    1 sinasohn   sinasohn           21 Mar 31 16:37 tmp2/testfile01
226-Options: -a -l 
226 1 matches total
ftp> 

I put blank lines in between commands here for clarity.

Hope this helps!

Upvotes: 2

Related Questions