# 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 {} \;

When I run the above test script, if the directory test1 isn't there it runs fine. However, if the directory test1 is there I get the following error even though it removes the directory.

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

How can I stop the error?

  • 2
    What's the purpose of the find command? Why not just rm -rf "$BACKUPDIR"/test1 ? – Muzer Nov 16 '16 at 11:33
  • In the "real", larger script I have an -mtime +6. I'll add it back in to help – eekfonky Nov 16 '16 at 11:50

Use -prune on the directories that you're going to delete to tell find not to trying to find files in them:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.