Is it possible to use find to recursively search a path but to only step through into a path which is symbolic link to a specified depth of n?

There is -maxdepth but this includes regular directories while I only want to limit the symbolic link depth.


Try something along the line of:

$ find -L -maxdepth n -path "/my/path/pattern" -xtype l -print

Short explanation:
- This should only print symbolic links to the specified depth, n, where n is a positive integer.
- Options must precede arguments. This means that the order in which the cmd above is written matters. Anyway, bash will complain otherwise.
- -print is the default so you can omit it in this case.

According to the man page on bash v4.4.12, the magic happens owing to:

-xtype c
The same as -type unless the file is a symbolic link. For symbolic links: if the -H or -P option was specified, true if the file is a link to a file of type c; if the -L option has been given, true if c is `l'. In other words, for symbolic links, -xtype checks the type of the file that -type does not check.

This works for me.

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.