21

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
  • 724
  • 2
  • 7
  • 14
Devid Demetz
  • 315
  • 1
  • 2
  • 6

3 Answers3

33

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
  • 724
  • 2
  • 7
  • 14
  • 4
    Note: in this case `-print` is unnecessary, it's the default action. Also, to start with the current directory only, one may not give a path because `.` is the default path. Good answer though. Wildcards may be a trap like in [this question](https://superuser.com/q/1217773/432690), quoting them is very important here. – Kamil Maciorowski Jun 11 '17 at 11:53
  • @DevidDemetz - great :-) – Yaron Jun 11 '17 at 11:58
  • I have another question. If id now like to rename the the directory name. like the "c" that i searched for should become an "a". how would i do that – Devid Demetz Jun 11 '17 at 12:40
  • @DevidDemetz - if you have a new question - how to 1) find directories with specific pattern and 2) replace the directories name with a specific pattern - please open a new question for that. – Yaron Jun 11 '17 at 12:49
  • On MacOS it doesn't work when you leave out the path. `find . -name "*c*" -type d` worked for me. – Gigo Aug 27 '18 at 17:49
  • you might prefer "-iname" if you want case insensitive – Ram Jul 08 '22 at 15:19
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
  • 26,555
  • 15
  • 113
  • 235
0
for dir in `find -name "*c*" -type d -maxdepth 1`; do
      echo $dir
done
  • 1
    Whilst this may indeed work, it would be good to [edit] your answer and add some explanation... – Greenonline Jul 08 '22 at 15:22
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://superuser.com/help/whats-reputation) you will be able to [comment on any post](https://superuser.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/1133896) – music2myear Jul 09 '22 at 05:15