views:

462

answers:

5

So I have have a directory - lets say /dir/. In it I have these folders- /dir/fold1/ /dir/fold2/ /dir/fold3/

each of these folders (fold1,2,3) may contain a folder called foo. I want to list all the folders inside dir, that have inside them a folder called foo.

Thanks!

+1  A: 

use this:

find dir -type d -name foo

So... dir is the directory you want to search.

-type is they type of what you're looking for: directory, file or link.

-name is the name of what you're looking for, case sensitive. You can use -iname if you want it to be case insensitive.

Nathan Fellman
This lists all the foo folders, not the contents of their parents.
mouviciel
+1  A: 

I propose:

find /dir -name foo -type d -exec dirname {} \; | xargs ls

but this does not work if any directory has whitespaces in it.

mouviciel
+1  A: 

What about

ls -R /dir | grep -i foo

Breaking down the command we have: ls -R = Lists files recursively (including directories

grep -i foo = Will take the command above and filter it case-insensitive to display any instances of 'foo'.

alfredodeza
How do I make this command not print every directory which I don't have access permissions to?
You should be able to redirect stderr. Something like this: ls -R /dir 2>/dev/null | grep -i foo
Duck
+1  A: 

If you want to list all of the directories which only contain a subdirectory named foo at the first level, you can do

for x in /dir/*; do
  [ -d $x/foo ] && ls $x
done

The [ ... ] construct is an abbreviation for the shell built-in test. -d $x/foo tests if the $x/foo exists and is a directory. If it does, then ls $x is executed.

Adam Rosenfield
+1  A: 
ls -ld /dir/*/foo | grep ^d
camh