Search code examples
bashunixfind

find - suppress "No such file or directory" errors


I can use -s in grep to suppress errors, but I don't see an equivalent for the find command in the man page... Is the only option to redirect STDERR>/dev/null?

Or is there an option that handles this? (open to fancy awk and perl solutions if needed)

Example:

$ for dir in `ls /mnt/16_c/`; do find /mnt/16_c/$dir/data/ -mtime +180 -type f -exec echo {} \;; done
find: `/mnt/16_c/test_container/dat/': No such file or directory

Solution

  • You can redirect stderr with 2>/dev/null, for example:

    find /mnt/16_c/$dir/data/ -mtime +180 -type f -exec echo {} \; 2>/dev/null
    

    Btw, the code in your question can be replaced with:

    find /mnt/16_c/*/data/ -mtime +180 -type f 2>/dev/null
    

    And if there is at least one matching directory, then you don't even need to suppress stderr, because find will only search in directories that match this pattern.