Strange behaviour of ln

Paolo Galtieri pgaltieri at gmail.com
Tue Aug 25 15:34:23 UTC 2015


On 08/24/2015 02:19 PM, Cameron Simpson wrote:
> On 24Aug2015 13:55, Paolo Galtieri <pgaltieri at gmail.com> wrote:
>> I've tried this on 2 different systems, and on both systems ln -s 
>> fails in the same way.
>>
>> Here's the steps
>>
>> mkdir Test
>> cd Test
>>> afile
>> cd ..
>> ln -s Test/afile Test/afile2
>> ls -l Test
>>
>> This is the output:
>> /bin/ls: cannot access Test/afile2: No such file or directory
>> total 0
>> -rw-rw-r--. 1 pgaltieri pgaltieri 0 Aug 24 13:46 afile
>> l?????????? ? ?         ?         ?            ? afile2
> [...]
>
> Firstly, run "/bin/ls", not "ls". Many system ship with an alias 
> called "ls" with presupplied various options, which is misleading. IMO 
> this is a terrible idea - the alias should at least use another name. 
> (I have ones called "l" and "L" for these conveniences, myself.)
>
> To your problem:
>
> Symlinks are strings resolved with respect to the directory in which 
> the symlink exists.
>
> So the example above "Test/afile" => "Test/afile2" makes a symlink 
> _in_ the directory "Test" named "afile2" pointing at "Test/afile". 
> Therefore it tries to access the path "Test/Test/afile", which fails 
> as one might expect.
>
> When making a symbolic link it needs to either be (a) an absolute path 
> or (b) relative WRT to the directory hodling the link. For the latter, 
> the reliable way is to cd to the  directory and make in from inside. 
> That way command line filename completion prodcues working results.
>
> Cheers,
> Cameron Simpson <cs at zip.com.au>
>
> Unix is user-friendly. It's just picky about who its friends are.
Cameron,
   thank you for your reply.  As it turns out the ln command

ln -s Test/afile Test/afile1

does create the link.  The problem as you noted is the ls command. If I do

/bin/ls -l Test

I get

total 0
-rw-rw-r--. 1 pgaltieri pgaltieri  0 Aug 25 08:20 afile
lrwxrwxrwx. 1 pgaltieri pgaltieri 10 Aug 25 08:20 afile2 -> Test/afile

But doing

ls -l Test

I get

/bin/ls: cannot access Test/afile2: No such file or directory
total 0
-rw-rw-r--. 1 pgaltieri pgaltieri 0 Aug 25 08:20 afile
l?????????? ? ?         ?         ?            ? afile2

I'll probably file a bug against freeradius indicating the documentation 
is wrong :-)

The correct way to do the link is

mkdir Test2
cd Test2
ln -s ../Test/file file
cd ..

/bin/ls -l Test2
total 0
lrwxrwxrwx. 1 pgaltieri pgaltieri 10 Aug 25 08:29 afile -> Test/afile
lrwxrwxrwx. 1 pgaltieri pgaltieri 13 Aug 25 08:29 afile2 -> ../Test/afile

ls -l Test2
total 0
-rw-rw-r--. 1 pgaltieri pgaltieri 0 Aug 25 08:20 afile

which is what I expect to see.  The ls command is an alias of /bin/ls -LCFb.

Again thank you for your help.

Paolo







More information about the users mailing list