ln command in Linux is used to create links between files or directories. It can create two types of links: hard links and symbolic links (also known as soft links).
Looking to get a head start on your next software interview? Pickup a copy of the best book to prepare: Cracking The Coding Interview!
- Hard links: A hard link is a direct reference to a file or directory on the file system. It creates another entry point (link) to the same file, which means that both the original file and the hard link share the same data on the disk. If you modify the contents of the original file, the hard link will also reflect those changes because they are essentially the same file. Hard links cannot be created for directories or across different file systems.
The syntax to create a hard link using the
ln command is:
ln <original_file> <hard_link_name>
For example, to create a hard link named
link1 for a file called
file1.txt, you would use:
ln file1.txt link1
- Symbolic links: A symbolic link is a special type of file that acts as a pointer to another file or directory. It is like a shortcut or alias to the original file or directory. Symbolic links can point to files or directories across different file systems. If you delete the original file or directory, the symbolic link will become broken.
ln -s example
The syntax to create a symbolic link using the
ln command is:
ln -s <original_file> <symbolic_link_name>
For example, to create a symbolic link named
link2 for a file called
file2.txt, you would use:
ln -s file2.txt link2
Symbolic links are often used to create shortcuts or references to files or directories located in different directories or to link libraries in software development.
You can also use the
ln command with additional options to modify its behavior or create links with different attributes. To learn more about these options and additional details, you can refer to the
ln manual page by running
man ln in a terminal.
What is the difference between a hard link and symbolic (soft) link?
The main difference between a hard link and a symbolic link (soft link) lies in how they function and how they reference files or directories:
- Hard Link:
- A hard link creates another entry point to the same data on the disk. It essentially associates multiple file names with the same inode (a data structure that stores information about a file).
- All hard links to the same file are equal, and there is no concept of an original file or a linked file.
- Deleting any of the hard links does not affect the data of the file as long as at least one hard link remains. The file is only deleted when the last hard link is removed.
- Hard links can only be created for files and not directories.
- Hard links cannot cross file system boundaries. They must be created within the same file system.
- Symbolic Link (Soft Link):
- A symbolic link is a special type of file that contains the path or name of another file or directory. It acts as a pointer or reference to the target file or directory.
- Symbolic links are independent files, and they have their own inodes. They store the path or name of the target file or directory.
- Deleting the original file or directory renders the symbolic link as a broken link. It no longer points to a valid target.
- Symbolic links can point to files or directories across different file systems. They provide flexibility in linking files or directories located in different locations.
- Symbolic links can be created for both files and directories.
In summary, hard links are multiple entries that directly point to the same data on the disk, while symbolic links are separate files that act as pointers or references to other files or directories. Hard links are restricted to the same file system and can only be created for files, whereas symbolic links can cross file system boundaries and can be created for both files and directories.