:using the ln command in bash to distinguish between hard and symbolic links
Stated below is an excerpt from the unix BSD General Commands Manual on ‘ln’. In the description, I have only copied one option, which is relevant to this tutorial, and I have left out the majority of the content from the manual.
NAME: link, ln — make links SYNOPSIS: ln [-Ffhinsv] source_file [target_file] DESCRIPTION: The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the ``copies’’; instead, a link ``points’’ to the original copy. There are two types of links; hard links and symbolic links. How a link ``points’’ to a file is one of the differences between a hard and symbolic link. The options are as follows: -s Create a symbolic link.
A practical use of the ln command can be first demonstrated here with excerpts from my terminal input and output, which I will then explain in order to demonstrate the difference between hard and symbolic links.
USER@computerName: ~/$ ls -la total 4604 drwxrwxr-x 3 vagrant vagrant 4096 Feb 7 17:21 . drwxr-xr-x 8 vagrant vagrant 4096 Feb 7 04:53 .. -rw-rw-r-- 1 vagrant vagrant 96 Feb 6 06:12 holberton -rw-rw-r-- 1 vagrant vagrant 48 Feb 6 06:05 holberton~ -rw-rw-r-- 1 vagrant vagrant 2339220 Jan 17 23:14 nasa_19950801.tsv USER@computerName: ~/$ touch aFile.txt USER@computerName: ~/$ ln aFile.txt aHardLink USER@computerName: ~/$ ln -s aFile.txt aSymLink USER@computerName: ~/$ ls -la total 4604 drwxrwxr-x 3 vagrant vagrant 4096 Feb 7 17:21 . drwxr-xr-x 8 vagrant vagrant 4096 Feb 7 04:53 .. -rw-rw-r-- 2 vagrant vagrant 0 Feb 7 17:23 aFile.txt -rw-rw-r-- 2 vagrant vagrant 0 Feb 7 17:23 aHardLink lrwxrwxrwx 1 vagrant vagrant 9 Feb 7 17:24 aSymLink -> aFile.txt -rw-rw-r-- 1 vagrant vagrant 96 Feb 6 06:12 holberton -rw-rw-r-- 1 vagrant vagrant 48 Feb 6 06:05 holberton~ -rw-rw-r-- 1 vagrant vagrant 2339220 Jan 17 23:14 nasa_19950801.tsv
Now, to understand the difference between the two links:
When I input ‘touch’ as the command to create aFile.txt, that file is stored in memory, which is a circuit somewhere on your computer. This file also has an inode corresponding to the file, which is like an “address” in C Programming Language, or the address of your home. It indicates where your home is or where the file is on your computer.
When you input a command or click your mouse to edit or use aFile.txt, the computer will use the inode as a reference to the information on your computer. A hard link or “aHardLink” from my example, links to that inode, and so it could be considered a copy of the same content that is stored in the inode or aFile.txt. Referencing the hard link references the same information that is in the original file. Editing anything from aHardLink will make changes to the same storage that aFile.txt references. Hard Links also can only link to files.
From the above explanation, you can see how a hard link is like a copy of a file. Well, a symbolic link is more like a link to the file and not a copy. Alternative to hard links, symbolic links can link to directories and files. So, the symbolic link only references content if the original file or directory exists. In my above example, deleting aFile.txt, makes aSymLink reference no content at all because it was deleted when you aFile.txt was deleted. However, if you delete aFile.txt, aHardLink continues to exist and references all the data that was stored in aFile.txt. Your duplicate hard link copy will still point to the file and you will still have access to that file. If you delete aHardLink, then you also delete all the storage of that content on the computer that the inode referenced.