25

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
  • 457
  • 1
  • 7
  • 14

6 Answers6

34

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,346
11

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
  • 18,154
PerlDuck
  • 13,885
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
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
  • 33,561
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,504
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.