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):


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


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


  • -u vivek : FTP login username
  • -p mypassword : Ftp password
  • : Ftp server name ( 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
$ echo $LIST


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"
for dir in $LIST
ncftp -u"vivek" -p"password" <<EOF
cd $rdir
rm *
rmdir $rdir

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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 2 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Shelon Padmore Apr 23, 2008 @ 13:33

    What exactly is ncftpls ?

    – Shelon Padmore

  • Cory Jan 4, 2010 @ 3:17

    I’m not sure if this is unique to my setup… but I was able to use ncftp and do:

    rm -fr /unempty-ftp-dir/

    from the ftp command line.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum