ln [-fhinsv] originalFile
[newlink
]
ln [-fhinsv] originalFile ... 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.
Cannot create a hard link if the newlink
is on another volume or if originalFile
is a directory. Then specify -s
to create a symbolic link.
Both directory entries have the link count increased.
> cd ~
> ln ../yourdirectory/ournotes
Creates a hard link in my home directory to your file notes
. > ls -log docu ../yourdirectory/ournotes
-rwxr-xr-x 2 20 Jan 5 11:43 ournotes
-rwxr-xr-x 2 20 Jan 5 11:43 ournotes
newlink
may be a directory in which to place the new link; otherwise it is placed in the current directory.
If only the directory is specified, the link will be made to the last component of source_file
.
Given more than two arguments, ln
makes links in link_dir
to all the
named source files. The links made will have the same name as the files being linked to.
-i |
link
takes exactly two filenames and creates a hard link. No options may be supplied.
hard link vs symbolic link.
originalFile
and
are indistinguishable from the original.ls -l
includes a link count of the number of directory entries which point to a file.
Changing the mode (permissions), owner, group … of one entry effects all entries.> ./reptPgm > origfile > ls -l origfile -rw-r--r-- 1 dgerman devgrp 2419 Apr 9 13:50 origfile > ln origfile hardLink > ls -l orig* hard* -rw-r--r-- 2 dgerman devgrp 2419 Apr 9 13:50 origfile -rw-r--r-- 2 dgerman devgrp 2419 Apr 9 13:50 hardlink
Specify -s
to create a symbolic directory link.
originalFile
which is used if the entries exist on different volumes or refer to directories.originalFile
is used when an open is performed on the symbolic link.ls -l
displays an l
as the first character, unique date fields (these are part of the directory entry, not the file) and an "arrow" ( ->
) pointing to the originalFile
.
(additional date
information is available using stat
) The link count for both entries may be 1 (if no hardlinks exist).> ls -l * lrwxr-xr-x 1 dgerman devgrp 6 Apr 1 14:08 symlink -> target -rw-r--r-- 1 dgerman devgrp 405 Apr 1 14:06 target
originalFile
full path name.newlink
!
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
BUG: ln -s nofile newlink
does not display a message if nofile
does not exist and return code is set to 0!