Symbolic Links - BASH Script

Hi all,

This is my first message in this forum.
I'd like to know if there is a nice way to get the complete path from a symbolic link.

Example:
When I do a ls -ltr I see this output.

lrwxr-xr-x 1 mmmm users 66 Sep 4 09:58 LINK_SEND -> /apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND

I just want to get the string in bold. I know I can do it with a simple cut, but is there a better way?

Thanks!
Rodrigo from Argentina

You mean apart from "readlink"?

The link points to an executable file. I want to get the directory where the executable file is allocated.

How can I do that?
Thanks in advance

Rodrigo

One way:

$ cat basher
#!/usr/local/bin/bash
string="lrwxr-xr-x 1 mmmm users 66 Sep 4 09:58 LINK_SEND -> /apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND"
echo $string
string=${string##* }
echo $string
string=${string%/*}
echo $string
exit 0
$
$
$ ./basher
lrwxr-xr-x 1 mmmm users 66 Sep 4 09:58 LINK_SEND -> /apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND
/apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests/TARGET_SEND
/apps/aaa/home/mmmm/FFFFF/Apps/RRRRRR/unit_tests
$

If you have the readlink command:

file=/path/to/file  ## adjust to taste
target=$( readlink "$file" )

If not:

file=/path/to/file  ## adjust to taste
temp=$( ls -l "$file" )
target=${temp#* -> }

Then you can extract the directory portion and the filename portion from $target:

target_dir=${target%/*}
target_file=${target##*/}