Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I search through directories starting with a certain letter with the Linux find command.

For example I want to search all directories starting with the letter a for a file or directory starting with b.

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

Try a find in a find:

find . -type d -name "a*" -exec find {} -name "b" \;

Starting at the current directory (.), find will look for all directories starting with the letter a recursively. For each directory it finds, it will look inside it for a file named b.

If you only want it to look in the folders starting with a, and no directories in those a* folders, use maxdepth:

find . -type d -name "a*" -exec find {} -maxdepth 1 -name "b" \;

to get rid of errors:

find . -type d -name "a*" 2> /dev/null -exec find {} -maxdepth 1 -name "b" \;
share|improve this answer
    
nix the -type f on the interior find -- OP's question indicates "b" could be file or directory. –  quack quixote Dec 8 '09 at 2:34
    
Yeah I had that part in mind so I just did a file example. If I take off the type it will also show symbolic links among other things he doesn't want -- if there happens to be one named "b". –  John T Dec 8 '09 at 4:05
    
well, he doesn't specify not wanting symlinks, so i'd make the opposite assumption -- but we've pointed out the limitations now, so he can make up his own mind. :) –  quack quixote Dec 8 '09 at 15:35
    
True enough, that way my command wont wrap either. –  John T Dec 8 '09 at 19:18
    
Thanks everyone, this is what I was looking for. –  bing Dec 9 '09 at 9:03
add comment

Just a quick update for people who might end up on this question.

In addition to the solution John T provided, I have also found that you can exclude directories by using the prune switch (should have read the man pages sooner I guess, hehe.)

So for example if I want to search all directories for file or directory "b" except directories starting with an "a" I can do this

find . -path 'a*' -prune -o -name "b" -print

bing

share|improve this answer
add comment

you can also use find -regex...

find -regex .*/a.*/b
share|improve this answer
add comment

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.