4

Earlier, I had created some links to another folder using the following command.

sudo ln -s path_name link_name 

These links are looking similar to the folders. I am not able to differentiate between a folder and a link. Is there a way to find the difference between a folder and a link to another folder?

terdon
  • 98,183
  • 15
  • 197
  • 293
mateen
  • 657
  • 2
  • 7
  • 18
  • when i type command ls, i see folder names in different colors. Is the link displayed in blue colour or is the folder displaying in blue color ? – mateen Feb 13 '15 at 05:52
  • I m working in terminal.No GUI :( i am not allowed to use a GUI editor. – mateen Feb 13 '15 at 05:56
  • 1
    It is the light blue/green ones (see http://askubuntu.com/a/17304/72216) – Jacob Vlijm Feb 13 '15 at 06:05
  • thanks that had the answer I was looking for. but do you know any other way around instead of color, like can we find the properties of the folder which shows it is a link and a folder ? – mateen Feb 13 '15 at 06:15
  • 1
    ls -l shows a `->` is that what you mean? – Jacob Vlijm Feb 13 '15 at 06:18
  • waow that's just another great way of what i need, dude seriously make an answer for this question. – mateen Feb 13 '15 at 06:20
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/21089/discussion-between-mateen-and-jacob-vlijm). – mateen Feb 13 '15 at 06:22
  • There are no such things as 'folders' and 'folder links'. There are, however, 'directories' and 'symbolic (sym) links'. – Agi Hammerthief Feb 13 '15 at 18:18

4 Answers4

6

There are many ways to see this. First of all, it is indicated in the output of ls -l. Note that the directory has a d at the beginning of the permissions field while the link has an l:

$ ls -l
drwxr-xr-x 2 terdon terdon 4096 Feb 13 14:12 bar
lrwxrwxrwx 1 terdon terdon    3 Feb 13 14:12 foo -> bar

You can also use file:

$ file bar foo 
bar: directory 
foo: symbolic link to `bar' 

Another choice is readlink which follows symbolic links to their targets:

$ readlink foo
bar

Running readlink bar will return no output (it fails, returning a non-0 exit code), so you can do something like:

readlink bar || echo "Not a link!"

or

readlink foo && "Echo this is a link"

Finally, you could also use find or the shell itself to list all links:

find . -type l

or

for f in *; do [ -L "$f" ] && echo "$f is a link"; done
terdon
  • 98,183
  • 15
  • 197
  • 293
5

You could recognize a link by either a small arrow (->) in the output of ls -l:

enter image description here

or by the distinguished color, also appearing in the output of ls, as described in this answer (test2 is the link):

enter image description here

Jacob Vlijm
  • 82,471
  • 12
  • 195
  • 299
2

Read man [ (or man test)and you will see you can do:

for theDir in path_name link_name ; do  
    if [ -L $theDir ] ; then  
        echo "$theDir exists and is a symbolic link"   
    elif [ -d $theDir ] ; then  
        echo "$theDir exists and is a directory"  
    fi  
done

I changed the order of the tests, so checking for a directory is done only if $theDir is not a link.

waltinator
  • 35,099
  • 19
  • 57
  • 93
1

use ls -F .

this will make ls append characters to filenames

* for executable files

/ for directories

@ for symlinks**

| for FIFOs

> for doors (whatever this means)

= for sockets

regular files don't have classifier at the end. So in your case you'll see @ at the end of each symlink name and / at the end of real directory.

However if you use ls -l you won't see classifiers for symlinks and instead you'll see -> and link target after that.

You can also use stat and file to differentiate dirs/symlinks e.g:

$ file a                                                                                                                                                                                     
a: directory

$ file b                                                                                                                                                                                      
b: symbolic link to a

$ stat a                                                                                                                                                                                  
  File: 'a'
  Size: 40          Blocks: 0          IO Block: 4096   directory
 [...]

$ stat b                                                                                                                                                                                    
  File: 'b' -> 'a'
  Size: 1           Blocks: 0          IO Block: 4096   symbolic link
  [...]
suawek
  • 11
  • 1