Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have read through several threads (which have been helpful) and I'm still getting an error when trying to sftp a file (client wants it to be sftp'ed)

Here is the script:



spawn sftp -o Port=$PORT $USER@$HOST
expect "sftp>"
spawn put $FILE

The error I am receiving is:

spawn sftp -oPort=XXXX root@XX.XX.XX.XX
Connecting to XX.XX.XX.XX...
sftp> spawn put /tmp/RANDY
couldn't execute "put": no such file or directory
    while executing
"spawn put /tmp/RANDY"

The file exists and is in the correct directory.

share|improve this question

The second spawn in your Expect script should be send, you should quote what you're sending to the spawned process, and you've left off the CR (Carriage Return, a.k.a. Enter):

send "put $FILE\r"

Also, you probably don't want the interact command. This probably makes more sense in its place:

expect "sftp>"
send "exit\r"
share|improve this answer
Thank you. I changed the spawn to send: send [put $FILE] but i'm getting this error: sftp> invalid command name "put" while executing "put /tmp/RANDY" invoked from within "send [put /tmp/RANDY]" – Randy Dec 10 '12 at 19:29
this is the response i'm getting and the file is not being sent to the destination server: [root@local.host /tmp] # ./tmpsftp.sh spawn sftp -o Port=XXXX root@XX.XX.XX.XX Connecting to XX.XX.XX.XX... sftp> [root@ny4tarc-weber /tmp] # – Randy Dec 10 '12 at 19:59
Added interact fix. If it's still not working for you, Randy, update your question with the current version of the shell script and the current results. Carrying out the back-and-forth in comments is painful because you don't have complete Markdown support here. – Warren Young Dec 10 '12 at 20:23
my apologies. I'm a long time reader, 1st time asker. But either way that worked! that for all your help. I'll edit the original with the updates – Randy Dec 10 '12 at 20:27
@Randy: You've misunderstood. I meant you should edit the question only if my suggested fixes didn't fix everything, since you'd still have something to ask about. Editing it to have the final fixed version is just confusing, since my answer then doesn't address the question. So, I've reverted those edits. Now you can accept my answer, hint. :) – Warren Young Dec 10 '12 at 21:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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