27

If I run this command find $HOME/MySymlinkedPath -name "run*.sh" nothing happens, and gives no error ('MySymlinkedPath' is a symlinked path to another hard drive other then my $HOME one).

These also fails:

find ~/MySymlinkedPath -name "run*.sh"
find /home/MyUserName/MySymlinkedPath -name "run*.sh"

And just to make it sure, this non existent path fails (of course) find $HOME/MySymlinkedPathDUMMYTEST -name "run*.sh" so the path is being found (because that error does not happen) but find doesn't perform the search on it, and I am a lot clueless now.

It only works if I cd $HOME/MySymlinkedPath first and remove the path reference like this find -name "run*.sh" but that is not good for my scripts.

An additional info:
this command works as usual ls $HOME/MySymlinkedPath/run*.sh, and if I go there cd $HOME/MySymlinkedPath and run this ls .. the result is not what I was expecting -- the list of the path where the symlinked path is located -- it returns the list of the real path on the another media/harddrive!!!

pwd -P
/media/MediaIdentifier/RealPath
pwd
/home/MyUser/MySymlinkedPath

Re-thinking:
Is this a problem with find and ls, or with my system? or it is expected and not a problem at all?? I am on Ubuntu 12.10. It fails on all terminals I tested so doesn't seems a terminal "problem".

2

1 Answer 1

31

Here is the answer. But that question points to bash as the target of the problem.

The explanation is that find finds "$HOME/MySymlinkedPath". It's a symbolic link, not a directory, so the recursive descent stops there. If the expression matched "$HOME/MySymlinkedPath" (for example, in find "$HOME/MySymlinkedPath" -name 'My*'), then find would print that as a match.

As pointed there, I found that the easiest/cleanest way to deal with it and fix all scripts is, instead of:

find "$HOME/MySymlinkedPath" -name "run*.sh"

just add a slash, so that find starts not from the symbolic link but from the target of the symbolic link:

find "$HOME/MySymlinkedPath/" -name "run*.sh"

Alternatively, pass the -H option to find (note that it must come first, before the paths) to tell it to traverse symbolic links passed on its command line. (This is different from -L which tells find to traverse symbolic links encountered during the recursive descent as well.)

find -H "$HOME/MySymlinkedPath" -name "run*.sh"

And finally it worked best here with -L (because of the several filesystems I have symlinked thru folders). But it generates loads of non problematic error messages so I added 2>/dev/null; and also decided to create this alias alias find='find -L':

find -L "$HOME/MySymlinkedPath" -name "run*.sh" 2>/dev/null
2
  • 2
    IMHO, it's generally good practice to use a trailing slash after directories to indicate that they're actually directories. :) Makes things more readable, etc.
    – dannysauer
    Oct 6, 2013 at 21:54
  • In case anyone falls into the same trap as me, the position of the -H or -L (and -P -D -O to be complete) flags do matter; it should go before the search path.
    – Vic
    May 4, 2021 at 6:06

You must log in to answer this question.

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