20

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 ?

Joseph Budin
  • 407
  • 1
  • 5
  • 13

6 Answers6

27

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.

    icon difference

  • 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.

tatsu
  • 3,107
  • 1
    Note that symlinks aren't directories themselves, they can just point to directories, though the GUI blurs the lines a bit. But anyway, that's not important for a newbie to know. – wjandrea May 25 '19 at 17:11
  • one thing I learned is that you need to be in the parent directory because if you do it directly to the command you think is the link it will ls the directory's contents (which is the symlink) instead of the actual directory/symlinks status/type. – Charlie Parker May 02 '20 at 20:58
9

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
Kulfy
  • 17,696
PerlDuck
  • 13,335
  • 2
    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. – fkraiem May 25 '19 at 18:06
7

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

rkosegi
  • 229
  • Worth noting that, using file, if you include the / character at the end it will report directory – hytromo May 25 '19 at 16:24
  • 1
    @hytromo : you can add similar comment to @guiverc answer. 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 – rkosegi May 25 '19 at 16:31
  • Thanks that's actually really worth noting! – hytromo May 25 '19 at 16:53
  • You can suppress the behaviour of ls by passing it the -d command. – LSpice May 26 '19 at 01:07
  • 1
    Also, possibly @rkosegi misread "that's actually really worth noting" as "that's actually really worth nothing"? – LSpice May 26 '19 at 01:08
6

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

guiverc
  • 30,396
4

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 resolved
  • l means symbolic link
  • d means directory (folder)
  • Each level of indentation represents a level of symlinks
wjandrea
  • 14,236
  • 4
  • 48
  • 98
2

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.

Charlie Parker
  • 425
  • 1
  • 5
  • 11