简体   繁体   中英

Move multiple file from one directory to another on remote sftp server

I am connecting to my remote sftp using below command:

sftp user@host

After inputing password next I get sftp prompt ie

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.

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.

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM