FTP删除X天之前的目录 [英] FTP delete directories older than X days

查看:44
本文介绍了FTP删除X天之前的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于此脚本创建一个脚本.但是当我在结果上运行它时,如果月份不同,它将删除比tha日期更新的文件.

I'm creating a script based on this one. But when i run it on the results it's deleting files newer than tha date if the months aren't the same.

用于从ftp删除旧文件的Linux shell脚本

正在删除02-02-2015"这是我今天创建的备份

"Removing 02-02-2015" which is a backup i created today

#!/bin/bash
# get a list of directories and dates from ftp and remove directories older than ndays
ftpsite="hostname"
ftpuser="user"
ftppass="pass"
putdir="/full"

ndays=15


# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`


echo removing files older than $MM $DD

# get directory listing from remote source
listing=`ftp -i -n -p $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
ls
quit
EOMYF
`
lista=( $listing )

# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
  # month (element 5), day (element 6) and filename (element 8)
  # echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]}          File: ${lista[`expr $FNO+8`]}

  # check the date stamp
  if [ ${lista[`expr $FNO+5`]}=$MM ];
   then
    if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
    then
      # Remove this file
      echo "Removing ${lista[`expr $FNO+8`]}";
      ftp -i -n -p $ftpsite <<EOMYF2
      user $ftpuser $ftppass
      binary
      cd $putdir
      mdelete ${lista[`expr $FNO+8`]}/*
      rmdir ${lista[`expr $FNO+8`]}
      quit
      EOMYF2
   fi
  fi
done

结果=>删除早于1月18日的文件删除02-02-2015

result => removing files older than Jan 18 Removing 02-02-2015

推荐答案

此行似乎是错误的,因为它始终是正确的:

This line seems to be wrong, because it's always true:

if [ ${lista[`expr $FNO+5`]}=$MM ];

您应在"="周围写空格.正确的格式是:

You should write spaces around "=". The correct form is:

if [ ${lista[`expr $FNO+5`]} = $MM ];

[] 是bash内置的,其参数将传递给"test"命令. [asd ="$ qwe"] 等效于: test"asd""=""$ qwe"

[ ] is bash builtin, and its argument will be passed to "test" command. [ asd = "$qwe" ] is equivalent to: test "asd" "=" "$qwe"

这篇关于FTP删除X天之前的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆