Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer
    
Is there anyway to only display the results of type file with this method? I forgot to mention that and adding -type f seems to break this. –  user1227038 Jan 17 '13 at 4:58
2  
Where are you adding -type f? I'd put it between -o and -print. –  jw013 Jan 17 '13 at 5:14
    
Thanks @jw013 , that was just about the only place I didn't try. –  user1227038 Jan 17 '13 at 5:30
    
On more complex expressions you'll need to use parenteses for grouping... –  vonbrand Jan 21 '13 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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