141

How can I recursively search for directory names with a particular string where the string is only part of the directory name?

For example: the directory name is "8.0.3-99966_en", but I want to recursively search for directories with the string "99966".

4 Answers 4

186

You can use the find command:

find YOUR_STARTING_DIRECTORY -type d -name "*99966*" -print

Example:

find ~ -type d -name "*99966*" -print

should find all directories (-type d) starting from your home directory (~)that have their names containing the string "99966" (-name "*99966*") and output them (-print).

4
  • How can I exclude a certain directory from the search? I need to search / but I get tons of /proc results which I do not care about.
    – Kozuch
    Oct 27, 2014 at 13:52
  • 1
    @Kuzuch (after a while!): you can use negative grep, piping the sinf search into a commend like: find <whatever> | grep -v "/proc" which will filter out all lines containing the search string.
    – Juan Lanus
    Oct 8, 2015 at 16:06
  • Ho Ho Ho! I've found this useful today. Thanks, my friend. Merry Christmas :)
    – Danny
    Dec 24, 2020 at 22:57
  • Is there a way to limit depth? this can save hours
    – Gulzar
    Mar 29, 2022 at 14:51
40

To avoid all of the "Permission denied" results, you can use:

find / -type d -name "*99966*" -print 2>/dev/null

See this article on null device and this one on standard streams for more info.

1
14

You can pipe the output to grep to have it highlight the directory name
Something like

find / -type d | grep "directory name"

The / indicates to search the whole computer

1
  • @Zanna you are right, I've edited the answer. Thanks for pointing that out
    – Collin
    Dec 27, 2016 at 13:43
4

An easy way to do this is to use find | egrep string. If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well.

Another way to do this is to use ls -laR | egrep ^d.

And the locate command also comes in handy: locate string

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .