Renaming multiple files in sftp server in a get files script

Hi,

In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this?
I am using #!/bin/ksh

For eg:

 sftp user@host <<EOF
           cd /path
           get *.txt
           rename *.txt *.txt.done
           EOF

How can I achieve this?

This would have to be done in two steps:

( echo cd /path; echo get \*.txt ) | sftp -b - user@host
ls -1 | sed -ne '1icd /path' -e 's/^.*\.txt$/rename & &.done/p' | sftp -b - user@host

I did not understand these commands. Please advise.
Also, is there any easier way to do this?

Did you run each step of the pipeline? You also might want to check the manpages for man sed (linux) and man sftp (linux)

( echo cd /path; echo get \*.txt )

Generates

cd /path
get *.txt

, which is read by man sftp (linux). After the files have been retrieved, you need to create a series of renames.

ls -1 | sed -ne '1icd /path' -e 's/^.*\.txt$/rename & &.done/p'

does that, but this could have been done equally as well as:

(
  echo cd /path
  ls -1 *.txt | awk '{print "rename", $0, $0 ".done";' }
)

or

(
  echo cd /path
  for file in *.txt; do
    echo rename ${file} ${file}.done
  done
)

The two steps also prevents you from renaming a file that was not retrieved, had rename *.txt *.txt.done worked as you intended.

Hi,

I understood the awk command but not the sed. Anyways, I tried using both the commands, but they are not renaming the files on remote server, even though the files are fetched.

Please give me another solution

Please provide the script(s) you used and errors displayed.

I suspect you do not have write permissions to the file (or containing folder) on that server.

Hi,

I also had a similar requirement and found that we cannot rename multiple files in the remote server using wild character and below are the steps which i followed to achieve this.

  1. First you need to receive the files.
  2. Generate rename command for the files which you have received.
  3. Again connect to the remote server passing the rename command as input.

Sample code which I used:

#!/usr/bin/ksh  
###The below shell script is used to receive and rename files in remote server, and below is the steps followed.  
###First you need to receive the xml files.  
###Generate rename command for the files which you have received.  
###Again connect to the remote server passing the rename command as input.  
  
# Shell script to receive xml files from remote machine #  
cd /home/oracle/XML_FILES/   ## local directory  
  
#Clear renamescript.txt file at each execution#  
cat /dev/null > /home/oracle/FTP_SCRIPTS/renamescript.txt  
sftp userid@remote_server <<END  
cd /home/user/XML  ## remote directory  
prompt  
mget *  
bye  
END  
  
# For generating rename script #  
ls /home/oracle/XML_FILES/ > /home/oracle/FTP_SCRIPTS/count  
for i in `cat /home/oracle/FTP_SCRIPTS/count`  
do  
{  
print `echo rename /home/user/XML/$i /home/user/XML_BACKUP/$i` >> /home/oracle/FTP_SCRIPTS/renamescript.txt  
};  
done  
  
# To rename files in remote machine #  
sftp userid@remote_server < /home/oracle/FTP_SCRIPTS/renamescript.txt  
bye  
#End of script  

Thanks,