1

我的外壳脚本:

#!/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

基本上,这将在作为第二个参数提供的给定目录中找到文件,并列出(-d 表示参数 1)或删除(-e 表示参数 1)>60 分钟前修改的文件。

我怎样才能重做这个也删除文件夹?

4

2 回答 2

2
  • 消除-type f
  • 更改ls -lls -ld

Change 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,因为ls -l在目录上将列出目录中的文件。

于 2010-09-01T13:09:23.167 回答
1
#!/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

那应该对你有用。

于 2010-09-01T13:15:28.790 回答