LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 05-28-2012, 09:28 PM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Rep: Reputation: 83
confusion: directory symlink and tailing /


Hya,

Situation
As shown below, /bkup/bkup/common is linked (symbolic) to /comm. Two search commands (with or without tailing slash at search path) work differently. Without slash, empty result. With slash, correct result.

Code:
>ls -l /bkup/bkup/common
lrwxrwxrwx. 1 penguin penguin 5 Sep 13  2011 /bkup/bkup/common -> /comm

>find /bkup/bkup/common -name penguinFood

>find /bkup/bkup/common/ -name penguinFood
/bkup/bkup/common/penguinFood
If searchs are done on the target directory, result look same.
Code:
>find /comm -name penguinFood
/comm/penguinFood
>find /comm/ -name penguinFood
/comm/penguinFood
my guess
I think it is a kind of symlink related rule. I am looking for the documentation.

so far
I have found in wikipedia (under symbolic link) following:
Quote:
The POSIX directory listing application, ls, denotes symbolic links with an arrow after the name, pointing to the name of the target file (see following example), when the long directory list is requested (-l option). When a directory listing of a symbolic link that points to a directory is requested, only the link itself will be displayed. In order to obtain a listing of the linked directory, the path must include a trailing directory separator character ('/', slash).
It looks related.

help
Can anybody direct me to correct documentation? So that I will not have further confusion in future. Also, I can avoid error-prone directory structure.

closing remark
Thank you for your reading my post and I apologyze my disorganized directory tree.

Happy Penguins!

Last edited by kaz2100; 05-29-2012 at 01:58 AM. Reason: typo
 
Old 05-28-2012, 09:58 PM   #2
CincinnatiKid
Member
 
Registered: Jul 2010
Distribution: Mageia-3
Posts: 395

Rep: Reputation: 33
Just out of curiousity, what happens if you pass an -L argument? For example:

Code:
>find -L /bkup/bkup/common -name penguinFood
I am not 100% sure, but I think that the behavior without the tailing slash would be to not follow the symbolic link and only report the properties of the symbolic link unless you use the -L option.

Here is a reference: http://linux.die.net/man/1/find

Here is something that may be relevant in the documentation:

Quote:
If the named file is in fact a symbolic link, and the -P option is in effect (or if neither -H nor -L were specified), the information used for the comparison will be taken from the properties of the symbolic link. Otherwise, it will be taken from the properties of the file the link points to.
To me, this says that if you haven't used -H or -L, then the symbolic link will not be followed. Of course, when you added a tailing / then it was followed, I can't find exact documentation to know this behavior though.

Last edited by CincinnatiKid; 05-28-2012 at 10:00 PM.
 
Old 05-28-2012, 10:13 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
Try the info find or pinfo find for detailed explanations and examples of the -L, -H, and -P options. (And lots of other cool uses of find.)

And, yes, you need to use find -L /bkup/bkup/common -name penguinFood
 
Old 05-29-2012, 01:57 AM   #4
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Original Poster
Rep: Reputation: 83
Hya,

Thanks for your time.

I completely did not think of -L option.
Code:
>find -L /bkup/bkup/common -name penguinFood
/bkup/bkup/common/penguinFood
>find -L /bkup/bkup/common/ -name penguinFood
/bkup/bkup/common/penguinFood
However, still why this works ??
Code:
>find /bkup/bkup/common/ -name penguinFood
/bkup/bkup/common/penguinFood
Happy Penguins!

Last edited by kaz2100; 05-29-2012 at 01:58 AM. Reason: typo
 
Old 05-29-2012, 10:13 AM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,360

Rep: Reputation: 583Reputation: 583Reputation: 583Reputation: 583Reputation: 583Reputation: 583
Quote:
Originally Posted by kaz2100 View Post
However, still why this works ??
Code:
>find /bkup/bkup/common/ -name penguinFood
/bkup/bkup/common/penguinFood
!
For that case, the find command never sees the symlink. When find calls lstat("/bkup/bkup/common/"), the trailing '/' tells the kernel that a directory is required, so the kernel follows the symlink and returns the data for the target directory. Without that trailing '/', the lstat() call returns the data for the symlink itself, and to get to the directory the find command would have to ask for the symlink to be followed (calling stat() instead of lstat()), and find won't do that unless you've used the "-L" flag.
 
1 members found this post helpful.
Old 05-30-2012, 08:23 PM   #6
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Original Poster
Rep: Reputation: 83
Hya,

Thank you for clear explanation. So, is this documented somewhere?

Have a nice Penguin!
 
Old 05-30-2012, 11:54 PM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 1,360

Rep: Reputation: 583Reputation: 583Reputation: 583Reputation: 583Reputation: 583Reputation: 583
Quote:
Originally Posted by kaz2100 View Post
So, is this documented somewhere?
Probably, in fact almost certainly -- just not anywhere that I know of. Some time ago I did some poking around with strace and wrote some simple test programs to piece together how it all appears to work.
 
Old 06-01-2012, 08:23 AM   #8
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Original Poster
Rep: Reputation: 83
Hya

Thanks a lot. This behavior may change in future, I guess. I will keep my eyes open.

Have a nice Penguin!

Last edited by kaz2100; 06-03-2012 at 08:24 PM. Reason: typo Penguins -> Penguin
 
Old 06-04-2012, 09:20 AM   #9
CincinnatiKid
Member
 
Registered: Jul 2010
Distribution: Mageia-3
Posts: 395

Rep: Reputation: 33
Quote:
Originally Posted by kaz2100 View Post
Hya

Thanks a lot. This behavior may change in future, I guess. I will keep my eyes open.

Have a nice Penguin!
It makes since how it works, because one might want to know the attributes of the soft link itself and not what it links to, this is precisely why it behaves like it does. There are other commands that work exactly the same way, for example, what would you expect this to do?

Code:
mkdir test
touch test/file
ln -s test/ link
rm -rf link
obviously this will remove the link, not what it links to. But if you add a trailing slash, it will delete what it links to, for example this:

Code:
mkdir test
touch test/file
ln -s test/ link
rm -rf link/
In the example above, because of the trailing slash, it will delete what is linked to, and not the symlink. So, this is normal behavior with pretty much all of the GNU/Linux utilities.
 
Old 06-04-2012, 05:06 PM   #10
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,429

Original Poster
Rep: Reputation: 83
Hya,

Yes, I do agree with you. So is ls (see post #1).

At least three penguins, me, CincinnatiKid (a short while ago) and rknichols (some time ago) ran into some sort of confusion. Hopefully no more penguins get confused in future.

Have a nice Penguin!
 
  


Reply

Tags
document, posix, slash, symlink


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to have all users get a symlink in their directory as they are created andrewz Linux - General 2 08-14-2007 02:23 PM
Can't Delete Directory SymLink Luvz2Fly Linux - Newbie 2 12-05-2006 12:45 PM
symlink directory eats files silindrin Debian 3 03-13-2006 08:28 AM
symlink home-directory? SteQve Linux - General 2 01-26-2006 04:42 PM
moving a symlink in a directory e1000 Linux - General 2 02-23-2004 05:09 PM


All times are GMT -5. The time now is 10:50 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration