Script to download structure of ftp-server

I decided to write this script to look at contents (filenames) of some our rich ftp-servers, without connection.

I spent many time in "perversions" with bash, until I discovered IFS variable :)

Here is my script:

get_listing:

#!/bin/bash
if [ "$1" == "" ]
then
  echo "Usage: `basename $0` server"
  exit
fi
server=$1
base=$PWD
IFS=$'\n'
function get_listing()
{
  local prev_dir=$PWD
  local url=
  if [ "$1" != "" ]
  then
     url=$1
     echo "Creating dir: $2"
     mkdir "$2"
     cd "$2"
  fi
  echo "Retreiving file list $url"
  curl ftp://$server$url/ 2 > /dev/null > listing
  lst=`cat listing | grep "^d" | grep -v "\.$" | xargs -I'{}' $base/get_fname "{}"`
  for f in $lst
  do
     get_listing "${url}/$f" "$f"
  done
  cd $prev_dir
}
get_listing

get_fname:

#!/bin/bash
echo `expr match "$1" '\\S*\\s*\\S*\\s*\\S*\\s*\\S*\\s*\\S*\\s*\\S*\\s*\\S*\\s*\\S*\\s*\(.*\)'`

I don't know how to insert get_fname into main script... I tried $() inside ``, vise versa, nothing helped...

blog comments powered by Disqus