4
votes

I am connecting to my remote sftp using below command:

sftp user@host

After inputing password next I get sftp prompt i.e.

sftp>

My job is to move multiple files from directory A to directory B. I am able to do this via rename command but only single file at a time. Is there any command/syntax which can move list of files from directory A to directory B. Something like below:

rename /A/file1 /A/file2 B/

Just to add I have to do it via command line only by using sftp protocol and Not any tool like fileZilla or winscp.

4
Cannot do using tool. I already hv winscp and filezilla. Need to do it via some command if one exists.nanosoft
older ftp clients vary widely in tools/options. I'm not so sure about sftp (and we don't know what version you are using), but recall that ftp has a group of m commands (for multi, I suppose): mget, mput (at least). Check your online help to see if there are other m commands in your version of sftp . Good luck!shellter
otherwise, you'll have to write a script that first connects to get a list of all available files, and then connects again, to feed a series of rename commands. Or maybe perl and python have functions that will help with this. Good luck.shellter
The lftp client has mmv option... lftp.yar.ru/lftp-man.htmlMark Setchell

4 Answers

4
votes

You've indicated in comments that you're trying to avoid anything which makes multiple requests to the SFTP server.

The most widely implemented version of the SFTP protocol is Version 3, draft 02. Notably, this is the version implemented by OpenSSH which is the most widely used SFTP server software. That version of the protocol makes no mention of wildcards, and the command to rename a file renames a single file or directory from an old name to a new name.

Any client that renames multiple files will have to issue one rename operation per file, possibly preceded by one or more operations to fetch the file names to be renamed. The client could provide the user with a single command to rename multiple files (or a drag-drop option, or whatever), but at the SFTP protocol level, it will necessarily have to issue at least one SFTP request per file.

1
votes

Does it have to be sftp?

You can issue commands as a block script with ssh directly.

ssh user@host '
    echo "Moving files"
    date
    rename /A/file1 /A/file2 B/
    date
' > logfile 2>&1
1
votes

psftp tool (from putty-tools) can move multiple files to another directory on remote server. Here is how I use it,

mget *.ACT
ren *.ACT backup

If the second parameter of the ren command is a directory then the first parameter can be a list of files or a wildcard, and it moves all files to the given directory.

mv command is also same as ren.

0
votes

There is no mv command using sftp. The only solution is, like you've said, to use rename.


As a workaround in terminal, you can use ftputil in python. It has a rename function:

rename(source, target)

It renames the source file (or directory) on the FTP server.

That way, you can easily connect to server, list directory, and create a loop to rename the listed files.