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

4

5

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.

Lucchesi answered 2/7, 2018 at 12:19 Comment(7)
Just install filezilla software. filezilla-project.org/download.php?platform=win32Mckelvey
Cannot do using tool. I already hv winscp and filezilla. Need to do it via some command if one exists.Lucchesi
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!Seoul
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.Seoul
The lftp client has mmv option... lftp.yar.ru/lftp-man.htmlMandrel
@shellter- Feeding series of rename commands means mutiple requests to stp server. That's what I am trying to avoid. And mget n mput are for downloading n uploading mutiple files not or moving so won't hep me here.Lucchesi
at nanosoft : I'm thinking of a command series sent to the remote sftp as @PaulHodges has done with ssh (which might be an option for you too), except to build up a string that has all the rename commands embedded. I have done such things with unhelpful ftp clients, so I think it would be possible for sftp. Good luck.Seoul
S
4

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.

Sentient answered 3/7, 2018 at 18:59 Comment(0)
I
1

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
Immunogenetics answered 2/7, 2018 at 18:12 Comment(1)
Yes its SFTP as I am dealing with sftp server only.Lucchesi
C
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.

Carnal answered 30/1, 2021 at 20:42 Comment(0)
E
0

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.

Ellaelladine answered 2/7, 2018 at 14:30 Comment(1)
Loop means mutiple requests to stp server. That's what I am trying to avoid.Lucchesi

© 2022 - 2024 — McMap. All rights reserved.