find: Symbolic Links
2.3.1 Symbolic Links
--------------------
Symbolic links are names that reference other files. GNU 'find' will
handle symbolic links in one of two ways; firstly, it can dereference
the links for you - this means that if it comes across a symbolic link,
it examines the file that the link points to, in order to see if it
matches the criteria you have specified. Secondly, it can check the
link itself in case you might be looking for the actual link. If the
file that the symbolic link points to is also within the directory
hierarchy you are searching with the 'find' command, you may not see a
great deal of difference between these two alternatives.
By default, 'find' examines symbolic links themselves when it finds
them (and, if it later comes across the linked-to file, it will examine
that, too). If you would prefer 'find' to dereference the links and
examine the file that each link points to, specify the '-L' option to
'find'. You can explicitly specify the default behaviour by using the
'-P' option. The '-H' option is a half-way-between option which ensures
that any symbolic links listed on the command line are dereferenced, but
other symbolic links are not.
Symbolic links are different from "hard links" in the sense that you
need permission to search the directories in the linked-to file name to
dereference the link. This can mean that even if you specify the '-L'
option, 'find' may not be able to determine the properties of the file
that the link points to (because you don't have sufficient permission).
In this situation, 'find' uses the properties of the link itself. This
also occurs if a symbolic link exists but points to a file that is
missing.
The options controlling the behaviour of 'find' with respect to links
are as follows:
'-P'
'find' does not dereference symbolic links at all. This is the
default behaviour. This option must be specified before any of the
file names on the command line.
'-H'
'find' does not dereference symbolic links (except in the case of
file names on the command line, which are dereferenced). If a
symbolic link cannot be dereferenced, the information for the
symbolic link itself is used. This option must be specified before
any of the file names on the command line.
'-L'
'find' dereferences symbolic links where possible, and where this
is not possible it uses the properties of the symbolic link itself.
This option must be specified before any of the file names on the
command line. Use of this option also implies the same behaviour
as the '-noleaf' option. If you later use the '-H' or '-P'
options, this does not turn off '-noleaf'.
Actions that can cause symbolic links to become broken while 'find'
is executing (for example '-delete') can give rise to confusing
behaviour. Take for example the command line 'find -L . -type d
-delete'. This will delete empty directories. If a subtree
includes only directories and symbolic links to directoires, this
command may still not successfully delete it, since deletion of the
target of the symbolic link will cause the symbolic link to become
broken and '-type d' is false for broken symbolic links.
'-follow'
This option forms part of the "expression" and must be specified
after the file names, but it is otherwise equivalent to '-L'. The
'-follow' option affects only those tests which appear after it on
the command line. This option is deprecated. Where possible, you
should use '-L' instead.
The following differences in behaviour occur when the '-L' option is
used:
* 'find' follows symbolic links to directories when searching
directory trees.
* '-lname' and '-ilname' always return false (unless they happen to
match broken symbolic links).
* '-type' reports the types of the files that symbolic links point
to. This means that in combination with '-L', '-type l' will be
true only for broken symbolic links. To check for symbolic links
when '-L' has been specified, use '-xtype l'.
* Implies '-noleaf' (⇒Directories).
If the '-L' option or the '-H' option is used, the file names used as
arguments to '-newer', '-anewer', and '-cnewer' are dereferenced and the
timestamp from the pointed-to file is used instead (if possible -
otherwise the timestamp from the symbolic link is used).
-- Test: -lname pattern
-- Test: -ilname pattern
True if the file is a symbolic link whose contents match shell
pattern PATTERN. For '-ilname', the match is case-insensitive.
⇒Shell Pattern Matching, for details about the PATTERN
argument. If the '-L' option is in effect, this test will always
return false for symbolic links unless they are broken. So, to
list any symbolic links to 'sysdep.c' in the current directory and
its subdirectories, you can do:
find . -lname '*sysdep.c'