Find out symbolic link target via command line

Solution 1:

Use the -f flag to print the canonicalized version. For example:

readlink -f /root/Public/myothertextfile.txt

From man readlink:

-f, --canonicalize
      canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist

Solution 2:

readlink is the command you want. You should look at the man page for the command. Because if you want to follow a chain of symbolic links to the actual file, then you need the -e or -f switch:

$ ln -s foooooo zipzip   # fooooo doesn't actually exist
$ ln -s zipzip zapzap

$ # Follows it, but doesn't let you know the file doesn't actually exist
$ readlink -f zapzap
/home/kbrandt/scrap/foooooo

$ # Follows it, but file not there
$ readlink -e zapzap

$ # Follows it, but just to the next symlink
$ readlink zapzap
zipzip

Solution 3:

This will also work:

ls -l /root/Public/myothertextfile.txt

but readlink would be preferred for use in a script rather than parsing ls.