开发者社区> 问答> 正文

shell脚本遍历目录,按照指定条件删除目录及其子文件

目录的结构为 A/年/月/日/随机数/随机数/随机数/随机数/
例如 A/2013/06/18/1/2/3/4/5

现在的删除目录的规则为:(只需要递归循环前4级目录)
如果第2级目录(目录名)(年)小于当前所在年份.直接删除该目录及其子目录文件.
如果第3级目录(月)的目录写入日期小于当前日期60天,直接删除该目录及其子目录文件.
如果第4级目录(月)的目录写入日期小于当前日期60天,直接删除该目录及其子目录文件.

展开
收起
杨冬芳 2016-07-08 11:30:47 4724 0
1 条回答
写回答
取消 提交回答
  • IT从业
    #!/bin/sh           
    #FileName:deleteDir.sh        
    #Function:Linux下使用Shell脚本删除一个目录下的所有子目录和文件(不可恢复删除且目录下目录名和文件名中没有空格)         
    #Version:V0.1           
    #Author:Sunrier           
    #Date:2012-08-01   
     CURRPATH=$PWD  
     #DESTPATH为删除的目标目录(目标目录本身不会删除)  
    DESTPATH="/home/Sunrier/Trash"  
      
    #isNullDir函数判断进入的目录下是否存在下级子目录或者文件  
    #存在返回1,不存在返回0  
    isNullDir()  
    {  
        local NewCurrentPath=$PWD  
        local NewDirName=$1  
        cd $NewDirName  
        local NewFileList=`ls 2>/dev/null`   
          
    #   -n string : 如果字符串不为空,则结果为真    
      
        if [ -n "$NewFileList" ]  
        then  
            echo "目录$NewDirName下列表信息为$NewFileList"  
            cd $NewCurrentPath  
            return 1   
        else  
            echo "目录$NewDirName为空目录"  
            cd $NewCurrentPath  
            return 0      
        fi    
    }  
      
    if [ "Sunrier" != "$LOGNAME" ]  
    then  
        echo "您没有执行权限!请联系管理员!"  
        exit 1    
    fi  
      
    cd $DESTPATH 2>/dev/null  
     if [ $? -ne 0 ]  
    then  
        echo "没有找到目标目录!"  
        exit 1  
    fi  
      
    echo "您正准备删除的目录为$DESTPATH"  
      
    #不考虑隐藏目录和文件  
    FileList=`ls 2>/dev/null`  
      
    while [ "" != "$FileList" ]  
    do  
        echo "当前列表信息为 $FileList"  
          
        for pFile in $FileList  
        do  
            echo "加载 ${pFile} 中......"  
            if [ -d "${pFile}" ]  
            then  
                echo "检查到 ${pFile} 为目录 "  
                echo "目录 ${pFile} 处理中......"  

    注:在Linux的Shell脚本中,调用函数作为判断条件时,函数返回值为0时,if后的条件才为真;否则if后的条件为假!

                if  isNullDir "${pFile}"  
                then  
                    echo  "删除目录 ${pFile} 中......"  
                    rm -rf ${pFile}  
                else  
                    echo "移动目录${pFile}下的所有目录和文件中......"  
                    mv ${pFile}/* . >/dev/null 2>&1  
                    if [ $? -ne 0 ]  
                    then  
                        ModifyTime=`date +%Y%m%d%H%M%S`    
                        mv ${pFile} $ModifyTime   
                    fi    
                fi  
            else  
                echo "检查到 ${pFile} 为文件 "  
                echo "正在把文件 ${pFile} 的文件名更改为文件名 1 中......"  
                mv ${pFile} 1 2>/dev/null  
            fi  
      
            if [ -f 1 ]  
            then  
                echo "发现文件名为1的文件,正在删除文件1中......"  
                echo "123456"> 1  
                rm -rf 1          
            fi  
        done  
          
        echo "更新新的文件列表中......"  
        cd $DESTPATH  
        FileList=`ls 2>/dev/null`  
          
    done                  
      if [ $? -eq 0 ]  
    then  
        echo "清理成功!"  
        cd $CURRPATH  
        exit 0  
    else  
        echo "清理失败!"  
        cd $CURRPATH  
        exit 1  
    fi  
    [cpp] view plain copy

    注:去掉一些显示信息

      #!/bin/sh           
    #FileName:deleteDir.sh        
    #Function:Linux下使用Shell脚本删除一个目录下的所有子目录和文件(不可恢复删除且目录下目录名和文件名中没有空格)         
    #Version:V0.2          
    #Author:Sunrier           
    #Date:2012-08-27   
     CURRPATH=$PWD  
     #DESTPATH为删除的目标目录(目标目录本身不会删除)  
    DESTPATH="/home/Sunrier/Trash"  
      
     #isNullDir函数判断进入的目录下是否存在下级子目录或者文件  
    #存在返回1,不存在返回0  
    isNullDir()  
    {  
        local NewCurrentPath=$PWD  
        local NewDirName=$1  
        cd $NewDirName  
        local NewFileList=`ls 2>/dev/null`   
          
    #   -n string : 如果字符串不为空,则结果为真    
      
        if [ -n "$NewFileList" ]  
        then  
            cd $NewCurrentPath  
            return 1   
        else  
            cd $NewCurrentPath  
            return 0      
        fi    
    }  
      
    if [ "Sunrier" != "$LOGNAME" ]  
    then  
        echo "您没有执行权限!请联系管理员!"  
        exit 1    
    fi  
      
    cd $DESTPATH 2>/dev/null  
      
    if [ $? -ne 0 ]  
    then  
        echo "没有找到目标目录!"  
        exit 1  
    fi  

    echo "您正准备删除的目录为$DESTPATH"

    #不考虑隐藏目录和文件  
    FileList=`ls 2>/dev/null`  
      
    while [ "" != "$FileList" ]  
    do  
          
        for pFile in $FileList  
        do  
            echo "加载 ${pFile} 中......"  
            if [ -d "${pFile}" ]  
            then  
                  
    #注:在Linux的Shell脚本中,调用函数作为判断条件时,函数返回值为0时,if后的条件才为真;否则if后的条件为假!                 
                if  isNullDir "${pFile}"  
                then  
                    rm -rf ${pFile}  
                else  
                    mv ${pFile}/* . >/dev/null 2>&1  
                    if [ $? -ne 0 ]    
                    then    
                        ModifyTime=`date +%Y%m%d%H%M%S`      
                        mv ${pFile} $ModifyTime     
                    fi      
                fi  
            else  
                mv ${pFile} 1 2>/dev/null  
            fi  
      
            if [ -f 1 ]  
            then  
                echo "123456">1  
                rm -rf 1          
            fi  
        done  
          
        cd $DESTPATH  
        FileList=`ls 2>/dev/null`  
          
    done                  
      
    if [ $? -eq 0 ]  
    then  
        echo "清理成功!"  
        cd $CURRPATH  
        exit 0  
    else  
        echo "清理失败!"  
        cd $CURRPATH  
        exit 1  
    fi   
    //解决目录下目录名和文件名中含有空格的情况
    
    [cpp] view plain copy
    #!/bin/sh           
    #FileName:deleteDir.sh        
    #Function:Linux下使用Shell脚本删除一个目录下的所有子目录和文件(不可恢复删除)         
    #Version:V0.3           
    #Author:Sunrier           
    #Date:2012-08-29   
      
    CURRPATH=$PWD  
      
    #DESTPATH为删除的目标目录(目标目录本身不会删除)  
    DESTPATH="/home/Sunrier/Trash"  
      
    #isNullDir函数判断进入的目录下是否存在下级子目录或者文件  
    #存在返回1,不存在返回0  
    isNullDir()  
    {  
        local NewCurrentPath=$PWD  
        local NewDirName=$1  
        cd "$NewDirName"  
        local NewFileList=`ls 2>/dev/null`   
          
    #   -n string : 如果字符串不为空,则结果为真    
      
        if [ -n "$NewFileList" ]  
        then  
            echo "目录$NewDirName下列表信息为$NewFileList"  
            cd $NewCurrentPath  
            return 1   
        else  
            echo "目录$NewDirName为空目录"  
            cd $NewCurrentPath  
            return 0      
        fi    
    }  
      
    if [ "Sunrier" != "$LOGNAME" ]  
    then  
        echo "您没有执行权限!请联系管理员!"  
        exit 1    
    fi  
      
    cd $DESTPATH 2>/dev/null  
      
    if [ $? -ne 0 ]  
    then  
        echo "没有找到目标目录!"  
        exit 1  
    fi  

    echo "您正准备删除的目录为$DESTPATH"

    #不考虑隐藏目录和文件  
    #FileList=`ls 2>/dev/null`  
    FileList=`ls | tr " " "?" 2>/dev/null`  
      
    while [ "" != "$FileList" ]  
    do  
        echo "当前列表信息为 $FileList"  
          
        for pFile in $FileList  
        do  
            echo "加载 ${pFile} 中......"  
            if [ -d "${pFile}" ]  
            then  
                echo "检查到 ${pFile} 为目录 "  
                echo "目录 ${pFile} 处理中......"  
                  
    #注:在Linux的Shell脚本中,调用函数作为判断条件时,函数返回值为0时,if后的条件才为真;否则if后的条件为假!                 
                if  isNullDir "${pFile}"  
                then  
                    echo  "删除目录 ${pFile} 中......"  
                    rm -rf "${pFile}"  
                else  
                    echo "移动目录${pFile}下的所有目录和文件中......"  
                    mv "${pFile}"/* . >/dev/null 2>&1  
                    if [ $? -ne 0 ]  
                    then  
                        echo "发现父目录与子目录同名,试图更改父目录目录名......"  
                        ModifyTime=`date +%Y%m%d%H%M%S`    
                        mv "${pFile}" $ModifyTime   
                    fi    
                fi  
            else  
                echo "检查到 ${pFile} 为文件 "  
                echo "正在把文件 ${pFile} 的文件名更改为文件名 1 中......"  
                mv "${pFile}" 1 2>/dev/null  
            fi  
      
            if [ -f 1 ]  
            then  
                echo "发现文件名为1的文件,正在删除文件1中......"  
                echo "123456" > 1  
                rm -rf 1          
            fi  
        done  
          
        echo "更新新的文件列表中......"  
        cd $DESTPATH  
        #FileList=`ls 2>/dev/null`  
        FileList=`ls | tr " " "?" 2>/dev/null`  
          
    done                  
      
    if [ $? -eq 0 ]  
    then  
        echo "清理成功!"  
        cd $CURRPATH  
        exit 0  
    else  
        echo "清理失败!"  
        cd $CURRPATH  
        exit 1  
    fi  

    //去掉一些显示信息

    [cpp] view plain copy
    #!/bin/sh           
    #FileName:deleteDir.sh        
    #Function:Linux下使用Shell脚本删除一个目录下的所有子目录和文件(不可恢复删除)         
    #Version:V0.4          
    #Author:Sunrier           
    #Date:2012-08-29   
      
    CURRPATH=$PWD  
      
    #DESTPATH为删除的目标目录(目标目录本身不会删除)  
    DESTPATH="/home/Sunrier/Trash"  
      
    #isNullDir函数判断进入的目录下是否存在下级子目录或者文件  
    #存在返回1,不存在返回0  
    isNullDir()  
    {  
        local NewCurrentPath=$PWD  
        local NewDirName=$1  
        cd "$NewDirName"  
        local NewFileList=`ls 2>/dev/null`   
          
    #   -n string : 如果字符串不为空,则结果为真    
      
        if [ -n "$NewFileList" ]  
        then  
            cd $NewCurrentPath  
            return 1   
        else  
            cd $NewCurrentPath  
            return 0      
        fi    
    }  
      
    if [ "Sunrier" != "$LOGNAME" ]  
    then  
        echo "您没有执行权限!请联系管理员!"  
        exit 1    
    fi  
      
    cd $DESTPATH 2>/dev/null  
      
    if [ $? -ne 0 ]  
    then  
        echo "没有找到目标目录!"  
        exit 1  
    fi  

    echo "您正准备删除的目录为$DESTPATH"

    #不考虑隐藏目录和文件  
    FileList=`ls | tr " " "?" 2>/dev/null`  
      
    while [ "" != "$FileList" ]  
    do  
          
        for pFile in $FileList  
        do  
            echo "加载 ${pFile} 中......"  
            if [ -d "${pFile}" ]  
            then  
                  
    #注:在Linux的Shell脚本中,调用函数作为判断条件时,函数返回值为0时,if后的条件才为真;否则if后的条件为假!                 
                if  isNullDir "${pFile}"  
                then  
                    rm -rf "${pFile}"  
                else  
                    mv "${pFile}"/* . >/dev/null 2>&1  
                    if [ $? -ne 0 ]  
                    then  
                        ModifyTime=`date +%Y%m%d%H%M%S`    
                        mv "${pFile}" $ModifyTime   
                    fi    
                fi  
            else  
                mv "${pFile}" 1 2>/dev/null  
            fi  
      
            if [ -f 1 ]  
            then  
                echo "123456" > 1  
                rm -rf 1          
            fi  
        done  
          
        cd $DESTPATH  
        FileList=`ls | tr " " "?" 2>/dev/null`  
          
    done                  
      
    if [ $? -eq 0 ]  
    then  
        echo "清理成功!"  
        cd $CURRPATH  
        exit 0  
    else  
        echo "清理失败!"  
        cd $CURRPATH  
        exit 1  
    fi  
    
    
     
    
    2019-07-17 19:53:14
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Shell 脚本速查手册 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载