The Computer Oracle

Using sftp like scp

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Using Sftp Like Scp
00:28 Accepted Answer Score 12
02:29 Answer 2 Score 0
02:51 Thank you

--

Full question
https://superuser.com/questions/1434225/...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #openssh #sftp #scp

#avk47



ACCEPTED ANSWER

Score 12


Since OpenSSH 8.7, scp can use SFTP protocol. Just add -s switch:

scp -s ./myfile myserver:/home/myfile

Since OpenSSH 9.0 the scp uses SFTP by default.


sftp command is indeed built around commands (like ftp). So it cannot work as an in-place replacement for scp.

Though you can use one-liners like:

echo get /remote/path/file.txt /local/path.txt | sftp user@example.com

or

echo put /local/path.txt /remote/path/file.txt | sftp user@example.com

You may want to add -b - to force a non-interactive mode.


Interestingly (as @Kamil mentioned), for downloads, you can also use this scp-like syntax:

sftp user@example.com:/remote/path/file.txt /local/path.txt

(the use of the second argument is not documented).

Use of sftp:// prefix is also possible since OpenSSH 7.7, which somewhat modifies the syntax (allows URL encoding of the username and the path).


There's also free/open-source pscp command-line client that comes in PuTTY package, which is available for Linux (while more commonly used on Windows). It has an identical command-line interface to OpenSSH scp. Similarly to recent versions of OpenSSH scp, pscp is primarily an SFTP client (while it can fallback to SCP, if the server does not support SFTP).

Despite its name, PSCP (like many other ostensible scp clients) can use either of these protocols.

...

Normally PSCP will attempt to use the SFTP protocol, and only fall back to the SCP protocol if SFTP is not available on the server.

You can install PuTTY/pscp with apt-get like:

sudo apt-get install -y putty

There's similar question on Stack Overflow: Single line sftp from terminal




ANSWER 2

Score 0


You should also consider rsync. Except for the remote1-to-remote2 transfer that scp allows, it appears -- from a quick read of man scp anyway -- that you could make rsync do all of that quite easily, or write a wrapper that is not too painless.

Rsync has other advantages that you get by default (viz., much more efficient when replacing an older file), even if you don't use any of it's many options.