SFTP Get command fails in Shell Script and works fine Manually

I'm trying to get a file using SFTP command in the shell script and it fails with "Cannot download non-regular file:". Below is the piece of code I have written.

sftp -v -F /usr/home/.ssh/sftp.config <user>@<servername> <<EOF2
cd <targetdirectory>
mget $File

Also when I try to list the files in the shell script, it gives "Couldn't get handle: Failure" error.

However, If I do the same operation manually(SFTP to get the file), I'm able to get the file successfully without any errors.

Very much confused as why the SFTP command in shell script is not allowing to download the file whereas the same set of commands when manually run works.

Any inputs is greatly appreciated. Thank you so much!!

Vijay MalleshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Unfortunately you can't do a recursive get with sftp. You should use scp instead.

scp -r user@host:/your/path/ .

Open in new window

Be sure to always use the absolute path and don't forget the '.' at the end, it is the destination directory.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.