LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-21-2011, 08:26 PM   #1
NoStressHQ
Member
 
Registered: Apr 2010
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609

Rep: Reputation: 221Reputation: 221Reputation: 221
Question Symbolic links to directories mystery.


Hi,

Although I'm quite at ease with command line and shell and fs (supposedly ), I just encountered a weird behavior which I am able to reproduce. I can't access some symlink from another symlink. Here it is.

Code:
~$ mkdir tmp ; cd tmp

~/tmp$ mkdir -p {contoto/toto,contata/tata} ; touch contoto/toto/toto.txt ; touch contata/tata/tata.txt

~/tmp$ ln -s contoto/toto/ symtoto ; ln -s contata/tata/ symtata

~/tmp$ ls -l symtoto symtata
lrwxrwxrwx 1 me myself 13 Aug 22 03:03 symtata -> contata/tata/
lrwxrwxrwx 1 me myself 13 Aug 22 03:03 symtoto -> contoto/toto/

~/tmp$ ls -l --dereference-command-line-symlink-to-dir symtata
total 0
-rw-r--r-- 1 me myself 0 Aug 22 03:02 tata.txt

~/tmp$ cd symtoto
~/tmp/symtoto$ ls -l --dereference-command-line-symlink-to-dir ../symtata 
ls: cannot access ../symtata: No such file or directory
Just a tree to graphically see what is being constructed:

Code:
~/tmp$ tree
.
|-- contata
|   `-- tata
|       `-- tata.txt
|-- contoto
|   `-- toto
|       `-- toto.txt
|-- symtata -> contata/tata/
`-- symtoto -> contoto/toto/
Could someone explain me this behavior ? That happened to me when I wanted to move the content of a subdir of a "parent symlink" from a sibling symlink (in the "real life" case, the links are even absolute references, and the bash completion is working great itself). I know that some tools need to explicitly handle symbolic links (as I do with ls), but even with that it doesn't work from the other symlink.

Sorry, I must have missed something stupid in my now long time ago education .

Thanks !
 
Old 08-21-2011, 08:56 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
When you cd into symtoto, you are now actually in ~/tmp/contoto/toto. The directory above that is ~/tmp/contoto, which doesn't contain the symlink you want.

Try ls ../../symtata instead, or ls $OLDPWD/symtata.

The shell is applying the logical directory structure, while the external program ls is looking at the actual physical directory structure. You can use set -P (a.k.a. set -o physical) to force bash to use the physical structure instead.

man bash > set built-in
Quote:
-P If set, the shell does not follow symbolic links when executing commands such as cd that change the current working directory. It uses the physical directory structure instead. By default, bash follows the logical chain of directories when performing commands which change the current directory.
 
1 members found this post helpful.
Old 08-21-2011, 09:06 PM   #3
NoStressHQ
Member
 
Registered: Apr 2010
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609

Original Poster
Rep: Reputation: 221Reputation: 221Reputation: 221
Oh! So you're telling me that ".." in a directory is actually a real link to the parent, and NOT a symbolic convention that work on the "path" (as a string of char). Alright, I didn't understood that. Thank you, I thought it was really an abstraction to build a "new path string" and not a real inode link.

Again, thank you for your fast answer .

Edit: Alright, I simply did what I should have done before posting:
Code:
 ~/tmp$ cd symtata ; ls -l ..
and indeed it shows me the light

Last edited by NoStressHQ; 08-21-2011 at 09:11 PM.
 
Old 08-21-2011, 09:30 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
That's right. . and .. are not shell synonyms, but actual directories, as defined by the posix standard for filesystems (I believe). The shell just passes these directly to the command being executed, like any other file name.

It appears that the confusion mostly comes from tab completion, which is acting according to bash's logical directory structure, before the command is executed. So there's a discrepancy between the apparent an actual directory contents. This is probably something that needs to be fixed in the tab completion scripts.

pwd/$PWD also displays the logical directory name, making it even harder to detect that you're in a symlinked directory. Running readlink -f $PWD will give you the real location.
 
Old 08-21-2011, 09:47 PM   #5
NoStressHQ
Member
 
Registered: Apr 2010
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609

Original Poster
Rep: Reputation: 221Reputation: 221Reputation: 221
Quote:
Originally Posted by David the H. View Post
pwd/$PWD also displays the logical directory name, making it even harder to detect that you're in a symlinked directory. Running readlink -f $PWD will give you the real location.
Yes, and I also saw that simply
Code:
 $ pwd -P
will do the job too:
Code:
 pwd [-LP]
              Print  the absolute pathname of the current working directory.  The pathname printed contains no symbolic links if the -P option is supplied or the
              -o physical option to the set builtin command is enabled.  If the -L option is used, the pathname printed may contain symbolic links.   The  return
              status is 0 unless an error occurs while reading the name of the current directory or an invalid option is supplied.
Thanks again, I'm glad to have learn something today .
 
1 members found this post helpful.
Old 08-21-2011, 10:29 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Hey, you taught me something too. I was not aware of (or forgot about) pwd -P.

It doesn't help with $PWD though, which I tend to use quite often in scripts.
 
Old 08-21-2011, 11:25 PM   #7
NoStressHQ
Member
 
Registered: Apr 2010
Location: Geneva - Switzerland ( Bordeaux - France / Montreal - QC - Canada)
Distribution: Slackware 14.2 - 32/64bit
Posts: 609

Original Poster
Rep: Reputation: 221Reputation: 221Reputation: 221
Maybe $(pwd -P) could do the job ? (I guess you have already thought about that ).

Cheers.
 
Old 08-22-2011, 01:25 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yeah, of course you can do that. But it requires opening up a subshell to run the command, as opposed to simply reading a variable, so it's sub-optimal. I did once use readlink in the same way for a script that could encounter symlinked files.

But I don't usually use symlinks to directories myself, so most of my scripts are unlikely to be affected by this. It is a theoretical concern, however.
 
  


Reply

Tags
directories, shell, symlink



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
symbolic links directories access and permissions goncalopp Linux - Security 2 05-07-2009 12:28 PM
Symbolic links Vs Hard links sulekha Linux - General 2 10-02-2008 07:03 AM
Users' home directories as symbolic links mschutte Linux - Software 4 04-11-2007 02:04 PM
Symbolic Links, directories, and relative paths (../). Can it be done? jimwillsher Linux - Newbie 1 03-11-2005 05:27 PM
mkisofs & symbolic links to directories Imek Linux - General 1 10-28-2004 06:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration