Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say that I setup a symbolic link:

ln -s /root/Public/mytextfile.txt /root/Public/myothertextfile.txt

is there a way to see what the target of myothertextfile.txt is using the command line?

share|improve this question
up vote 59 down vote accepted

readlink /root/Public/myothertextfile.txt

share|improve this answer
As suggested in another question, you may want to use readlink -f. – Denilson Sá Jun 7 '14 at 0:14

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

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

$ # Follows it, but just to the next symlink
$ readlink zapzap
share|improve this answer

This will also work:

ls -l /root/Public/myothertextfile.txt

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

share|improve this answer

If you can't use readlink, then parsing the result of ls -l could be done like this.

The normal result would be:

ls -l /root/Public/myothertextfile.txt
lrwxrwxrwx 1 root root 30 Jan  1 12:00 /root/Public/myothertextfile.txt -> /root/Public/mytextfile.txt

So we want to replace everything before " -> " and the arrow included. We could use sed for this:

ls -l /root/Public/myothertextfile.txt | sed 's/^.* -> //'
share|improve this answer

If you want to show the source and the destination of the link, try stat -c%N files*. E.g.

$ stat -c%N /dev/fd/*
‘/dev/fd/0’ -> ‘/dev/pts/4’
‘/dev/fd/1’ -> ‘/dev/pts/4’

It’s not good for parsing (use readlink for that), but it shows link name and destination, without the clutter of ls -l

-c can be written --format and %N means “quoted file name with dereference if symbolic link”.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.