I am looking for a way, if it exists, to know if a the path to a folder is a symbolic link or the original path to the folder.
Is there such a thing ?
To determine whether the folder is a symbolic link you can use either of these methods.
GUI Method:
The folder icon will be different. The icon of the folder would have an arrow.
CLI Method
The output of ls -l
will clearly indicate that the folder is a symbolic link and it will also list the folder where it points to.
$ ls -l
total 4
drwxr-xr-x 2 t domain users 4096 mai 24 15:56 original
lrwxrwxrwx 1 t domain users 8 mai 24 15:56 symbolic -> original
Here symbolic
is a symbolic link pointing to original
folder.
The starting l
in lrwxrwxrwx
represents a symbolic link, while d
represents folder (directory), and -
represents file.
If you want to check programmatically whether a given object is a plain directory
or a symbolic link to a directory you may use the test
command. Note that it
returns is directory for both directories and symlinks to directories but only
returns is symlink for symbolic links. Hence:
#!/usr/bin/env bash
mkdir i_am_a_directory
ln -s i_am_a_directory i_am_a_symlink_to_a_directory
for object in i_am_a_directory i_am_a_symlink_to_a_directory; do
if test -d $object; then
if test -L $object; then
echo "$object is a symlink to a directory"
else
echo "$object is just a plain directory"
fi
else
echo "$object is not a directory (nor a link to one)"
fi
done
Result:
i_am_a_directory is just a plain directory
i_am_a_symlink_to_a_directory is a symlink to a directory
test
can also be used on the command line, e.g., test -L foo && echo yes
will print "yes" if and only if it's a symlink.
Alternative approach:
1, using file
file /usr/lib/jvm/jre-9
/usr/lib/jvm/jre-9: symbolic link to /etc/alternatives/jre_9
2, using readlink
readlink -f /usr/lib/jvm/jre-9
/usr/lib/jvm/java-9-openjdk-9.0.4.11-6.fc28.x86_64
Here you can see that path is expanded to link target
file
, if you include the /
character at the end it will report directory
stat vids/
will report directory instead of symlink. Actually same for ls -l somedir/
-it will not report that somedir is symlink, but rather content of that dir
There are many ways, my usual way is using stat
guiverc@ultracrap:~$ stat vids
File: vids -> Videos/
Size: 7 Blocks: 0 IO Block: 4096 symbolic link
Device: fd00h/64768d Inode: 524725 Links: 1
Access: (0777/lrwxrwxrwx) Uid: ( 1000/ guiverc) Gid: ( 1001/ guiverc)
Access: 2019-05-25 00:16:26.708558789 +1000
Modify: 2019-05-25 00:16:26.708558789 +1000
Change: 2019-05-25 00:16:26.708558789 +1000
Birth: -
You'll note my ~/vids is a symbolic link to my ~/Videos/ directory
namei
will break down every component in a path, whether the path leads to a directory or not.
For example my ~/Playlists
is a symlink to Documents/Playlists
, and Documents
itself is a symlink to Dropbox/Documents
:
$ namei Playlists
f: Playlists
l Playlists -> Documents/Playlists
l Documents -> Dropbox/Documents
d Dropbox
d Documents
d Playlists
In this output,
f:
means the pathname currently being resolvedl
means symbolic linkd
means directory (folder)Go to the parent directory of the suspected symlink and do
ls -la
The -a
is in case your file is a .
hidden file.
Then it should start with l
if its a link:
(automl-meta-learning) miranda9~/automl-meta-learning/automl-proj/experiments $ ls -la ~
total 136
drwx------. 18 miranda9 miranda9 4096 May 2 15:55 .
drwxr-xr-x. 23 root root 0 May 2 15:45 ..
drwxrwxr-x. 8 miranda9 miranda9 4096 May 2 12:17 automl-meta-learning
-rw-------. 1 miranda9 miranda9 49858 May 2 15:10 .bash_history
-rw-r--r--. 1 miranda9 miranda9 18 Aug 22 2019 .bash_logout
-rw-r--r--. 1 miranda9 miranda9 176 Aug 22 2019 .bash_profile
-rw-r--r--. 1 miranda9 miranda9 1132 May 2 12:49 .bashrc
drwxrwxr-x. 6 miranda9 miranda9 70 Jan 30 14:29 .cache
lrwxrwxrwx. 1 miranda9 miranda9 26 May 2 15:55 .conda -> /home/miranda9/miniconda3/
see the last one:
lrwxrwxrwx. 1 miranda9 miranda9 26 May 2 15:55 .conda -> /home/miranda9/miniconda3/
starts with an l
and even shows you where it's pointing too.
ls -la
. The-a
is in case your file is a.
hidden file.