2

I use find command to find all regular files older than some number of minutes in a directory tree containing some symbolic links that need to be resolved:

find -L . -type f -mmin +300 -print

But this directory tree has some looped symbolic links that cause find to emit errors like:

/usr/bin/find: File system loop detected; ‘/storage/tester1/data/webdav’ is part of the same file system loop as ‘/storage/tester1/data’

I can't remove this loops from directory structure and can't avoid using key -L but I need to suppress this errors to get right exit code from find to detect any real possible errors (and not 'loop detected' warnings).
Redirecting of sdterr 2>/dev/null can't help because I still get exit status 1 on any search because of this loops, and it makes impossible to detect presence or absence of any real errors during searching in my script.

How can I avoid it and get exit status 0 on successful search in directory tree that has symlink loops?

Note: I know that there are some similar questions about 'loop detected' on this site, but all these questions could be solved by removing following of symbolic links (that is impossible in my case), so this question is not a duplicate. Please, don't close it!

4
  • "containing some symbolic links that need to be resolved" – Are the links known in advance? And do they form a relatively short list? If so, then maybe find -H . ./foo/good_link_1 ./bar/baz/good_link_2 …. Commented Dec 3, 2022 at 23:47
  • @KamilMaciorowski, in most cases - no, I don't know these links in advance. These are directories of users and they very often store very strange things there. Commented Dec 4, 2022 at 2:20
  • What is the real goal? Why do you want to "find all regular files older than some number of minutes"? What are you going to do with the results? I expect any user to be able to create a symlink to /. Maybe it's easier to assume such symlink exists and start from / in the first place, without -L. What bad will happen if this assumption is wrong? A user may create such symlink anytime just for fun… Right? What difference does it make if the link is really there? Please make sure there is no XY problem here. Edit the question if there is. Commented Dec 4, 2022 at 8:12
  • 1
    @KamilMaciorowski, in the real life it's the part of cli utility that every user can run on their storage as a cron task. User may supply started directory point and interval in minutes, and cli utility must found all regular files owned by the user, older than supplied interval and linked in any way from supplied user's project starting point, then remove its and report about its to user. Utility must report to admin about any errors during execution. In real life 'find' command is longer, but I've removed the part that does not directly address the question. Commented Dec 4, 2022 at 11:52

1 Answer 1

1

I don't have enough reputation to leave a comment.

You could take the output of find and use grep to invert-match (-v) to exclude anything that contains "File system loop detected":

find / -type d -name bar 2>&1 | grep -v "File system loop detected"

An example:

cd /usr
find . -type d -name bar 2>&1

./share/locale/bar

find: ‘./share/polkit-1/rules.d’: Permission denied

echo $?

1

find . -type d -name bar 2>&1 | grep -v "Permission denied"

./share/locale/bar

echo $?

0

3
  • At present I use parsing of find stderr as temporary workaround, but this is not a really solution. I'm looking for some combination of find arguments (if this combination exist) to get exit status of find that can accurately reflect the presence of other search errors. Commented Dec 5, 2022 at 1:35
  • I've not tested this, but have a look at find-printf, look for %Y can also be used to show other problems a symbolic link may be having - Does this help your scenario?
    – Aubs
    Commented Dec 5, 2022 at 23:09
  • I've tested it right now: unfortunately, this does not help in any way in the -L mode. Commented Dec 6, 2022 at 2:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .