M HYPE SPLASH
// general

Finding last depth directories inside all directories with the same name

By Emily Wilson

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

Now my directory structure is:

$ find .
.
./4
./4/1
./2
./2/1
./5
./5/1
./1
./1/1
./3
./3/1

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

./4/1
./2/1
./5/1
./1/1
./3/1

but I don't want

./1

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

UPDATE

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

Answer

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 cpioor 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
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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