5

This is part of a SFTP file transfer.

Here are the steps:

  1. Check files with .csv extension and get them to the local directory.
  2. After that, move them to the another folder in the remote connection.

Tried using the rename command but it throws an error "Failure"

Tried using -b batch-file option with sftp but looks like rename command needs a specific file-name instead of a set of files.

So how do I achieve this ?

2
  • Do you have permissions to remove the file? Do you have permissions to put the file in the remote new folder? Commented Aug 12, 2015 at 15:12
  • yes I do. I was not giving the destination filename and thats the reason it was failing Commented Aug 13, 2015 at 15:00

4 Answers 4

9

Works for me, so you will need to provide more information about the problem:

chris@localhost$ finger 2> file.txt
chris@localhost$ sftp remotehost
Connected to remotehost.
sftp> ls -l file.txt
Can't ls: "/home/chris/file.txt" not found
sftp> ls -l file.tmp
Can't ls: "/home/chris/file.tmp" not found

# So the file doesn't exist on the remote in either form

sftp> put file.txt file.tmp
Uploading file.txt to /home/chris/file.tmp
file.txt                                                      100%  501     0.5KB/s   00:01
sftp> ls -l file.txt
Can't ls: "/home/chris/file.txt" not found
sftp> ls -l file.tmp
-rw-r-xr--    0 1001     1001          501 Aug 12 16:35 file.tmp

# It has arrived as file.tmp

sftp> rename file.tmp file.txt
sftp> ls -l file.txt
-rw-r-xr--    0 1001     1001          501 Aug 12 16:35 file.txt
sftp> ls -l file.tmp
Can't ls: "/home/chris/file.tmp" not found

# And been successfully renamed
6
  • Thanks , But when i try to pass as arguments, it doesnt read the filename from a while loop. Commented Aug 12, 2015 at 17:11
  • When i use this rename "$Remote_server_source_path/$filename" "$Dest_path/$filename" Commented Aug 12, 2015 at 17:11
  • Sorry, i posted the comment before it was complete. Here is the code i wrote Commented Aug 12, 2015 at 17:12
  • cat tempfilelist | while read line do #echo $line echo "Filename to be moved : " filename= echo $line | awk '{print $9}' echo $filename sftp $LOGIN << EOF cd $Remote_server_source_path pwd rename "$Remote_server_source_path/$filename" "$Dest_path/$filename" bye EOF done Commented Aug 12, 2015 at 17:12
  • @shravani please update your question to include this new information. Otherwise it can get too spread out and lost in the comments. Commented Aug 13, 2015 at 13:31
1

Apparently the rename command in sftp does NOT work when the source and target are on different file systems.

I see this behaviour in RedHat6, SLES9, and more.

0

This might not exactly be what the OP wanted however, I found sftp !command to be helpful for "renaming files in SFTP session".

  • From sftp help: "!command - Execute 'command' in local shell"

Here's my usage from SFTP session - (Local: macOS Catalina, Remote: Ubuntu 16.04.6 LTS)

sftp> lls
blankFileForVSCODEbrowser.txt   counties
sftp> !command mv  blankFileForVSCODEbrowser.txt blankFileForVSCODEbrowserRenameTest.txt
sftp> lls
blankFileForVSCODEbrowserRenameTest.txt counties
0

in bash shell you can try doing following (can also shrink it to oneliner):

Renaming all csv files to txt on remote sftp $server/$dir (assuming passwordless access):

for f in `echo 'ls *.csv' | sftp -qp $user@$server:$dir | grep -v ^sftp`; do
    echo "rename '$f' '${f%.csv}.txt'" | sftp -qp $user@$server:$dir ;
done

or

# to move to $newpath folder:
for f in `echo 'ls *.csv' | sftp -qp $user@$server:$dir | grep -v ^sftp`; do
    echo "rename $f $newpath/${f##*/}" | sftp -qp $user@$server:$dir ;
done

where

  • echo 'ls *.csv' | sftp $server:$dir - runs ls on remote sftp in dir $dir
  • ${f%.csv}.txt - replaces csv with txt in variable $f
  • ${f##*/} - remove path from filepath $f (just in case)

P.S. if you need to enable interactive entering of password - try adding -o "BatchMode=no" argument to sftp

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.