I'm attempting to move multiple files on an FTP server to a different directory. 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.

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.

  • I think you can find easier solution than moving files. Why do you need to move files, running out of space in a disk? – JohnKoch Jun 26 '19 at 5:46

Since it appears that you can login successfully with your script, here are the command once that script is logged in.

  • Contrary to common sense the rename command will move files

    rename /path/to/file/to/move /path/to/files/destination/

  • And this should work with a wildcard, for example mp4's

    rename /path/to/files/*.mp4 /path/to/destination/

Hope this is what you were looking for!

Source: https://stackoverflow.com/questions/9461844/how-to-move-files-using-ftp-commands

| improve this answer | |
  • Unfortunately that doesn't work. 'Requested action not taken. File unavailable (file not found or no access).' I have access to the file as I can explicitly name the file to be renamed and it works. The wildcard is what I believe is not working. – Danny Mar 30 '17 at 20:34
  • "The rename command does not support wildcards." - stackoverflow.com/questions/31180849/… "For example, to retrieve files named myfile1, myfile2, and myfile3 from another computer, at the FTP prompt, enter: mget myfile?" - kb.iu.edu/d/aceu It appears that you can use a ? as a single character wildcard, try that. – enconn Mar 30 '17 at 22:15
  • Thanks for the suggestion, though, I'm trying to move files within the same ftp server. For example, I downloaded file yyy from the server but now I need to move it to an archive directory. I need a way to script the move without knowing the file names. – Danny Mar 31 '17 at 13:18

FTP do not have mv command. You have to use rename command

The concept is generate a todo file to rename(move) each file one by one
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 , for demo you can do more actions on text file
2.2 rename (move file)
3. execute ftp command with to do list file

# change local directory
cd [local-directory]

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

# create ftp action list
echo >>todo.lst user [user] [password]
while read N
    #echo >>todo.lst cd /OUT #for demo you can do more actions on text file
    #echo >>todo.lst get $N  #for demo you can do more actions on text file
    echo >>todo.lst rename $N ARCHIVE/$N #did not change file name, move directory only
done <list.txt

echo >>todo.lst quit

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

# cleanup
rm todo.lst
| improve this answer | |
  • (1) After you’ve been on this site for a few months, you will anticipate that, in situations like this, there will always somebody who asks “Will that work for filenames with spaces, tabs, backslashes, asterisks, and other weird characters in them?”  I actually can’t tell whether your code will handle those.  (2) If there are 42 files, then todo.lst will cd /OUT 42 times, without ever cd’ing anywhere else.  Is that necessary? … (Cont’d) – Scott Jun 26 '19 at 6:19
  • (Cont’d) …  (3) Can you explain what is happening a little more clearly?  Is the rename command moving files between directories on the server?  If so, why are you getting them, too?  The OP doesn’t really seem to want to make local copies; they are just operating in the belief that the only way to move a file between directories on the FTP server is to download it and re-upload it to the destination. – Scott Jun 26 '19 at 6:19
  • Thank you for your opinions. I have update the answer – chunhunghan Jun 26 '19 at 6:42

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.