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?
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?
Use the
-f
flag to print the canonicalized version. For example:From
man readlink
: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:
This will also work:
but
readlink
would be preferred for use in a script rather than parsingls
.If you want to show the source and the destination of the link, try
stat -c%N files*
. E.g.It’s not good for parsing (use
readlink
for that), but it shows link name and destination, without the clutter ofls -l
-c
can be written--format
and%N
means “quoted file name with dereference if symbolic link”.The
readlink
is a good thing, but GNU-specific and non cross platform. I used to write cross platform scripts for/bin/sh
, therefore I'd use something like:or:
but these needs to be tested on different platforms. I think they'll work, but don't 100% sure for
ls
output format.The result of
ls
can also be parsed withinbash
without depending on an external command likeawk
,sed
orperl
.This
bash_realpath
function, resolves the final destination of a link (link→link→link→final):If you can't use
readlink
, then parsing the result ofls -l
could be done like this.The normal result would be:
So we want to replace everything before " -> " and the arrow included. We could use
sed
for this:The question is not accurate enough to give a simple answer as the one of brian-brazil:
will indeed dereference every symlink involved in the path construct to the final target behind
some_path
.But one level of cascading symlinks is just a particular case among others in a system, the general case being N levels of cascading symlinks. Look at the following on my system:
rwhich
is my own recursive implementation ofwhich
that prints all of the intermediate cascading symlinks (to stderr) down to the final target (to stdout).Then if I want to know what is:
the target of symlink /usr/bin/emacs**, the obvious answer to me is
/etc/alternatives/emacs
as returned by:the final target behind cascading symlinks /usr/bin/emacs, the answer shall be
/usr/bin/emacs24-x
as returned by:ll
orls -l
should list the directory items, including your symbolic link and it's targetcd -P /root/Public/myothertextfile.txt
(in your case) should point to the original pathIf you find the target of all links within a folder and its subfolder use the
find . -type l -ls
command with link type as follows:If you want the target of a single, then
ls -l
should work: