9

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.

14

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.

1

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 'my.old@email.address'
1
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.