0

I have a weird problem. I am trying to download a file from a ftp server to a different directory than what the bash script is running. If I do it manually everything works fine. I can connect type

get sampleFile.csv /path/to/directory/

Trying to put that in a bash script doesn't work. I get all the same output as doing it manaully

/ftp/sampleFile.csv 100%  384     0.4KB/s   00:01

but the file isn't in the directory or anywhere that I can find on my server. I have tried

sftp ***** << EOF
get sampleFile.csv /path/to/directory/
quit
EOF

and

cd /path/to/directory/
sftp ***** << EOF
get sampleFile.csv
quit
EOF    

and

sftp ***** << EOF
lcd /path/to/directory
get sampleFile.csv
quit
EOF    

All of these work doing it manually.

The only thing that works is

sftp ***** << EOF
get sampleFile.csv
quit
EOF 

This puts the file in the same directory the bash script is in and it shows up. Any ideas whats going on?

1
  • This is really strange. A shell script doesn't know or care about the directory it's stored in - it runs with the directory of the caller. Do you have a cd or chdir earlier in the script perhaps? – roaima Jul 9 '20 at 7:46
1

Add lcd path/you/want/to/save/to before the get line of your sftp script.

 ❯ sftp localhost
Connected to localhost.
# can't get sampleFile.csv because lpwd isn't what I think
sftp> get sampleFile.csv path/to/directory
File "/home/ryan/sampleFile.csv" not found.
sftp> lpwd
Local working directory: /home/ryan
# this will grab the file I want, but save it into `lpwd` again
sftp> get path/to/directory/sampleFile.csv
Fetching /home/ryan/path/to/directory/sampleFile.csv to sampleFile.csv
# pretend I want to save it locally in /tmp... so: lcd /tmp
sftp> lcd /tmp
sftp> get path/to/directory/sampleFile.csv
Fetching /home/ryan/path/to/directory/sampleFile.csv to sampleFile.csv

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.