The ln Command
Each hard linked file is assigned the same Inode value as the original, therefore they reference the same physical file location. Hard links are more flexible and remain linked even if the original or linked files are moved throughout the file system, although hard links are unable to cross different file systems. ls -l command shows all the links while the link column shows number of links. Links have actual file contents, and removing any link just reduces the link count, but doesn’t affect other links. We cannot create a hard link for a directory to avoid recursive loops. If the original file is removed, then the link will still show the content of the file.
Command to create a hard link is:
ln [,OPTION/]... [,-T/] ,TARGET LINK_NAME/ (1st form) ln [,OPTION/]... ,TARGET/ (2nd form) ln [,OPTION/]... ,TARGET/... ,DIRECTORY/ (3rd form) ln [,OPTION/]... ,-t DIRECTORY TARGET/... (4th form)
In the first form, the command creates a link to TARGET with the name LINK_NAME.
In the second form, the command creates a link to TARGET in the current directory.
In the third and fourth forms, the command creates links to each TARGET in DIRECTORY. The ln command creates hard links by default, to create symbolic links you must yse the --symbolic option. By default, each destination (name of new link) should not already exist. When creating hard links, each TARGET must exist. Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.
# A simple link of one file to another name - notice the output of thje ls -li command # showing the file's node number is the same for both files, bacause they are links pbmac@pbmac-server $ ln program1.cpp linkprogram1.cpp pbmac@pbmac-server $ ls -li total 0 12987377 -rw-r--r-- 2 pbmac pbmac 0 Aug 4 17:42 linkprogram1.cpp 12987377 -rw-r--r-- 2 pbmac pbmac 0 Aug 4 17:42 program1.cpp # link the file to a file with the same name in the directory subFolder pbmac@pbmac-server $ ln program1.cpp subFolder/ pbmac@pbmac-server $ ls subFolder/ program1.cpp # link the file to a different name in the directory subFolder pbmac@pbmac-server $ ln program1.cpp subFolder/anotherLink.cpp pbmac@pbmac-server $ ls subFolder/ anotherLink.cpp program1.cpp # link a file in a subfolder to a file with the same name in the current folder. pbmac@pbmac-server $ ln subFolder/testFile.txt pbmac@pbmac-server $ ls linkprogram1.cpp myStuff.txt program1.cpp subFolder testFile.txt pbmac@pbmac-server $
The image shows the concept - two different filenames pointing to the same space on the physical disk.
- A symbolic link is similar to the file shortcut feature which is used in Windows Operating systems. Each symbolic linked file contains a separate Inode value that points to the original file. Similar to hard links, any changes to the data in either file is reflected in the other. Symbolic links can be linked across different file systems, although if the original file is deleted or moved, the symbolically linked file will not work correctly (called hanging link).
- ls -li command shows the node numbers, all linked files will show the first permission column value of l, and the filename field points to original file.
- Symbolic link contains the path for original file and not the contents.
- Removing symbolic link doesn’t affect anything, but if removing the original file, the link becomes a “dangling” link which points to nonexistent file.
- A symbolic link can link to a directory.
- Link across filesystems: If you want to link files across the filesystems, you can only use symlinks links.
Symbolic/Soft Link Syntax
$ ln -s [original filename] [link name]
As mentioned above, the symbolic, or soft link, has a different filename, but the link points back to the original filename, not to the disk drive. Notice in the following example, the node numbers are different signifying that these are two different names; also the filename field shows the link back to program1.cpp.
# notice the node numbers are different, the first colum of permissions is a 'l', and filename points back to original file pbmac@pbmac-server $ ls -li program1.cpp newprogram.cpp 12987272 lrwxrwxrwx 1 pbmac pbmac 12 Aug 5 07:55 newprogram.cpp -> program1.cpp 12987377 -rw-r--r-- 4 pbmac pbmac 0 Aug 4 17:42 program1.cpp
This can be viewed as the shown in the image to the right. The linked filename, file2.txt, points back to the original filename. The original filename points to the physical file on disk.
"ln command in Linux with Examples" by Rangeesh_A_R, Geeks for Geeks is licensed under CC BY-SA 4.0
"Soft and Hard links in Unix/Linux" by Akash Gajjar, Geeks for Geeks is licensed under CC BY-SA 4.0