Finding last depth directories inside all directories with the same name
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/1I want to list all the directories at the end with "1" in their name:
./4/1
./2/1
./5/1
./1/1
./3/1but I don't want
./1I 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 dUPDATE
find . -depth 2gives the error:
find: paths must precede expression: 2Found 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 levelsDescend at most
levels(a non-negative integer) levels of directories below the command line arguments.-maxdepth 0means only apply the tests and actions to the command line arguments.— Option:
-mindepth levelsDo not apply any tests or actions at levels less than
levels(a non-negative integer).-mindepth 1means process all files except the command line arguments.— Option:
-depthProcess each directory's contents before the directory itself. Doing this is a good idea when producing lists of files to archive with
cpioortar. 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
-depthdidn't work while-mindepthworked?
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 2On other filesystems, a solution could be :
find -type d | sort -r | awk 'a!~"^"$0{a=$0;print}' | sort 2