23

I need to find all directories which contain a certain character in their name and print them out.

So if i have the directories:

abc cde fgh

And I search for "c" I should get:

abc 
cde
Yaron
  • 754

3 Answers3

36

The following commands perform the required query:

find -name "*c*" -type d
  • starts with the current directory (no need to specify directory in case of current directory)
  • -name "*c*" - with name contains the letter c
  • -type d - which are a directory

You can run the command on other directory (/full/path/to/dir) using:

find /full/path/to/dir -name "*c*" -type d

More info nixCraft find command

Yaron
  • 754
1

If globstar is enabled you can use this

for d in **/*c*/; do echo $d; done

The first ** will match any arbitrary subdirectory paths. Then *c*/ with match folders with the c character in it

If it's not enabled you can enable it with shopt -s globstar

  • globstar

    • If set, the pattern ** used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a /, only directories and subdirectories match.
phuclv
  • 30,396
  • 15
  • 136
  • 260
0
for dir in `find -name "*c*" -type d -maxdepth 1`; do
      echo $dir
done