How to nuke a FTP Server
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,
- -u vivek : FTP login username
- -p mypassword : Ftp password
- ftp://array05.lan.nixcraft.com/vivek/ : Ftp server name (array05.wan.nixcraft.com) followed by path (/vivek/).
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?
- ncftp ftp client login to my ftp server.
- Script will change directory to $rdir
- First, it will remove all files
- Next, it will remove subdirectory
- And ftp connection will terminated
- 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:
- How To: Double Linux disk read performance with readahead parameter
- Tuning MySQL server under Sun Solaris OS
- MySQL avoid unauthorized reading and SQL Injection vulnerabilities in PHP
- Power Off Server Once In a While?
- How to setup Linux antivirus and antispam mail server
Discussion on This Article:
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!


What exactly is ncftpls ?
- Shelon Padmore