Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Secure Shell (SSH) is a network protocol for secure data communication, mainly used to administer remote Unix / Linux servers via command line. But it also allows the user to open a secure tunnel between a client and a server where he can send any k…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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

610 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