sftp through

General support questions
Post Reply
Posts: 115
Joined: 2017/02/25 12:41:42

sftp through

Post by knzzz » 2018/03/08 15:38:01

Hi Team
can u someone pls tell a shell script for sftp


User avatar
Forum Moderator
Posts: 22789
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: sftp through

Post by TrevorH » 2018/03/08 16:10:14

can u someone pls tell a shell script for sftp
What does that even mean?
CentOS 5 died in March 2017 - migrate NOW!
Full time Geek, part time moderator. Use the FAQ Luke

Posts: 7
Joined: 2017/02/14 15:59:24

Re: sftp through

Post by dimwit » 2018/03/11 12:06:58

The sftp client offers the -b option by which you can pass it a text file that contains a sequence of sftp commands that you would like to get executed.


Code: Select all

$ sftp -o identityfile=~/.ssh/some_key -b ~/scripts/file_with_sftp_cmds.txt someuser@somehost
If you only want to get a single or few sftp command(s) executed you can also pass -b a single dash, meaning read from stdin,
and either read from a here string or a pipe.

e.g. (n.b. newer versions of the sftp client now also support the -i option to pass the path to identity file, respectively the key pair to use)

Code: Select all

$ sftp -i ~/.ssh/some_key -b- someuser@somehost:some/path/relative/to/someusers/home <<<"ls -l"


$ echo -e "progress\nget /some/absolute/path/someuser/may/access/reading/some_file_glob*\nbye" | sftp -i ~/.ssh/some_key -b- someuser@somehost
But personally, I would always prefer the ssh and scp client commands for scripting over sftp,
and only use the latter where I don't have the two former alternatives, maybe because I am restricted to some anonymous SFTP server where I have no login account.

Post Reply