Renaming a file while using SFTP


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming a file while using SFTP
# 1  
Old 11-14-2011
Renaming a file while using SFTP

Hi All,
I have a script which works perfectly for using SFTP commands.

I use SFTP -b batchfile remoteuser@remoteserver.

This batch file contains a command

cd /dir1
put file1

I am wondering if there is a flag to rename a file on Remote server.

I see the option
rename file1 file1_old in SFTP

This works fine if file1 exists on Remote Server. If it does not then it errors out.

I did not see any force flag on rename command, so any idea on how to FIX IT??

Smilie
# 2  
Old 11-14-2011
'man sftp' yields:
Code:
          put [-P] local-path [remote-path]
               Upload local-path and store it on the remote machine.
               If the remote path name is not specified, it is given
               the same name it has on the local machine.  local-path
               may contain glob(3) characters and may match multiple
               files.  If it does and remote-path is specified, then
               remote-path must specify a directory.  If the -P flag
               is specified, then the file's full permission and
               access time are copied too.

put file1 file1_old should work - give it a go.
# 3  
Old 11-14-2011
Yes I agree if I use

put file1 file1_old

then it will copy file1 from current server to file1_old on remote server.

What I am trying to accomplish is if file1_old exists on remote server then rename it before copying.

so

cd /dir1
rename file1_old file1_old2
put file1 file1_old

Rename command works if file1_old exists on remote server.

If it does not exist then SFTP fails.
# 4  
Old 11-14-2011
Quote:
Originally Posted by Hangman2
Yes I agree if I use

put file1 file1_old

then it will copy file1 from current server to file1_old on remote server.

What I am trying to accomplish is if file1_old exists on remote server then rename it before copying.

so

cd /dir1
rename file1_old file1_old2
put file1 file1_old

Rename command works if file1_old exists on remote server.

If it does not exist then SFTP fails.
the rename operation fails, not the entire SFTP session (IIRC).So if rename fails => there's nothing to rename => put file1 file1_old.
If rename succeeds => do the same (put file1 file1_old).

I guess I'm missing something obvious.....
# 5  
Old 11-14-2011
By default in batch mode sftp commands will terminate when an error is encountered. Per the man page, this behavior can be changed by prefixing the command with a "-":

Code:
 -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*).

These 2 Users Gave Thanks to in2nix4life For This Post:
# 6  
Old 11-14-2011
Quote:
Originally Posted by in2nix4life
By default in batch mode sftp commands will terminate when an error is encountered. Per the man page, this behavior can be changed by prefixing the command with a "-":

Code:
 -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*).

Ah, good to know - thanks.
Missed the 'batch mode' bit from the OP description.
# 7  
Old 11-14-2011
Smilie

Works like a charm.......

Thanks in2nix4life. Now I know why I did not get "A" grades. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error while renaming the file in SFTP Session

Hi All Below is the script I am trying to execute to rename a file in an SFTP session. It is in GNU/Linux. This script is for generic use and so I am passing arguments. Everything in the script works fine except "reading the filename in SFTP session" In the below script the... (2 Replies)
Discussion started by: sparks
2 Replies

2. Shell Programming and Scripting

SFTP batch not renaming file with "put"

I have a .ksh script that creates an sftp batch file and runs it through sftp. It works except for one thing. If I try to "put" to a different name, it doesn't use the specified remote name...it still "puts" the original local name. I've tried both of these, and neither work...it will always... (4 Replies)
Discussion started by: dbiggied
4 Replies

3. Shell Programming and Scripting

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 ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. Shell Programming and Scripting

Renaming the file

Hi guys, I have written a code to move my file from one directory to another directory, the file is in .csv format and i need to append a current date to the file. the prolem is date is getting appended after the file extension.. here is my code : cd /data/home/abc/xyz now=$(date... (7 Replies)
Discussion started by: azherkn3
7 Replies

5. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

6. Solaris

sftp file renaming

Hi Admins, I am trying to get a file with different name including date format using sftp. For example --> get test.bak auto`date +"%Y%m%d"`err Here i am trying to get the file test.back with name auto20120126err But file is copied with the name auto`date --> date is not printing... (1 Reply)
Discussion started by: newaix
1 Replies

7. Shell Programming and Scripting

bulk renaming of files in sftp using script

Hi, Am using sftp (dsa method) to transfer 20 files from one server(sftp) to another (local). After the transfer is complete the files in the sftp server has to be renamed from .txt extension to .done extension ( aa.txt to aa.done, bb.txt to bb.done and likewise...). I tried rename command... (4 Replies)
Discussion started by: Sindhuap
4 Replies

8. Shell Programming and Scripting

File renaming from list of names contained in another file

I have to rename a large number of files so that the name of each file corresponds to a code number that is given side by side in a list (textfile). The list contains in column A the filename of the actual files to be renamed and in column B the name (a client code, 9 digits) that has to be... (7 Replies)
Discussion started by: netfreighter
7 Replies

9. Shell Programming and Scripting

Renaming a file use another file as a sequence calling a shl

have this shl that will FTP a file from the a directory in windows to UNIX, It get the name of the file stored in this variable $UpLoadFileName then put in the local directory LocalDir="${MPATH}/xxxxx/dat_files" that part seems to be working, but then I need to take that file and rename, I am using... (3 Replies)
Discussion started by: rechever
3 Replies

10. Shell Programming and Scripting

Renaming a file

I am a complete Unix newbie and I need some help! (Please...) I need to rename a file from the following format: Test_Test_EAR_1234.ear To the following: Test_Test_EAR.ear In other words, I need to remove everything after & including the final underscore up to the "." What is the best... (6 Replies)
Discussion started by: VeloLisa
6 Replies
Login or Register to Ask a Question