3

I am new to unix. My client has a requirement to send some data files to a sftp server on regular basis.

On the SFTP server there are the same name files already existing. What they want is that when we sftp files to server, it will append the data of the file in the existing file.

Let's say on legacy server there is a file A.dat with 10 rows. Now I am doing sftp file A.dat with 5 rows. So after sftp, on the SFTP server file A.dat should have 15 rows.

Same with other files. Also, if the file doesn't exists on the SFTP system, then the script should place the file.

Any quick response is highly appreciated. My current sftp script looks like below, which is simply putting and overwriting the existing files.

#!/usr/bin/expect -d
set timeout -1
spawn sftp user@server
expect "sftp>"
send "cd /destinationpath\n"
expect "sftp>"
send "lcd /sourcepath\n"
expect "sftp>"
send "put A.dat\n" 
expect "sftp>"
send "exit\n"
interact

Can you pls help on updating this script as per the requirement.

What I got to know SFTP doesn't support append, then how this can be achieved using SSH?

1 Answers1

1

OpenSSH sftp client does not support appending.

  • You will either have to use another SFTP client.

  • Or resort to using a shell access like:

    cat /sourcepath/A.dat | ssh user@server "cat >> /destinationpath/A.dat"
    
  • Or, if you can keep a complete local file, even with the original contents, you can use reput command to "resume" the transfer of the file.