Search with "grep" for folder names
67,400
Solution 1
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
Solution 2
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.
Solution 3
If you are just concerned with matching the name you can simply use '-name' in find.
find <path> -name '<regex>' -type d
Solution 4
find is far better but a clunky answer to your question:
ls -l | grep '^d'
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
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?
-
Mark Longair about 13 years
-name
doesn't take a regular expression. If you need to match with a regular expression, you probably want-regex
or-iregex
-
AlG about 13 years@Mark You're right - my bad there. I usually only need the supported * and ? (see man find) to get things found.
-
SDG over 7 yearsCould you also elaborate on the options. It's an easy online search, but I would still like to know.
-
lilHar about 4 yearstechnically is the only one that actually answered the question as written, even if not as intended. However, it's limited to the folder you're in. May want to adjust so it's recursive with the -R flag on ls.