Search with "grep" for folder names

6

4

When using grep you can search for a specific regex, but only inside of a file. Is there any way, I can search for a folder name?

kpj

Posted 2011-04-05T19:29:07.363

Reputation:

Answers

13

I usually use find:

$ find . -name 'FolderBasename*' -type d

or for more complex queries

$ find . -regex '{FolderRegex}' -type d

As pointed out in the comments if you want case insensitive searches do -iname and -iregex

AlG

Posted 2011-04-05T19:29:07.363

Reputation: 315

Could you also elaborate on the options. It's an easy online search, but I would still like to know. – Sharan Duggirala – 2016-12-06T07:49:29.000

3-name doesn't take a regular expression. If you need to match with a regular expression, you probably want -regex or -iregex – Mark Longair – 2011-04-05T19:32:40.090

@Mark You're right - my bad there. I usually only need the supported * and ? (see man find) to get things found.

– AlG – 2011-04-05T19:37:33.967

6

If you really mean regexp instead of shellglob, you may want to use

find <path> -regex '<regex>' -type d

eg.

find Code/ -E -regex '(bin|redblack)_tree\.hs' -type d

the option -E turns on extendend regexp, see man find for more.

EdvardM

Posted 2011-04-05T19:29:07.363

Reputation: 161

0

If you are just concerned with matching the name you can simply use '-name' in find.

find <path> -name '<regex>' -type d

ssapkota

Posted 2011-04-05T19:29:07.363

Reputation: 1 213

-1

find is far better but a clunky answer to your question:

ls -l | grep '^d'

jim mcnamara

Posted 2011-04-05T19:29:07.363

Reputation: 769