find usage with -L

I have

link -> file

I do
find -L . -name 'link'

And get
./link

Why is that?

man find says:

-L : Follow symbolic links. When find examines or prints information about files, the information used shall be
taken from the properties of the file to which the link points, not from the link itself (unless it is a bro‐
ken symbolic link or find is unable to examine the file to which the link points).

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The general rule is that if a command operates on links (i.e. directory entries, which are pointers to inodes) then the command treats symlinks as themselves rather than as the object the link points to. Otherwise the command operates on what the symlink points to. Thus cp follows symlinks by default and copies the contents of the file pointed to by the link. But when you ask cp to deal with directory entries by specifying -R, it stops following symlinks. mv always works with directory entries, and so it never follows symlinks.

The find command’s normal activity is to operate on directory entries, so symlinks are not followed by default. Adding -L causes find to follow symlinks for all properties except the one that cannot be ignored when doing directory search, the name. One of purposes of find -name is to provide input for commands like mv and rm, which operate on directory entries. There would be unpleasant and surprising results if find -L dir -name could produce names that pointed outside the directory tree rooted at dir.

Method 2

With -L, it examines the properties of the file – contents or metadata, not those of the link. E.g. if you use -atime, it will check the atime of the file, not the link:

$ find testdir/ -name link -newer testdir/ref
testdir/link
$ find -L testdir/ -name link -newer testdir/ref
$

testdir/link was created after testdir/ref, but the file it points at was not.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments