Is there a simple way to make this silently do nothing, if /my-directory does not exist?

find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm


  • find: GNU findutils 4.5.10
  • bash 4.2.53

You can throw away error reporting from find with 2>/dev/null, or you can avoid running the command at all:

test -d /my-directory && find /my-directory -type f -mtime +14 -print0 | xargs -r0 rm

As a slight optimisation and clearer code, some versions of find - including yours - can perform the rm for you directly:

test -d /my/directory && find /my-directory -type f -mtime +14 -delete
|improve this answer|||||

You can use,

find /my-directory -type f -mtime +14 -print0 2>/dev/null | xargs -r0 rm


2> /dev/null means redirects stderr to /dev/null.

  • /dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
|improve this answer|||||

print0 and xargs -r 0 are useless here, find has that capability builtin:

[ -d /my-directory ] && find /my-directory -type f -mtime +14 -exec rm {} +

or, as you are using GNU find, this variant suggested by @terdon:

[ -d /my-directory ] && find /my-directory -type f -mtime +14 -delete
|improve this answer|||||
  • 1
    Since the OP is using GNU find, you could also just do [ -d /my-directory ] && find /my-directory -type f -mtime +14 -delete – terdon Jun 20 '16 at 12:29
  • 2
    @terdon You are right. I usually prefer to stick with POSIX syntax and avoid suggesting GNUisms when they have no (or very little) added value though. – jlliagre Jun 20 '16 at 12:34

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.