5

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 ?

12

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 jabard domain users 4096 mai  24 15:56 original
    lrwxrwxrwx 1 jabard domain users    8 mai  24 symbolic -> 15:56 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.

  • 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 at 17:11
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

5

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

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

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
  • 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 at 18:06
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.