FTP Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsSystem AdministrationFTP Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 1st, 2012, 06:05 PM
psychobyte psychobyte is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 116 psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 19 h 46 m 52 sec
Reputation Power: 26
Ncftp recursive get all files then delete everything

Hi,

I'm using ncftpget to get all the files in a remote ftp directory tree, then delete all the files after they have been downloaded.

Code:
ncftpget -u username -p lamepass -R -DD ftp://10.0.1.14


This works great, except it leave all the empty directories intact.

Does anyone have a simple way of clearing out all these empty remote directories? with ncftp would be great but, I'd like to hear of any other way.

THanks,

Reply With Quote
  #2  
Old February 2nd, 2012, 01:31 PM
psychobyte psychobyte is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 116 psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level)psychobyte User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 19 h 46 m 52 sec
Reputation Power: 26
Code:
#!/bin/bash

# Script for retrieving all files on a an ftp server then deleting them.
#
# Requires ncftp and stock ftp client.
#
# We have to do some funkyness since there is no easy way of recursively deleting
#   remote directories.  We use ncftp to download all files and delete them on successfull
#   download. This ,however, leaves empty directories.  So we download the empty directory
#   tree to FSTREEDIR to list all directories to delete(we can't trust the download directory
#   because other directories may exist there). Those directories are then passed to the
#   usual ftp client to delete. 

# @todo - store credentials in a file

FTPSERVER=10.0.1.150
DOWNLOADDIR=/tmp/dl
FSTREEDIR=$DOWNLOADDIR/fstree
USERNAME=bart
PASSWORD=dude
DELETEREMOTEFILES=1


if [ $DELETEREMOTEFILES -eq 1 ]
 then
  DELFILESFLAG="-DD"
 else
  DELFILESFLAG=""
fi

echo "Downloading Reports...
"

cd $DOWNLOADDIR
ncftpget -u $USERNAME -p $PASSWORD -R $DELFILESFLAG ftp://$FTPSERVER


# Delete Files after download
if [ $DELETEREMOTEFILES -eq 1 ]
 then
    echo "Deleting Remote Reports...
    "
 
    RMSTRING=""
    
    # if fstree dir exists empty it and recreate it
    if [ ! -d "$FSTREEDIR" ]; then 
      mkdir $FSTREEDIR
    else
      rm -rf $FSTREEDIR/*
    fi

    # Copy remote directory structure to FSTREEDIR
    cd $FSTREEDIR
    ncftpget -u $USERNAME -p $PASSWORD -R $DELFILESFLAG ftp://$FTPSERVER

    # Generate list of directories to delete
    for D in `find $FSTREEDIR -type d| sort -r`
    do
      if [ ! "$D" = "$FSTREEDIR" ]; then
        RMSTRING="$RMSTRING 
        rmdir ${D#$FSTREEDIR/}"
      fi
    done

# Delete remote file structure
ftp -i -n <<EOF
open $FTPSERVER
user $USERNAME $PASSWORD
$RMSTRING
EOF

    # delete old FSTREEDIR
    rm -rf $FSTREEDIR

fi

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationFTP Help > Ncftp recursive get all files then delete everything

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap