How do I search all subdirectories to find one with a certain name?

73,729

Solution 1

Try find /dir -type d -name "your_dir_name".

Replace /dir with your directory name, and replace "your_dir_name" with the name you're looking for.

-type d will tell find to search for directories only.

Solution 2

For a more general solution of finding one or more directories and searching them for something like finding old email addresses in git repositories look at the following pattern:

find . -type d -name .git -print0|\
    xargs -0r -I {} find {} -type f -print0 |\
    xargs -0r grep -e '[email protected]'

Solution 3

echo **/target

or to get one match per line:

printf %s\\n **/target

This works out of the box in zsh. In bash, you need to run shopt -s globstar first, and beware that this also traverses symbolic links to directories. In ksh93, you need to run set -o globstar first.

If you want to match only directories or symbolic links to directories, add a trailing / (i.e. **/target/). In zsh, to match only directories but not symbolic links to directories, make that **/target(/).

In any shell, you can use the find command:

find . -name target

On Linux and Cygwin, the . is optional. If you want to match only directories, add -type d.

Share:
73,729

Related videos on Youtube

bernie2436
Author by

bernie2436

Updated on September 18, 2022

Comments

  • bernie2436
    bernie2436 almost 2 years

    Let's say I have a top level directory called /dir and many sub directories. How do I search the subdirectories of /dir to find the one called x/x/dir/x/x/x/target?

    This question is similar to, but not exactly what I am looking for: find command for certain subdirectories.

    I am not looking for files, just directories with a particular name.

  • PowerKuu
    PowerKuu about 2 years
    And, to search all the directories, replace /dir with... ..