72

When you do

cat some-symlink-to-some-real-file

it shows the contents of the real file, not what is within the symlink itself. Is there a way to see what's actually in it?

bpeterson76
  • 12,918
  • 5
  • 49
  • 82
fabio
  • 2,269
  • 5
  • 22
  • 34
  • 2
    What programming language were you wanting to do this from? – Ben Voigt Jan 01 '11 at 01:30
  • 4
    The underlying system call is `readlink(2)`. As noted in one of the answers, the direct command to use is `readlink(1)` on some systems (GNU and relatives). On other systems - HP-UX, Solaris, AIX - the closest approach is likely to be `ls -l`, but be aware of problems if the path name in the link contains newlines or other weird characters. – Jonathan Leffler Jan 01 '11 at 01:33

5 Answers5

84

The ls -l command will show you that:

$ ls -l foo
lrwxrwxrwx 1 user group 11 2010-12-31 19:49 foo -> /etc/passwd

Or the readlink command:

$ readlink foo
/etc/passwd

So, the symbolic link foo points to the path /etc/passwd.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • 28
    Just to make this clearer, the "contents" of the symlink are nothing more than the name it points too. – wnoise Jan 01 '11 at 00:56
  • 1
    @Ben: meh, shell is a programming language in the right hands, so the distinction is not entirely clear-cut. – ijw Jan 01 '11 at 21:15
  • 1
    @ijw: True that. However we don't have to pin that line down exactly in order to figure out that this isn't on the programmer side of it. Besides, these aren't even shell commands, `ls` and `readlink` are both separate applications. – Ben Voigt Jan 01 '11 at 21:23
  • @Ben: Again, meh. Most lines in scripts run separate programs, such is the nature of Unix. This line is a broad and grey one. It seems reasonably likely he wants it for a script rather than to use on the command-line, though, which makes is analogous to a 'what's the library function for doing xxx' question in other languages. – ijw Jan 01 '11 at 21:40
  • 1
    @CMCDragonkai A hardlink doesn't have contents per se. It just points to an inode. You can get the inode with `stat` or `ls -di`. – wjandrea Dec 28 '17 at 03:04
10

You can call the readlink(2) function, which will place the linked-to name into a buffer.

Note that the result has a length (stored in the return value) rather than being NUL-terminated. So if you want to use it as a string, append a NUL yourself.

Most higher-level/scripting languages, such as perl or python, will provide a readlink wrapper that converts to the usual language-appropriate string type, so you won't be bothered by details such as NUL-termination.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
3

Regarding to man page http://man7.org/linux/man-pages/man7/symlink.7.html symlink is regular file (with special flag) with path to target in its content. So you could copy symlink to FAT partition and read it content there.

Pavel Patrin
  • 1,630
  • 1
  • 19
  • 33
1

Try

find . -type l -exec ls -la {} \;
Eric Fortis
  • 16,372
  • 6
  • 41
  • 62
  • This command will find all the links starting in the current dir. Essentially the ls -l, is in charge of showing the content of each one. – Eric Fortis Jan 01 '11 at 01:12
0

See result of ls -l below. Size of each link is exactly how many bytes is needed for storing name of the original file. Not even final NUL is there, as @BenVoigt mentions. And surely it is not the absolute path of the file.

result

Ashkan Sarlak
  • 7,124
  • 6
  • 39
  • 51