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

Posted on 2011-09-29
Medium Priority
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 35

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.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 35

Accepted Solution

Duncan Roe earned 500 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 1500 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 1500 total points
ID: 36899670
Works for me:
>cat sftp.test | sftp test.host -b
Connecting to test.host...
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 1500 total points
ID: 36910134
This is an official openssh sftp client feature. I quoted here 'man sftp'.

Author Closing Comment

ID: 36911217

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

850 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