3

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

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

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

4

4 回答 4

6

由于您澄清了您不限于 ncftp,因此您应该使用lftp代替,它内置了对使用globand等操作的支持rm -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$

文档给出了完整的解释:

rm [ -r ] [ -f ]文件

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

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

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

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

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

于 2012-07-30T22:52:05.183 回答
4

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

运行命令

rmdir -r [文件夹]

于 2015-12-16T23:29:37.953 回答
1

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

rm -r *

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

于 2016-04-26T10:37:51.770 回答
0

这是一个执行删除的 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
于 2012-07-30T18:51:21.517 回答