我在远程服务器上有一个我需要清除的文件夹。我需要删除此文件夹中的所有文件和文件夹。我无法删除并重新创建父文件夹,因为我不想搞砸这些权限。
例如:
远程文件夹是开发/
该文件夹包含多个文件和多个文件夹
我想运行一个命令来完全清空Development /文件夹,并给我一个新的空版本。
我还需要它与Windows FTP客户端兼容。
答案 0 :(得分:5)
由于您明确表示您不仅限于ncftp,因此您应该使用lftp代替使用glob
和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$
The docs给出完整的解释:
rm [ -r ] [ -f ] 文件
删除远程文件。不扩展通配符,使用 mrm 。 -r用于递归目录删除。小心,如果出现问题,您可能会丢失文件。 -f禁止错误消息。
glob [ -d ] [ -a ] [ -f ] 命令模式< / em>的
Glob给出包含元字符的模式并将结果传递给给定命令。例如
glob echo *
-f普通文件(默认)
-d目录
- 所有类型
(请注意mrm
在这种情况下不可用,因为它不会扩展*
以包含目录。)
答案 1 :(得分:3)
使用登录ftp服务器 ncftp -u [user.ftp] [backup.server]
运行命令
rmdir -r [文件夹]
答案 2 :(得分:1)
直接解决OP的问题,以下命令完全按照要求执行:
rm -r *
确保导航到要删除的内容的目录并发出命令。它删除当前目录中的所有目录。
答案 3 :(得分: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