How to script downloading multiple files from remote server
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:-
Code:
lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "ls /home/adm/testfiles|grep .out; bye" >> $LOG
if [ $? -eq 0 ]
then
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
else
echo ".out files do not exist" >> $LOG
fi
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. It is not necessary to be in lftp. It is just that is only I know.
# something like this
lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "ls /home/adm/testfiles" > local_list
next I would try to analyse that list and find out what to download and what should be deleted.
finally execute that.
To process that local_list file I suggest you to use awk/perl/python/whatever, what is your preferred language?
Try not to use sftp with username & passwd in code - fiddly and asking to be hacked since creds are in the clear.
Instead, use ssh-keys with scp to copy files and ssh to get any other info.
If there are other files in the dirs apart from the pairs, you could use ssh to list .out files, then for each filename look for the matching .txt and scp it down .
In fact (faster/easier), just list the entire dir down (filenames) into a file and process list locally, then just fetch any matching pair's .txt if it's in the list.
I'd personally use Perl (which also has ssh modules), but bash could do it as well, prob with help from awk or sed etc.
# something like this
lftp sftp://$UserID:$PASSWORD@10.x.x.x -e "ls /home/adm/testfiles" > local_list
next I would try to analyse that list and find out what to download and what should be deleted.
finally execute that.
To process that local_list file I suggest you to use awk/perl/python/whatever, what is your preferred language?
I only know bash but basic only. Perl/python is beyond my knowledge. Any language would be fine as long as I can achieve the objective.
Try not to use sftp with username & passwd in code - fiddly and asking to be hacked since creds are in the clear.
Instead, use ssh-keys with scp to copy files and ssh to get any other info.
If there are other files in the dirs apart from the pairs, you could use ssh to list .out files, then for each filename look for the matching .txt and scp it down .
In fact (faster/easier), just list the entire dir down (filenames) into a file and process list locally, then just fetch any matching pair's .txt if it's in the list.
I'd personally use Perl (which also has ssh modules), but bash could do it as well, prob with help from awk or sed etc.
HTH
How to do scripting for the matching? How I do I know that file1.out is pair/match with file1.txt, file2.out is pair of file2.txt. This is what I am wondering now. Sorry, I know basic bash scripting only. Perl/Python is beyond my knowledge.
For a start, list the .out filenames into one filelist .txt filenames into another file, then read through the .out filelist and use eg grep to search for corresponding .txt file in the other filelist.
Stuff I used to do in the days of dialup. Build up my list and hit up the broadband at the truck stop / rest stop / library until the battery on my laptop died.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.