Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get real path of a symlink

Tags:

c

linux

Suppose that I want to get the real path of a symlink. I know that both readlink and stat system calls can dereference the link and give me its real path. Do they operate in the same way (only regarding the dereferencing, I know that stat does lots more)? Should I prefer one over the other?

like image 409
Guy Avatar asked Aug 10 '13 07:08

Guy


People also ask

What is a symlink path?

A symlink (also called a symbolic link) is a type of file in Linux that points to another file or a folder on your computer. Symlinks are similar to shortcuts in Windows. Some people call symlinks "soft links" – a type of link in Linux/UNIX systems – as opposed to "hard links."

How do I copy a symlink to a directory?

We can use the -l option of rsync for copying symlinks. rsync copies the symlinks in the source directory as symlinks to the destination directory using this option. Copying the symlinks is successful in this case. rsync copied file1 to destination_directory.

How do I read a symlink from a file?

readlink -f /path/file ( last target of your symlink if there's more than one level ) If you just want the next level of symbolic link, use : readlink /path/file

How do I get the absolute path of a symbolic link?

Get real path (absolute path) from symbolic link (aka. softlink) On Linux/Unix/Mac systems one can create symbolic links (aka. softlinks) to a file or a directory using the ln -s TARGET or ln -s TARGET LINK . Behind the scenes a symbolic link is just a file that has the TARGET as the content.

How to add the next level of symbolic link to symlink?

( last target of your symlink if there's more than one level ) If you just want the next level of symbolic link, use : readlink /path/file Edit 2020:

How to get full symlink path in AWK?

You can use awk with a system call readlink to get the equivalent of an ls output with full symlink paths. For example: Show activity on this post. Then if I use the symlink to goto dir: cd game If I issuethe cmd: pwd, the actual directory you're in is: ~


2 Answers

Use stat() to tell you about the file at the end of any chain of symlinks; it does not get you the path in any way. Use lstat() to get information about the symlink, if any, that is referred to; it acts like stat() when the name given is not a symlink. Use readlink() to obtain the path name stored in the symlink named as its argument (beware — it does not null terminate the string).

If you want the full pathname of the file at the end of the symlink, you can use realpath(). This gives you an absolute pathname which does not cross any symlinks to reach the file.

like image 159
Jonathan Leffler Avatar answered Oct 10 '22 06:10

Jonathan Leffler


Yeah, you should use readlink() for that. However, notice that it requires that you allocate a buffer to store the dereferenced path in. lstat() can help if you want to allocate a buffer of the exact size that is required, as is shown in the example at the bottom of the readlink() man page.

like image 38
Jorge Israel Peña Avatar answered Oct 10 '22 05:10

Jorge Israel Peña