0

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 ?

1

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 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.

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.