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, i.e. users can always reference newlink although the actual file may be original.2

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 (i.e. in the file itself), 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 different attributes, if original is a directory or is on another filesystem
newlink is placed in the current directory unless newlink is a directory then the new link will be newlink/original .
 > ls -l tony/ourNotes 
rwxr-xr-x  1 tony staff 2459 Aug 25 16:51 tony/ourNotes
> ls -l lib/Notes
ls: lib/Notes: No such file or directory
> ln -s tony/ourNotes  lib/Notes
> ls -l lib/Notes
lrwxr-xr-x  1 librarian staff 38 Aug 25 16:51 lib/Notes -> 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.

In this example, the mode indicates librarian can write to the entry, i.e. can change the link.
Should a new version of Notes be created as Notes.v1 the link can be recreated by librian to refer to the new version. This allows the users 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.

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.

-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 does not exist! and return code is zero!)

hard link vs symbolic link.

There is no information in the newlinkfile that a symbolic link to it exists!

Additional date information is available using stat )
Unfortunately, removing the original file without 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

/Volumes/Rx > stat src
 size           access        modification         change         Born   
  82  __-__-__  18:18:58  __-__-__  18:18:58  __-__-__  18:18:58  __-__-__  18:18:58  src

/Volumes/Rx > stat -L src
 size           access        modification         change         Born  
1504  __-__-__  18:26:12  __-__-__  18:26:12  __-__-__  18:26:12  02/21/22  08:00:34  src

/Volumes/Rx > readlink src
/Volumes/DATA/dgerman/Documents/computerstuff/phyweb/ruuvi/firmware/3.31.1/305/src
See lstat,readlink,stat.

To determmine is multiple filenames are indeed the same file (i.e. hardlink, same inode) use

% /usr/bin/stat -f%i /usr/bin/cc
1152921500312524264

% /usr/bin/stat -f%i /usr/bin/gcc
1152921500312524264

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!