且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用shell脚本删除文件在服务器

更新时间:2023-12-05 20:23:04

您想

 为* 20120330 *文件

而不是环比字面的话 LS * 20120330 * 。如果您的实际使用情况多,复杂得多,有可能是你想在一个变量中的文件列表的一个原因,但我的猜测是,你不这样做,永远不会。

(什么你可能会想是

 在`文件LS * 20120330 *`

不过,这也是错误的,因为不同但相关的原因。另请参见 http://porkmail.org/era/unix/award.html#ls一>)

四角切圆,你可能会更好过创建这将删除所有的文件名,而不是为每创建一个新的FTP会话文件要删除脚本;

 #!/斌/庆典dellist =
NL ='
'#是啊,NL是在单引号换行符
在* 20120330 *文件

    dellist =删除文件$ $ NL
DONE
FTP -vn< $主机> << EOFD
报价的<用户名>
  报价PASS<&通GT;
  二进制
  LS -lrt \\ * 20120330 \\ *。
  $ dellist
  放弃
EOFD

How do you delete a file in a server after doing a wget of all the files?
I understand the delete command works, when you are have done a ftp into the server, but I am unable to use the command while running a shell script. Below is my script, kindly let me know what is wrong in it.

#!/bin/bash

filelist='ls *20120330*'
for file in $filelist
do
ftp -vn <$hostname> <<EOFD
  quote USER <username>
  quote PASS <Pass>
binary
 ls -lrt *20120330*
 delete $filelist
quit
EOFD
done

You want

for file in *20120330*

rather than a loop over the literal words ls and *20120330*. If your real use case is much, much more complex, there could be a reason you want the file list in a variable, but my guess is that you don't, and never will.

(What you were probably thinking was

for file in `ls *20120330*`

but that is also wrong, for different but related reasons. See also http://porkmail.org/era/unix/award.html#ls)

Tangentially, you are probably better off creating a script which deletes all the file names, rather than creating a new ftp session for each file you want to remove;

#!/bin/bash

dellist=
nl='
' # yeah, nl is a newline in single quotes
for file in *20120330*
do
    dellist="delete $file$nl"
done
ftp -vn <$hostname> <<EOFD
  quote USER <username>
  quote PASS <Pass>
  binary
  ls -lrt \*20120330\*
  $dellist
  quit
EOFD