3

リモート サーバー上にクリアする必要のあるフォルダーがあります。このフォルダ内のすべてのファイルとフォルダを削除する必要があります。アクセス許可を台無しにしたくないため、親フォルダーを削除して再作成することはできません。

例: リモート フォルダーは Development/
そのフォルダーには複数のファイルと複数のフォルダーが含まれます。
コマンドを実行して Development/ フォルダーを完全に空にし、新しい空のバージョンを残したい。

また、これを Windows FTP クライアントと互換性を持たせる必要があります。

4

4 に答える 4

6

ncftp に限定されないことを明確にしたので、代わりにlftpglobを使用する必要があり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 echo *

-f プレーン ファイル (デフォルト)
-d ディレクトリ
-a すべてのタイプ

(ディレクトリを含むようにmrm展開されないため、この場合は使用できないことに注意してください。)*

于 2012-07-30T22:52:05.183 に答える
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 に答える