is there a way to see the actual contents of a symlink?
Asked Answered
P

5

76

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?

Pd answered 1/1, 2011 at 0:35 Comment(2)
What programming language were you wanting to do this from?Swath
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.Penland
K
91

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.

Kial answered 1/1, 2011 at 0:50 Comment(5)
Just to make this clearer, the "contents" of the symlink are nothing more than the name it points too.Velocipede
@Ben: meh, shell is a programming language in the right hands, so the distinction is not entirely clear-cut.Costanzo
@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.Swath
@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.Costanzo
@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.Fascia
S
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.

Swath answered 1/1, 2011 at 1:27 Comment(0)
A
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.

Akilahakili answered 22/3, 2015 at 12:5 Comment(0)
G
1

Try

find . -type l -exec ls -la {} \;
Groark answered 1/1, 2011 at 0:47 Comment(1)
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.Groark
P
1

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

Photocompose answered 27/5, 2023 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.