The executable file linked to the names ln
and link
is unusual in that both names exist in the same /bin
directory. More often when a file has more than one link, each filename is stored in a different directory. For example, if two difference collections contain the same image and both collections are stored in different directories, then when prudent space can be saved by storing the image in a single file. A file name can then be stored in each directory with a link back to the file’s inode. In other words, a filename is stored in each directory along with the inode. The file associated with the inode also has an associated reference counter which would be set to 2. The filenames can be different, but the inode would be the same.
As for the executable with the names ln
in link
, only the name link
has special meaning. I will try to illustrate through the following examples. In the first example, an error message is produced. The message shows the command knows its name is ln
.
% ln -z
ln: illegal option -- z
usage: ln [-Ffhinsv] source_file [target_file]
ln [-Ffhinsv] source_file ... target_dir
link source_file target_file
In the next example, the command knows its path and name is /bin/ln
.
% /bin/ln -z
/bin/ln: illegal option -- z
usage: ln [-Ffhinsv] source_file [target_file]
ln [-Ffhinsv] source_file ... target_dir
link source_file target_file
Below, I create a local directory, then copied the command to the directory. Here the command knows its name is now fred
.
% cd ~
% mkdir test
% cd test
% cp /bin/ln fred
% ./fred -z
./fred: illegal option -- z
usage: ln [-Ffhinsv] source_file [target_file]
ln [-Ffhinsv] source_file ... target_dir
link source_file target_file
This time I successfully create a symbolic link.
% echo hi >hi.txt
% ./fred -s hi.txt hi2.txt
% ls -li
total 72
114259724 -rwxr-xr-x 1 davidanderson staff 32160 Mar 28 15:16 fred
114261744 -rw-r--r-- 1 davidanderson staff 3 Mar 28 15:18 hi.txt
114261755 lrwxr-xr-x 1 davidanderson staff 6 Mar 28 15:18 hi2.txt -> hi.txt
However, if the rename the command to link
, then I can not create a new symbolic link. The name link
has a special meaning.
% mv fred link
% ./link -s hi.txt hi3.txt
./link: illegal option -- s
usage: ln [-Ffhinsv] source_file [target_file]
ln [-Ffhinsv] source_file ... target_dir
link source_file target_file
I can however successfully create a hard link. Below both hi.txt
and hi3.txt
have the same inode and the number of links (i.e. reference count) is set to 2.
% ./link hi.txt hi3.txt
% ls -li
total 80
114261744 -rw-r--r-- 2 davidanderson staff 3 Mar 28 15:18 hi.txt
114261755 lrwxr-xr-x 1 davidanderson staff 6 Mar 28 15:18 hi2.txt -> hi.txt
114261744 -rw-r--r-- 2 davidanderson staff 3 Mar 28 15:18 hi3.txt
114259724 -rwxr-xr-x 1 davidanderson staff 32160 Mar 28 15:16 link
Question and answers here at Ask Different are not supposed to get into programming. However, if interested, you can read the pertinent source for the macOS 14.3 version at this link. The main entry point is at line 74. By line 91, the path has been removed and the code is checking to see if the name is link
.