Sftp using batchfile - storing result local

I'm making an sftp-connection to a remote server. I want the result of an ls-command in a local file and the result of ls on another folder in another local file. Because everything has to go as fast a possible I wan't to do everyting in one connection.

The command I use is : psftp -v -batch -b batchfile.ftp @ACCOUNT@remoteserver

The batchfile.ftp contains the following :

lcd *local directory*
cd folder1
ls *.txt filename1.lst
cd ../folder2
ls *.txt filename2.lst
bye

running the command doesn't give any errors. The results are displayed in the terminalwindow. But the local files aren't build. I want to use these commands in a script and need the 2 files. Can anybody help? How can I create these files using 1 connection. (redirecting the output of the psft-command isn't a solution because I need 2 seperate files.)

sftp does not work that way, it is not a shell and doesn't have shell features like file redirection.

Why not split the file afterward?

I'm trying to solve my problem this way now but I'm stuck on the following :
How can I split a file in different files using variable linenumbers?
I have a variable "begin" that contains the linenumber where the split should start and a variable "end" that contains a linenumber where the split should end.
My code :

sed -n $begin,$start 'p' > file1.txt

doesn't work. I tried different variations of this with " ' / ....
Can you help me with this command? On the internet there are a lot of solutions to this problem but so far nothing works.

---------- Post updated at 11:18 ---------- Previous update was at 11:02 ----------

who searches, finds ... :slight_smile:

sed -n "$begin,$end"p inputfile > outputfile

Look at lftp as an alternative to sftp. It is scriptable, can be used for various protocols including ftp, sftp and http, and ls can be redirected! So:

sftp stfp://joe@remote.com
cd dir1
ls > file1.txt
cd ../dir2
rels > file2.txt

should do what you want. Note the use of rels as ls is cached (although it may not matter if you change directory - best to experiment).

lftp may not be there by default, but it will be in the repository if you are using a Linux distro.

Andrew

Depending on your versions of sftp, you may need to adapt this:

psftp -v -batch -b batchfile.ftp @ACCOUNT@remoteserver | 
awk     '/^sftp> cd/    {fn=$NF; next}
         /^sftp>/       {P=0}
         P              {print > fn}
         /^sftp> ls/    {P=1}
        ' 

In any case, you need to split the cd ../folder2 into cd..; cd folder2 to make above work.