Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SFTP from command line?

Status
Not open for further replies.

Autosys

Programmer
Jun 1, 2004
90
GB
Hi there

I'm trying to write a small script that will loop through all the files in a directory (for file in ls) and then to SFTP them one at a time onto my remote server.

My SFTP Command looks like this: sftp user@server

Very basic I know, I was wondering if there was a way to pass an extra param to sftp (say -f filename) of the file that I would like to put onto the remote server. Another switch that would be useful I guess would be to first specify the target directory as well.

Is it better to use the -B Batchfile option in order to achieve this? I tried it but when I run the command:

psftp user@server -B batchfile I get the following error:

usage: sftp [-1vC] [-osshopt = value] [user@]host

Any help would be great!
Thx
 
Hi

No idea about [tt]sftp[/tt], I preffer the old good [tt]ftp[/tt]. You could give it a try like this :
Code:
ftp -u "ftp://myname:password@example.com/destination/directory/" filename.ext

Feherke.
 
Something like this ?
for file in $(ls); do
echo "cd /path/to/remotedir\nput $file\nquit" | sftp user@server
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks PHV I tried your solution but am getting a weird error ...

bash-2.05$ echo "cd basel\nput test.txt\nquit" | sftp sftpbasel@server
Connecting to server...
sftp > Couldn't canonicalise: Failure
sftp >
bash-2.05$

As you can see above, basel is my remote directory and I want to put the file test.txt in that directory.

Thanks again .... apologies .. not a UNIX expert at all
 
When you call sftp like this:
[tt]sftp sftpbasel@server[/tt]
how do you manually transfer test.txt to the basel remote directory ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

PHP's code is [tt]ksh[/tt]. For [tt]bash[/tt] you must add a -e option too the [tt]echo[/tt], otherwise the [tt]\n[/tt] will not be expanded to new line character.
Code:
echo [red]-e[/red] "cd basel\nput test.txt\nquit" | sftp sftpbasel@server

Feherke.
 
Works now - added the -e switch to my echo. thanks for the good advice PHV, not using the -e would obviously work in a normal shell.

Thanks feherke.
 
Personally I find scp more suitable than sftp when scripting transfers.

Annihilannic.
 
Guys you won't believe this but I'm so useless!!! What you've explained to me before works fine on the command line but my below script now doesn't work - it gives me the usage list every time I run it ..

Any ideas please :( - Sorry! I can at least confirm that is hasn't taken me all day to write this script.


#!/bin/ksh

for file in `ls *.txt`; do

ftpstring=`echo "cd basel\nput $file\nquit" | sftp sftpbasel@server

$ftpstring

done

exit
 
Looks like you're missing a closing backquote "`" after the sftp command.

Annihilannic.
 
Sorry no the back tick is there I've just missed it out when copying my text
 
Personally I would do it like this:

Code:
scp *.txt sftpasel@server:basel/

Annihilannic.
 
Thanks for the info Annihilannic - When I try it the command just hangs and nothing happens .... no copying taking place
 
In that case, try:

Code:
#!/bin/ksh

for file in *.txt; do
    echo "cd basel\nput $file\nquit" | sftp sftpbasel@server
done

Were you trying to save the sftp output in $ftpstring for any particular reason?

Annihilannic.
 
#!/bin/ksh
for file in $(ls *.txt); do
echo "cd basel\nput $file\nquit" | sftp sftpbasel@server
done

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top