4

I was skimming over the documentation of find to better utilize the command usage.

I was reading the part that says

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).

To my understanding, if I do something like:

find -L -iname "*foo*"

this will search the current directory recursively and when it encounters a symlink, it follows the link to the original file. If the original file has the name pattern *foo*, the former link is reported.

However, this doesn't seem the case. I have

main-file
sl-file -> main-file

Running the command above find -L -iname "*main*" reports

./main-file

And I was expecting

./main-file # because it matches the criterion
./sl-file   # because the file points to matches the criterion

That being said, using another test like -type works as I am expecting. Say I have this:

main-file
dir/sl-file -> ../main-file

Running this

find dir -type f

returns nothing. But this

find -L dir -type f

reports dir/sl-file.

What gives?

I have gone through this post that says a file name isn't a file property. This is something I can't really get my head around.

  • 1
    I would exactly expect what has happened. Why should find find sl-file if your searching for *main* ? -L means that it follow symlinks, not use their real name. – RoVo Oct 10 '18 at 13:15
  • find returns ./main-file because you have both file and link in the same directory. If you change symlink to point to different directory that won't be searched you will see that find won't return anything. – mrc02_kr Oct 10 '18 at 13:19
  • 1
    But I do see that it is not really clear in the docs. -type uses the type of the linked file, while -name does not. But I think that this is a duplicate to the linked question. I don't think that we can answer any better or anything else than this. – RoVo Oct 10 '18 at 13:26
  • For me it's clear. I give you example. Let's suppose that you today symlink to file that was last modified 1 year ago and you're trying to find all files modified in less than week, if you use option -L find won't return that symlink and I you don't use option -L find will return that symlink. If you specify type you will be searching file with that particular type. -name doesn't check file type and it's looking at only name of the file (directory is also file) – mrc02_kr Oct 10 '18 at 13:31
  • @RoVo, yep. Test -type works as I am expecting. I have updated the post with an example. Maybe you can elaborate why does it work with -type but doesn't with -name. – joker Oct 10 '18 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.