I already saw the question: Finding subdirectories inside all directories with the same name

Now my directory structure is:

$ find .


I want to list all the directories at the end with "1" in their name:


but I don't want


I have tried the following commands:

find . -name "*1*"
find . -type d -path '*/1*'
find . -path '*/1*' -depth 2 -type d
find . -depth 2 -path '*/1*' -type d


find . -depth 2

gives the error:

find: paths must precede expression: 2

Found my solution

find -mindepth 2 . -type d -path "*1*"

Can anybody explain why -depth didn't work while -mindepth worked ?

3 Answers 3



find -mindepth 2 . -type d -path "*1*"

Explaination found here

— Option: -maxdepth levels

Descend at most levels (a non-negative integer) levels of directories below the command line arguments. -maxdepth 0 means only apply the tests and actions to the command line arguments.

— Option: -mindepth levels

Do not apply any tests or actions at levels less than levels (a non-negative integer). -mindepth 1 means process all files except the command line arguments.

— Option: -depth

Process each directory's contents before the directory itself. Doing this is a good idea when producing lists of files to archive with cpio or tar. If a directory does not have write permission for its owner, its contents can still be restored from the archive since the directory's permissions are restored after its contents.

I got confused between these options.


Can anybody explain why -depth didn't work while -mindepth worked?

I can. -depth didn't work while -mindepth worked because you're using the "wrong" implementation of find.

You are right in your answer -depth has nothing to do with the problem and cannot solve it for you. You are apparently using (and linking to the documentation of) GNU find; it supports -depth, -mindepth and -maxdepth exactly as your answer states.

find in FreeBSD supports -depth n:

True if the depth of the file relative to the starting point of the traversal is n.

Your -depth 2 would work With FreeBSD implementation of find.

Note FreeBSD find also supports -depth (without any option-argument) and its meaning is identical to -depth in GNU find. This usage of -depth is mandated by POSIX. -depth n or -mindepth n and -maxdepth n are non-portable extensions in respective implementations of find.


A quick and easy solution for common filesystems (ext* and xfs at least), if you don't know the depth of your subdirs :

find -type d -links 2

On other filesystems, a solution could be :

find -type d | sort -r | awk 'a!~"^"$0{a=$0;print}' | sort
  • 1
    For it to be a good solution you need to know the filesystem(s) involved. In many filesystems a directory without subdirectories reports 2 links, a directory with subdirectories reports more than 2 links. In some filesystems it's different and then your answer will not work. E.g. in Btrfs the link count of a directory it's always(?) 1. Aug 6, 2021 at 16:37
  • You are right I've edited my answer to more be filesystem independant, but I think the "links" version might be helpful anyway.
    – Julien
    Aug 9, 2021 at 12:04

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.