Has anyone tried to access and manipulate files in an SFTP server using shell script Basically, here's what I have to do: 1. Open SFTP, access the designated directory 2. Loop through all .txt files 3. Read filename of each .txt files and only get the file/s that contain/s 'XX' substring (i.e. hello-XX.txt) 4. Rename that file by appending an '-OK' string (i.e.hello-XX-OK.txt)
Thanks for your inputs.
Nothing in your description requires you to run any logic on the sftp server. You can just get the data using the sftp command and do the processing locally, e.g.:
You can script SFTP with a "here document" (
<<EOF
, as specified here), but you won't be able to use shell-script-like primitives (a la bash) in an SFTP script.Perhaps try an SSH-based script instead, if you have SSH access.
Here's a starting point that I got working for my own purposes. I'm using
sshpass
so that the password can be specified but you should be able to remove that extra goo if you have the RSA public keys properly exchanged.What this will do is list all the files on the server in the starting directory.
This is what expect is for. Roughly:
Let's use Perl for that: