sftp & Openssh limitation : can't rename file via ssh

Posted on 2011-09-29
Last Modified: 2012-05-12

From my ssh client (on a Linux box) I would sftp to a remote Windows
2003 box (running Openssh server).

However using Shell scripts,
  sftp remoteid@remote_IPaddr
  sftp> rename file1 file1.bak
  sftp> put file1

However, the rename command would fail if file1 does not
exist on the remote Windows end & this would cause the
subsequent commands (including the put) to fail.

Alternatively, I can code my script to do as follows :
  ssh remoteid@remote_IPaddr 'cd directory; rename file1 file1.bak'
  but the above rename fails when coded as in the above in my
  Linux shell script : it says command not found but this rename
  command works (ie managed to rename file1 if I ssh manually
  into the remote server to issue it manually;  not if it's automated
  in a script).

I've found a GNU utility mv.exe (for Windows) which would allow my
Linux Shell script to work ie
  ssh remoteid@remote_IPaddr 'cd directory; mv file1 file1.bak'
But alas, due to security reason, the remote end (which is a
military related organization) won't allow freeware / GNU /
Open source tools to be installed/loaded there

So what other options do I have now to automate remote renaming
of file via sftp/scp/ssh ?
Question by:sunhux
  • 7
  • 4
  • 2

Author Comment

ID: 36816170

Also,  the following would fail if file1. bak already exists
& the failed operation would cause all subsequent sftp
commands/operations after it to fail:
sftp> rename file1 file1. bak
LVL 34

Expert Comment

by:Duncan Roe
ID: 36891089
You need to query whether the file exists before trying to rename it. This is not an sftp function per se, but I would do it by driving sftp via an expect script.

Expert Comment

ID: 36894945
Why would failure in rename command cause entire sftp to fail? It doesn't for me.
There is also an 'ls' command in sftp that specifically tests for file's presence.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 34

Accepted Solution

Duncan Roe earned 125 total points
ID: 36895085
Can you post actual output? (especially from the script)
For the script, insert the following 2 lines at line 2 (straight after #!/bin/sh)
exec >> /tmp/script.out 2>&1
set -xv

Open in new window

Run the script and then post /tmp/script.out

Author Comment

ID: 36897093

Yes I know there's ls command in sftp but can parparov give exact sample Linux
Shell script on how to test this?  How would you pass the ls test result within
sftp to the next sftp command to test before executing the rename?

Author Comment

ID: 36897168


Below is the content of script.out :

       echo "lcd /tmp"

      echo "rename nonexist.txt nonexist.txt.bak"
      echo "put exist.txt"
      echo "quit"
      } | sftp -b - remoteid@

      *** Warning: Unauthorized users, logoff now ! ***

sftp> lcd /tmp
sftp> rename nonexist.txt nonexist.txt.bak
Couldn't rename file "/home/xxxx/nonexist.txt" to /home/nspd/nonexist.txt.bak": No such file or directory

Author Comment

ID: 36897174

The "put exist.txt" was not executed due to the rename failure.

So, Parparov, in my Linux sftp client (to Windows Openssh) case, everything else after an error statement don't get executed

Assisted Solution

parparov earned 375 total points
ID: 36897487
Ok, you're using sftp -b.

From the manual:
 -b batchfile
             Batch mode reads a series of commands from an input batchfile
             instead of stdin.  Since it lacks user interaction it should be
             used in conjunction with non-interactive authentication.  A
             batchfile of `-' may be used to indicate standard input.  sftp
             will abort if any of the following commands fail: get, put,
             rename, ln, rm, mkdir, chdir, ls, lchdir, chmod, chown, chgrp,
             lpwd, df, and lmkdir.  Termination on error can be suppressed on
             a command by command basis by prefixing the command with a `-'
             character (for example, -rm /tmp/blah*).
That means you need to do
-rename file1 file2

Open in new window

instead of
rename file1 file2

Open in new window

Try it.

Author Comment

ID: 36898817

Ok, thanks;  the "-rename"  syntax works ie it won't fail subsequent
sftp commands if file1 doesn't exist.  However, if file2 exists, "-rename"
doesn't allow file1 to be renamed/overwritten to file2 :

I'm quite sure I used the same remoteid to sftp put to the remote WIndows
Openssh server & I used the same remoteid to "-rm file2" in sftp as well:

sftp> lcd /tmp
sftp> -cd /nonexist
Couldn't canonicalise: No such file or directory
sftp> -rm nonexist.txt.bak
Removing /home/nspd/nonexist.txt.bak
sftp> -rename nonexist.txt nonexist.txt.bak
Couldn't rename file "/home/nspd/nonexist.txt" to "/home/nspd/nonexist.txt.bak": Failure
sftp> put exist.txt nonexist.txt
Uploading exist.txt to /home/nspd/nonexist.txt
Couldn't get handle: Failure


Assisted Solution

parparov earned 375 total points
ID: 36899670
Works for me:
>cat sftp.test | sftp -b
Connecting to
sftp> -cd /tmp
sftp> -rm TODO.old
Couldn't stat remote file: No such file or directory
Removing /tmp/TODO.old
Couldn't delete file: No such file or directory
sftp> -rename TODO.txt TODO.old
Couldn't rename file "/tmp/TODO.txt" to "/tmp/TODO.old": No such file or directory
sftp> put TODO TODO.txt
Uploading TODO to /tmp/TODO.txt
TODO                                          100%  287     0.3KB/s   00:00    

Open in new window


Author Comment

ID: 36908832

Thanks, it's just that particular file only;  subsequent files that I put over
could be rm'ed.

Last question:
Btw, is this "-" (eg: -rename, -rm, ...) feature something that's officially
supported in sftp or sftp RFC ?  I can't find this feature in Linux man
pages & just wanted to be sure it will continue to be there when we
migrate to Solaris 10 sftp client or Redhat 5.x sftp client (in the next
few months' time)

Assisted Solution

parparov earned 375 total points
ID: 36910134
This is an official openssh sftp client feature. I quoted here 'man sftp'.

Author Closing Comment

ID: 36911217

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Recently, an awarded photographer, Selina De Maeyer (, completed a photo shoot of a beautiful event ( in An…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question