Creating links is a useful options many times in certain situations. You can solve dependency issues, save some hard drive space and organize files in any whimsical way you wish to. Links in general are similar to shortcut files in Windows or Aliases in Mac OS. It’s very easy to implement the same in Linux systems. If you wish to directly go to the examples click here, or read through a little theory first and then head to the examples.
While concerned about electronics and computer theory, a hard link is a reference to the inode number of the file. An inode number itself is the reference to the structure of the file on the hard drive.
A hard link is a pointer to the inode of a file. In other words, the inode number of both the original file entry and its hard link will be the same. Whatever changes you make to a file, including relocation of the file and removal will reflect across all the hard links of that file. Almost all POSIX compliant operating systems support multiple hard links to the same file.
. Quoting from the The Free Dictionary, an inode (hard link) is:
A data structure holding information about files in a Unix file system. There is an inode for each file and a file is uniquely identified by the file system on which it resides and its inode number on that system. Each inode contains the following information: the device where the inode resides, locking information, mode and type of file, the number of links to the file, the owner’s user and group ids, the number of bytes in the file, access and modification times, the time the inode itself was last modified and the addresses of the file’s blocks on disk.
So, if you give a command like,
ls -i myFile.txt
It will display the inode number (a unique number identifying each file on the hard drive) of myFile.txt. So, when you create a hard link by
ln -i myFile.txt myLink.txt
Then a hard link called myLink.txt is created which refers to the inode of the file myFile.txt. You can check the inode of both the original file and the hard link my ls -i command as described above. The number will be same. Any changes you make across any hard link of a file will also be reflected in the original file as well.
Directories are files, so multiple hard links to directories are possible; however, their unrestricted creation is usually avoided, because of the cyclic structures this may create. Hence we generally use symbolic or soft links for directory linking.
In essence, a symbolic link is the pointer to the pointer of a file or directory. It merely lists the file/directory name to be accessed by the link. What is means is, unlike in case of Hard links, which only create the pointer for the inode of original file, the symbolic links generate a new inode which points to the inode of the original file or directory.
You can create symbolic links by issuing,
ln -s myFile.txt myLink.txt
You can now check the inode of the original file and the link of that file – myLink.txt – and see that the inode number of both files is not the same.
In general, hard links cannot point to directories, they cannot be pointed to files outside the current logical volume/partition and unlike symbolic links, they are updated when a source is moved or removed.
Let’s consider an example now. The most frequent use of symbolic links that comes to my mind is while using MPD (Music Player Daemon). Generally we set a directory with all out music files as the main database root and then MPD searches recursively in all subdirectories for audio files and builds a database. Most users, like me, want to organize their music in some peculiar way. So, making multiple actual copies of the music on the hard drive wastes a lot of unnecessary space. The answer – symbolic links.
I have ~/Music listed as my main Music directory which MPD scans and plays music from. Now, if I have downloaded Sophomore Jinx – a wonderful instrumental album by Rob Costlow released under CC – and it’s in ~/Downloads directory.
I now have 2 options, one is to stop seeding this album and move it to ~/Music directory for MPD to index it OR, I can create a symbolic link to this album in the Music directory. The second option is obviously better one and I can do it with simple command as,
ln -s /home/aditya/Downloads/Sophomore\ Jinx/ /home/aditya/Music/
And that’s it, as long as the file permissions allow execution and access to the file (755 is good enough) then MPD can index this album via the symbolic link created in ~/Music directory.