【问题标题】:Command to delete all files and folders in a folder using ncftp使用ncftp删除文件夹中所有文件和文件夹的命令
【发布时间】:2012-07-26 10:25:41
【问题描述】:

我需要清除远程服务器上的一个文件夹。我需要删除此文件夹中的所有文件和文件夹。我不能删除并重新创建父文件夹,因为我不想弄乱权限。

例如: 远程文件夹是 Development/
该文件夹包含多个文件和多个文件夹。
我想运行一个命令来完全清空 Development/ 文件夹并给我一个新的空版本。

我还需要它与 Windows FTP 客户端兼容。

【问题讨论】:

  • 您是否绝对需要使用 ncftp,或者任何 ftp 程序就足够了?
  • 我实际上将它用于 TeamCity 构建,因此任何兼容的 FTP 替代方案都会很棒。

标签: ftp windows-server


【解决方案1】:

这是一个执行删除的 bash 脚本。

#!/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

【讨论】:

  • 我正在运行一个 Windows 服务器,所以我更喜欢更原生的东西。
【解决方案2】:

由于您明确说明您不限于 ncftp,您应该使用 lftp 代替它,它内置了对使用 globrm -r 的操作的支持。这是一个完整的演示:

~/ftptest$ find .    # Test folder with a number of files and directories in it.
.
./dir1
./dir1/subdir1
./dir1/subdir1/subsubfile1
./dir1/subfile1
./dir2
./file1
./file2

~/ftptest$ lftp localhost    # Connect
Password:
lftp blahdiblah@localhost:~> cd ~/ftptest/    # cd to test folder
cd ok, cwd=/Users/blahdiblah/ftptest

lftp blahdiblah@localhost:~ftptest> ls    # The files are there...
total 0
drwxr-xr-x  4 blahdiblah  staff  136 Jul 30 15:40 dir1
drwxr-xr-x  2 blahdiblah  staff   68 Jul 30 15:40 dir2
-rw-r--r--  1 blahdiblah  staff    0 Jul 30 15:40 file1
-rw-r--r--  1 blahdiblah  staff    0 Jul 30 15:40 file2

lftp blahdiblah@localhost:~/ftptest> glob -a rm -r *    # the magic happens...
rm ok, 7 files removed

lftp blahdiblah@localhost:~/ftptest> bye
~/ftptest$ find .    # ...and then they're gone!
.
~/ftptest$

The docs给出完整解释:

rm [-r] [-f] 文件

删除远程文件。不扩展通配符,使用 mrm 。 -r 用于递归目录删除。请注意,如果出现问题,您可能会丢失文件。 -f 禁止显示错误消息。

glob [-d] [-a] [-f] 命令模式

给定包含元字符的模式并将结果传递给给定的命令。例如
glob echo *

-f 普通文件(默认)
-d 目录
-a 所有类型

(请注意,mrm 在这种情况下不可用,因为它不会扩展 * 以包含目录。)

【讨论】:

  • 看起来 lftp 与 Windows 服务器不兼容?这就是我正在运行的。
  • @MikeC。四处寻找,我看到了一些用于 Windows 的 lftp 构建,以及使用 Cygwin 构建它的说明,所以它仍然应该是您的选择,但需要一些额外的工作。
  • 好的,我去看看。谢谢!
【解决方案3】:

使用 ftp 服务器登录 ncftp -u [user.ftp] [backup.server]

运行命令

rmdir -r [文件夹]

【讨论】:

    【解决方案4】:

    为了直接解决 OP 的问题,以下命令完全符合要求:

    rm -r *
    

    确保导航到要删除内容的目录并发出命令。它会删除当前目录中的所有目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      相关资源
      最近更新 更多