Bash – Recursive find that does not find hidden files or recurse into hidden dirs

bashfind

I am wanting to search recursively through a directory and find all files that are not hidden files themselves and are not in a hidden dir. I tried using find . -type f -not -name '.*' which excludes any base name hidden files, but it still recurses into hidden directories.

dir/
   file.py
   .hidden_file
   .hidden_dir/
      file.c

I would want the output to be:

./dir/file.py

however, I get:

./dir/file.py
./dir/.hidden_dir/file.c

EDIT:
I would like to list only files, i.e., -type f

Best Answer

You'll have to "prune" the directories you don't want to recurse into:

find dir -name '.*' -prune -o -print

Usually -prune is used with -o because it returns true, so when combined with short-circuit OR, it has the effect of skipping the -print on hidden files / directories, which is exactly what you want.

Related Question