从 bash 中查找并删除目录

从 bash 中查找并删除目录
#!/bin/bash

# error handling
function error_handler() {
  echo "Error occurred in script at line: ${1}"
  echo "Line exited with status: ${2}"
}

trap 'error_handler ${LINENO} $?' ERR

set -o errexit
set -o errtrace

set -o nounset

# backup dir vars
BACKUPDIR=$(dirname "$0")

# check for directories
if [[ -d $BACKUPDIR/test1 ]]; then
  find $BACKUPDIR/test1 -mtime +6 -exec rm -rf {} \;
else
  :
fi

当我运行上面的测试脚本时,如果该目录test1不存在,则运行正常。但是,如果该目录test1在那里,即使它删除了该目录,我也会收到以下错误。

Test ./testFind.sh  
find: ‘./test1’: No such file or directory
Error occurred in script at line: 20
Line exited with status: 1

我怎样才能停止错误?

答案1

-prune在要删除的目录上使用以告诉find不要尝试在其中查找文件:

find $BACKUPDIR/test1 -mtime +6 -prune -exec rm -rf {} \;

相关内容