8

On the remote FTP server, I have a set of files in the directory remote.dir1. I would like to move all the files in that directory to remote.dir2.

I am using lftp and was trying something like this:

lftp> mv remote.dir1/* remote.dir2/

It does not work and displays: Access failed: 550 remote.dir1/*: The filename, directory name, or volume label syntax is incorrect.

I also tried:

lftp> glob -a mv remote.dir1/* remote.dir2/

which displays the usage message: Usage: mv <file1> <file2>

However, moving single file works: lftp> mv remote.dir1/file1 remote.dir2/

As a last resort, we can construct a file containing a set of lftp mv commands and source it.
Are there any solutions I am not aware of?
Or, are there any capable command line tools for the task?

Edit: I am restricted to FTP environment, so only solutions using FTP are acceptable due to the environmental constraint.

cychoi
  • 580
  • 1
  • 5
  • 12
  • When you say 'it does not work' are you getting any errors? – Drew Khoury Aug 21 '13 at 07:56
  • yes, in this particular case i'm using `lftp`, it throws me this line: `Access failed: 550 remote.dir1/*: The filename, directory name, or volume label syntax is incorrect.` – cychoi Aug 21 '13 at 08:01

6 Answers6

5

For documentation purpose, I will post the steps I used to complete the task. Any better solutions are much appreciated. ;-)

Note: this solution uses the lftp FTP client. You may have to install it on your machine before you can proceed.

Solution:

lftp> renlist remote.dir1/ | "sed 's/\(.*\)/mv \"\1\" \"remote.dir2\/\"/'" > list  
lftp> source list  
lftp> !rm list  

Or, the one-linerTM:

lftp> renlist remote.dir1/ | "sed 's/\(.*\)/mv \"\1\" \"remote.dir2\/\"/'" > list && source list && !rm list
cychoi
  • 580
  • 1
  • 5
  • 12
3

It appears that lftp only supports the glob syntax with commands accepting a single argument, so mv, which requires two, is out.

With FTP, your server might allow for extended commands, especially the execution of a limited command set via SITE EXEC or similar means - check the FTP server's documentation, its help (SITE HELP) or the login banner. These however are not standardized, so if the solution needs to be FTP-server-agnostic, scripting based on the output of the file list seems like the best idea.

the-wabbit
  • 40,737
  • 13
  • 111
  • 174
1

A bit late to the party, but in July 2017 the "mmv" command was added to lftp.

Usage: mmv [OPTS] <files> <target-dir>
Move <files> to <target-directory> with wildcard expansion
 -O <dir>  specifies the target directory (alternative way)

So, in your case:

mmv remote.dir1/* remote.dir2/
thedutchy
  • 11
  • 1
0

The answer given by cychoi is for an interactive session. I was able to make use of it in a shell script (Bash 4.2.46, lftp 4.4.8-14.el7_9, CentOS 7.6.1810). Essentially:

local flist=files.txt
local cmds=cmds.txt
cd ${WORK_DIR}
LFTP_PASSWORD=${SFTP_PASSWORD} \
lftp --env-password -u ${SFTP_USER} sftp://${SFTP_HOST}/ << EOF
nlist ${SRC_DIR} > $flist
!cat $flist | grep -v "^\." | sed "s|\(.*\)|mv ${SRC_DIR}/\1 ${DST_DIR}/|" > $cmds
source $cmds
EOF

Where, the script's variables had the following meanings:

  • WORK_DIR The work directory for the script to create intermediate files. The files files.txt and cmds.txt get created inside this directory.
  • SFTP_HOST SFTP server IP address or hostname.
  • SFTP_USER, SFTP_PASSWORD SFTP user and password used to connect to the server.
  • SRC_DIR Source directory on the SFTP server. All files residing on this directory had to be moved.
  • DST_DIR Destination directory on the SFTP server. Files were moved to this directory.
pdp
  • 778
  • 1
  • 7
  • 16
0

rsync is great for moving files from one machine to another (or even between the same machine).

rsync -avz -e "ssh -i /key/path/key.rsa" user@1.2.3.4:/path/on/remote/server/ /path/on/local/server

This command supplies a private key, and connects to a machine. It grabs files from the remote directory, and copies them to the local directory. You can also do the reverse of this by switching the source and destination.

Drew Khoury
  • 4,637
  • 8
  • 27
  • 28
  • 2
    The question is about using *an FTP client* to move a server directory's files – the-wabbit Aug 21 '13 at 07:50
  • OP also stated: "Are there any solutions I am not aware of? Or, are there any capable command line tools for the task?". rsync sounds pretty capable to me. – Drew Khoury Aug 21 '13 at 07:51
  • The question is about moving files. OP happened to try FTP but that's not the only way. – Drew Khoury Aug 21 '13 at 07:53
  • @DrewKhoury Sorry for not being clear enough. I am restricted to FTP protocol only. One of reasons is because the FTP server is a Windows machine. I will update the question on that. Thanks :) – cychoi Aug 21 '13 at 07:54
-1

I thought FTP was more or less dead. It's been close to 5 years since I've used it, and it was on the way out 10 years ago.

That said, I always found the ncftp family of command line tools very friendly, and I expect this would "just work".

I'm not going to set up an FTP server for the purposes of verifying this, but give it a try. There's a lot else to like about ncftp (At least from version 2 on).

mc0e
  • 5,866
  • 18
  • 31