In remote server, there are always a pair of files like:-

in folder /Pair1

IN_S-WW001_20160418100715.out IN_S-WW001_20160418100715.txt

in folder /Pair2

ar.MON.20160226.output.ZZ.out ar.MON.20160226.output.ZZ.txt

This is just an example of files. There will be a lot of them generated in one day.

File name in a pair is always same except for timestamps. From other server, I need to SFTP to the remote server and check one pair by one pair.

Logic: The script should check if .out file is there, then grab/download the .txt file (from the pair). Once the .txt file is transferred/downloaded successfully, script will go back to remote server and delete the .out file.

This is my test script:-

lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "ls /home/adm/testfiles|grep .out; bye" >> $LOG
if [ $? -eq 0 ]
echo "Files exist, getting *.txt" >> $LOG
lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "mget /home/adm/testfiles/*.txt -O /home/adm/test/ ; bye" >> $LOG
echo ".out files do not exist" >> $LOG

The problem is that, it will list out all files with extension .out instead of checking pair by pair. I do not know how to classify a pair. I am not good in scripting, so, I have to ask for some help.


A for loop that loops round the .out files should work. The following is not tested as I'm unfamiliar with lftp and its output.

for file in $(lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "ls /home/adm/testfiles|grep .out; bye")
  echo "Found .out file: $file"
  TXTFILE=$(echo $file | sed -e 's/.out$/.txt/')
  lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "mget /home/adm/testfiles/$TXTFILE -O /home/adm/test/ ; bye"
  # Do any deleting here
  • Thanks @Unbeliever. You gave me some idea to proceed further – user240614 Aug 25 '16 at 3:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.