Find Command – Avoid Multiple Levels of Symlinks

findsymlink

I know the thread and try to fix my find with -mindepth 15 unsuccessfully

find -L $HOME -type f -name "*.tex" \
   -exec fgrep -l "janne" /dev/null {} + | vim -R -

Unsuccessful attempt

find -L $HOME -type f -mindepth 15 -name "*.tex" \
   -exec fgrep -l "janne" /dev/null {} + | vim -R -
  • find -L about it here

Its STOUT

Vim: Reading from stdin...
find: ‘/home/masi/LOREM’: Too many levels of symbolic links

Visualization of symlinks unsuccessful which gives all files while I would like to see only symlinked directories and files in the system

tree -l

Law29's proposal

# include symlinks
find "$1" -type l -name "$2*" -print0 \
    | xargs -0 grep -Hr --include "*.tex" "$2" /dev/null {} + | vim -R -

Output unsuccessful but it should not be empty

Vim: Reading from stdin...
grep: {}: No such file or directory
grep: +: No such file or directory

Characteristics of the system

masi@masi:~$ ls -ld -- "$HOME" /home/masi/LOREM 
drwxr-xr-x 52 masi masi 4096 Aug 16 16:09 /home/masi
lrwxrwxrwx  1 masi masi   17 Jun 20 00:27 /home/masi/LOREM -> /home/masi/LOREM/

masi@masi:~$ type find
find is /usr/bin/find

masi@masi:~$ find --version
find (GNU findutils) 4.7.0-git
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2) 

System: Linux Ubuntu 16.04 64 bit
For for Script at the thread: here
Find: 4.7.0
Grep: 2.25
Application of find: haetex here

Best Answer

If you want to display all files under $HOME, including those referenced via symbolic links, that end with .tex and contain the string janne:

find -L "$HOME" -type f -name '*.tex' -exec grep -l 'janne' {} + 2>/dev/null | vim -R -

If you want to display only symbolic links found under $HOME named *.tex corresponding to files that contain the string janne:

find -L "$HOME" -xtype l -name '*.tex' -exec grep -l 'janne' {} + 2>/dev/null | vim -R -

The only way to avoid the error message "Too many levels of symbolic links" is to discard all errors, which I've done with the 2>/dev/null construct.

In both cases the find verb will not traverse across files and directories that it has already traversed - it remembers where it's already visited and prunes those parts of the filesystem tree automatically. For example,

mkdir a a/b a/b/c
cd a/b/c
ln -s ../../../a

# Here you can ls a/b/c/a/b/c/a/b/...

# But find will not continue for very long
find -L a
a
a/b
a/b/c
find: File system loop detected; ‘a/b/c/a’ is part of the same file system loop as ‘a’.
Related Question