Balaji
Balaji

Reputation: 89

SHELL script to make ftp connection and get xml files

I need a shell script that will login to a remote FTP server, get the list of files present in only root folder and identify only xml files and get those files to local system. Login credentials can be mentioned in the script it self. This script must be run once a day only.

Please help me with a UNIX BASH SHELL script.

Thanks

Upvotes: 0

Views: 2797

Answers (2)

evil otto
evil otto

Reputation: 10582

If you can install ncftpget, this is a one-line operation:

ncftpget -u user -p password ftp.remote-host.com /my/local/dir '/*.xml'

Upvotes: 0

user478681
user478681

Reputation: 8329

script:

#!/bin/bash
SERVER=ftp://myserver
USER=user
PASS=password
EXT=xml
DESTDIR=/destinationdir

listOfFiles=$(curl $SERVER --user $USER:$PASS 2> /dev/null | awk '{ print $9 }' | grep -E "*.$EXT$")
for file in $listOfFiles
do
   curl $SERVER/$file --user $USER:$PASS -o $DESTDIR/$file
done

for scheduled run every day check the crontab:

crontab -e

for edit your current jobs and add for example:

0 0 * * * bash /path/to/script

that will mean run the script every day at midnight.

Upvotes: 2

Related Questions