About nixCraft

Topics

How to nuke a FTP Server

Posted by Vivek Gite [Last updated: August 13, 2006]

You can nuke whole directory or sub-directory tree by using rm -rf command with -rf flags under UNIX or Linux. But ftp server does not gives rm -rf command. For ftp server you use the rmdir command to remove a directory (and rm for files). Depending on the remote server, you may be able to remove a non-empty directory.(most remote ftp server disables this option)

For example, consider my following ftp directory server structure (I do not have ssh access to this FTP box):

ccbackup/
ora9backup/
usershotdata/
vivek/
  |-Junk
  |-mp3
  |-02/09
  |-03/09
  |....
...
linux-iso/
openbsd-ports/
freebsd-ports/
tosbackup/
cnowbackup/
...

Now let us say I want remove all files and sub-directories under /vivek/ directory. There are least 100 subdirectories and deleting all files and directory manually will take lots of time.

To nuke a vivek/ directory, you need to use ncftpls command to obtain the list of all subdirectories:
$ ncftpls -u vivek -p mypassword ftp://array05.wan.nixcraft.com/vivek/

Output:

Junk/  mp3/ 02/09/ 03/09/   ...

Where,

Now you can store a list of all sub-directories in a shell variable called LIST. Type the following command at shell prompt:
$ LIST=$(ncftpls -u vivek -p mypassword ftp://array05.wan.nixcraft.com/vivek/)
$ echo $LIST

Output:

Junk/  mp3/ 02/09/ 03/09/ ….

To remove all sub-directories from /vivek/ dir you can write a shell script as follows:
LIST="$(ncftpls -u vivek -p mypassword ftp://array05.wan.nixcraft.com/vivek/)"
for dir in $LIST
do
rdir="/vivek/${dir}"
ncftp -u"vivek" -p"password" array05.wan.nixcraft.com <<EOF
cd $rdir
rm *
rmdir $rdir
quit
EOF
done

How it works?

  1. ncftp ftp client login to my ftp server.
  2. Script will change directory to $rdir
  3. First, it will remove all files
  4. Next, it will remove subdirectory
  5. And ftp connection will terminated
  6. The entire cycle is repeated using a shell for loop construct.

You can obtain complete working and generic version here. You can use logic presented here to write a shell script for sftp server. You can modify this script rotate ftp backups and much more.

E-mail this to a Friend    Printable Version

Ubuntu / Kubuntu Ubuntu Ver. 8.10 and Linux Training Library 2DVD+CD

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Shelon Padmore Says:

    What exactly is ncftpls ?

    - Shelon Padmore

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.