What are hard and symbolic links on Linux? How to create them, and what is the difference between both?.
Definition:
A symbolic link (it is call a symlink too) is a type of file in Linux that points to another file or a folder on your computer.
This kind of links are pointers to a file. Like pointers in any programming languages, links in LINUX are pointers pointing to a file or a directory. Creating links is a kind of shortcuts to access a file. Links allow more than one file name to refer to the same file.
There are some types of links, but the most important types are hard and soft links, let´s talk about it for a while:
Soft links.
- A soft link is similar to the file shortcut feature which is used in Windows Operating systems. Each soft linked file contains a separate Inode value (An inode is a data structure in UNIX operating systems that contains important information pertaining to files within a file system)that points to the original file. As similar to hard links, any changes to the data in either file is reflected in the other. Soft links can be linked across different file systems, although if the original file is deleted or moved, the soft linked file will not work correctly (called hanging link).
- ls -l command shows all links with first column value l? and the link points to original file.
- Soft Link contains the path for original file and not the contents.
- Removing soft link doesn’t affect anything but removing original file, the link becomes “dangling” link which points to nonexistent file.
- A soft link can link to a directory.
- If we change the name of the original file then all the soft links for that file become dangling.
- Link across file systems: If you want to link files across the file systems, you can only use symlinks/soft links.
Hard links
- 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.
- Links have actual file contents
- Removing any link, just reduces the link count, but doesn’t affect other links.
- Even if we change the filename of the original file then also the hard links properly work.
- We cannot create a hard link for a directory to avoid recursive loops.
- If original file is removed then the link will still show the content of the file.
- The size of any of the hard link file is same as the original file and if we change the content in any of the hard links then size of all hard link files are updated.
- The disadvantage of hard links is that it cannot be created for files on different file systems and it cannot be created for special files or directories.
How can we create these links?
The command for creating those links is ln.
The programming sintax of ln is
ln -s <path to the file/folder to be linked> <the path of the link to be created>
<< where -s flag is to create a soft link, ln creates hard links by default>>
This blog was proudly created by Imanol Asolo (Codecodix). Images were downloaded from medium.com using Google image downloader.
Image link:
Comentarios
Publicar un comentario