1834166 Members
3362 Online
110064 Solutions
New Discussion

"ls -L" command

 
SOLVED
Go to solution
Leif Halvarsson_2
Honored Contributor

"ls -L" command

Hi,

Do I understand this wrong ( from "man ls") ?
-L If the argument is a symbolic link, list the file or directory to which the link refers rather than the link itself."


# ll
total 2
-rw-rw-r-- 1 root sys 16 Apr 21 23:05 AAA
lrwxrwxr-x 1 root sys 3 Apr 21 23:05 BBB -> AAA
#
#
# ls BBB
BBB
#
# ls -L BBB
BBB


As I understand the ls -L BBB should return AAA (as BBB is a link and refers to AAA).

Or, am I wrong ?



5 REPLIES 5
A. Clay Stephenson
Acclaimed Contributor

Re: "ls -L" command

The difference will be much more obvious if you do an "ls -l BBB" and "ls -Ll BBB".
If it ain't broke, I can fix that.
Hazem Mahmoud_3
Respected Contributor

Re: "ls -L" command

Look at the example below. Yes, it is poining to AAA, but it still keeps the name BBB. Below you can see the file hazem1 with mod of 600 and hazem3 is a link to it. Look at the difference in the permissions when I do ll -L and just ll on hazem3. So it is showing you hazem1, but it displays it still as hazem3 since that is the actual name of the file you are doing ll on. Does that make sense? For directories it is more straightforward than this.

# ll
total 0
-rw-rw-r-- 1 root sys 0 Apr 21 16:29 hazem1
-rw-rw-r-- 1 root sys 0 Apr 21 16:29 hazem2
lrwxrwxr-x 1 root sys 6 Apr 21 16:30 hazem3 -> hazem1
# chmod 600 hazem1
# ll -L hazem1
-rw------- 1 root sys 0 Apr 21 16:29 hazem1
# ll -L hazem3
-rw------- 1 root sys 0 Apr 21 16:29 hazem3
# ll hazem3
lrwxrwxr-x 1 root sys 6 Apr 21 16:30 hazem3 -> hazem1


-Hazem
Leif Halvarsson_2
Honored Contributor

Re: "ls -L" command

Hi,
Yes, I notice the difference (but I am not sure I find it very logic).

I searched for a (direct) method to get the file or directory a link refers to (AAA in my example).
Of course there is simple workarounds, For example:

ll BBB | awk '{ print $NF }'

And, there is a readlink() system call.


A. Clay Stephenson
Acclaimed Contributor
Solution

Re: "ls -L" command

There is no need to use the readlink() system call. Perl has a built-in readlink function to do the same thing. Even more useful is the lstat() function as well as the stat() function in Perl. Lstat() behaves identically to stat() except when given a symbolic link as an argument. In that case, instead of reporting on the file that is pointed to, it instead reports on the symlink itself. It's rather nice because you can use lstat() mall the time and look at the mode bits. If it's a symbolic link then you do a readlink and stat the file.

If it ain't broke, I can fix that.
James A. Donovan
Honored Contributor

Re: "ls -L" command

I think the -L option is there for when you want to get a listing of the files in a directory that you only have a symbolic link to.

e.g.
[morpheus|jdonovan]
$ ll /bin
lr-xr-xr-t 1 root sys 8 Feb 22 2003 /bin -> /usr/bin

[morpheus|jdonovan]
$ ll -L /bin
total 74512
dr-xr-xr-x 3 bin bin 8192 Mar 22 13:08 X11
-r-xr-xr-x 1 bin bin 73728 Nov 14 2000 acl_edit
-r-xr-xr-x 1 bin bin 212992 Nov 14 2000 adb
.
.
.
Remember, wherever you go, there you are...