代码之家  ›  专栏  ›  技术社区  ›  Chris

修改shell脚本以删除文件夹和文件

  •  1
  • Chris  · 技术社区  · 15 年前

    我的shell脚本:

    #!/bin/bash
    if [ $# -lt 2 ]
    then
        echo "$0 : Not enough argument supplied. 2 Arguments needed."
        echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
        echo "Followed by some path to remove files from. (path of where to look) "
        exit 1
    fi
    
    if test $1 == '-d'
    then
        find $2 -mmin +60 -type f -exec ls -l {} \;
    elif test $1 == '-e'
    then
        find $2 -mmin +60 -type f -exec rm -rf {} \;
    fi
    

    我怎样才能修改它来删除文件夹?

    2 回复  |  直到 15 年前
        1
  •  2
  •   codaddict    15 年前
    • 删除 -type f
    • ls -l ls -ld

    更改1将列出所有内容,而不仅仅是文件。这也包括链接。如果您不能列出/删除除文件和目录以外的任何内容,则需要单独列出/删除文件和目录,如下所示:

    if test $1 == '-d'
    then
        find $2 -mmin +60 -type f -exec ls -ld {} \;
        find $2 -mmin +60 -type d -exec ls -ld {} \;
    elif test $1 == '-e'
    then
        find $2 -mmin +60 -type f -exec rm -rf {} \;
        find $2 -mmin +60 -type d -exec rm -rf {} \;
    fi
    

    根据需要更改2 在目录上会列出目录中的文件。

        2
  •  1
  •   user285879 user285879    15 年前
    #!/bin/bash
    if [ $# -lt 2 ]
    then
        echo "$0 : Not enough argument supplied. 2 Arguments needed."
        echo "Argument 1: -d for debug (lists files it will remove) or -e for execution."
        echo "Followed by some path to remove files from. (path of where to look) "
        exit 1
    fi
    
    if test $1 == '-d'
    then
        find $2 -mmin +60 -type d -exec ls -l {} \;
        find $2 -mmin +60 -type f -exec ls -l {} \;
    elif test $1 == '-e'
    then
        find $2 -mmin +60 -type d -exec rm -rf {} \;
        find $2 -mmin +60 -type f -exec rm -rf {} \;
    fi
    

    这对你应该有用。