ln, link

Create additional pointers to a file

ln [-fhinsv] original[] [newlink]
ln -s[-fhinv] originalDirectory link_dir

link originalFile newlink

Creates a new directory entry resulting in multiple appearances of a file, perhaps in a different directory or to provide a consistant reference for new versions of a file.

By default creates a hard link, both entries have the link count increased.

> ln ../tony/ourNotes     #Creates a hard link in my home directory to your file ourNotes
> ls -l ../tony/ourNotes
-rwxr-xr-x 2 tony devGroup  23463 Jan  5 11:43 ourNotes 
> ls -l ourNotes
-rwxr-xr-x 2 tony devGroup  23463 Jan  5 11:43 ourNotes 
As attributes (modes, owner, group) are NOT stored in the directory, both instances will have the same attributes.
> chmod g+w ourNotes
> ls -l ourNotes
-rwxrwxr-x 2 tony devGroup  23463 Jan  5 11:43 ourNotes 
> ls -l ../tony/ourNotes
-rwxrwxr-x 2 tony devGroup  23463 Jan  5 11:43 ourNotes 

Specify -s to create a symbolic link if the newlink needs to have diffreent attributes, if original is a directory or is on another filesystem.
newlnk is placed in the current directory unless newlnk is a directory then the new link will be newlnk/original .

> cd librian
> ln -s tony/ourNotes  tony+samNotes
> ls -l tony+samNotes
lrwxr-xr-x  1 librarian staff 38 Aug 25 16:51 tony+samNotes -> tony/ourNotes
It is unfortunate that the order of arguments is reversed from what is shown by ls -l .
The bytes is actually the length of the path to the original.
Although the mode indicates that librarian can write to the entry, what this means is that librian can change the link. In this example since the file ourNotes is writable by the devGroup, librarian will not be able to write to it or change its mode.

Should a new version of ourNotes be created as ourNotes.v1 the link for librarian can be recreated to refer to the new version. This allows the librarian to always refer to the same name.

If only the directory is specified, the link will be made to the last component of original.

Given multiple originals, makes links in link_dir to the named originals with the same name.

-i interactivey prompts standard error with:"replace newlink?" if newlink exists.
A response from standard input begining with y unlinks ( deletes ) newlink then creates newlink.
overrides any previous -f
-f force creation if the newlink file already exists.
overrides any previous -i .
-v verbose, showing files as they are processed, as in ln -v prefix*
-h,n If the newlink_file or link_dir is a symbolic link, do not follow it. Useful with -f to replace a symlink which may point to a directory.
-s symbolic link is created. ( No message is generated if original/var> does not exist! and return code is zero!) /table>

hard link vs symbolic link.

    removing a directory entry reduces the link count but does not delete the file until the last entry is removed.
    Hard links may referer to different directories but may not span file systems or volumes and may not refer to directories
Additional date information is available using stat )
There is no information in the target file that a symbolic link to it exists!

Unfortunately, removing the target file with out removing the synbolic link, causes accesses to the symbolic link to report: No such file or directory!

 
> ln -s "/Volumes/BACKUPS/notTimeMachine" mirror3
> ls -l mirror3
lrwxr-xr-x  dgerman  staff  33 Sep7 22:22 mirror3 -> /Volumes/BACKUPS/notTimeMachine
> ls -ld mirror3
lrwxr-xr-x  dgerman  staff  33 Sep7 22:22 mirror3 -> /Volumes/BACKUPS/notTimeMachine
> stat mirror3
234881028731036 lrwxr-xr-x dgerman (501) 0 33 
  "Sep7 22:22:34 2008" "Sep7 22:22:34 2008" "Sep7 22:22:34 2008" "Sep7 22:22:34 2008" 4096 8 0 mirror3
> readlink mirror3
/Volumes/BACKUPS/notTimeMachine
> cd mirror3
 mirror3 > 

Symbolic links may span file systems and may refer to directories. stat on a symbolic link will return information about newlink.
stat -L displays information about the originalFile.
readlink is used to read the contents of a symbolic link and shows the path to the originalFile
See lstat,readlink,stat.

Errors

Cross-device link : use -s to create a symbolic link.
Is a directory : use -s

BUG: ln -s nofile newlink does not display a message if nofile does not exist and return code is set to 0!