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

时间:2010-09-01 13:03:04

标签: bash shell find rm

我的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

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

如何重新设计以删除文件夹?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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

那应该适合你。