Greg Wooledge wrote: > That leaves names which contain ->. The tricky part here is that we > can't easily tell whether an extra -> is in the symbolic link or in > the target. > > imadev:~$ ln -s tmp 'x -> y' > imadev:~$ ln -s 'y -> tmp' x > imadev:~$ ls -ld x* > lrwxr-xr-x 1 wooledg pgmr 8 Feb 12 09:28 x -> y -> tmp > lrwxr-xr-x 1 wooledg pgmr 3 Feb 12 09:28 x -> y -> tmp > > However, there actually is enough information available to extract > the desired part. When we call ls -l, we're passing it the filename > we're resolving. So we already know the source name. Removing the > source name and the ' -> ' which follows it should leave us with the > target name. > > link=$(command ls -l -- "$file"; printf x) > link=${link%$'\nx'} > remove="$file -> " > file=${link#*$remove}
Your solution using the original filename is better but I thought I would point out that the size of the target value is also displayed by 'ls -l'. In the above the value "tmp" is 3 characters and "y -> tmp" is 8 characters and that value is displayed in the size field. So even without knowing the filename to be listed the result can be parsed to extract the target name. Bob