In Linux, a file can have two types of links: soft link and hard link. An understanding of differences between soft and hard links in Linux is crucial for following the topics discussed in this article. Without further due, let’s briefly review them here.
A soft like also known as symbolic is an actual link to the original file in comparison with hard link being only a mirror copy of the original file. Their difference becomes visible when you delete the original file in which case soft link loses its value; whereas the hard link stays intact since it acts as a mirror of the original file. Some of features of soft link are that they have different inode number and file permissions than original file as well as instead of holding file’s contents, they just keep the path to the original file. They also let you link between directories in addition to crossing the file system. Conversely, hard links can not link to the directories while having the same inode number and permission as the original file. If any changes happen to the original file, hard link’s permission will be updated accordingly while soft link stay intact. Here is a good article to learn more about how Linux file system work.
Now that we know the differences between soft and hard link, in this section we learn how to create and manage soft links in Linux. As discussed earlier, a hard link is another name for an existing file; there is no difference between the link and the original file. So if you make a hard link from file ‘boy’ to file ‘sun’, and then remove file ‘sun’, file ‘boy’ is also removed. Each file has at least one hard link, which is the original file name itself. Directories always have at least two hard links—the directory name itself (which appears in its parent directory) and the special file ‘.’ inside the directory. By the same token, when you make a new subdirectory, the parent directory gains a new hard link for the special file ‘..’ inside the new subdirectory. What you take away from this tutorial is essential for performing tasks related to cybersecurity and cloud technology.
A soft link passes most operations—such as reading and writing—to the file it points to, just as a hard link does. However, if you remove a soft link, you remove only the soft link itself, and not the original file. In Linux, we can use ln (“link”) command to make links between files. Give as arguments the name of the source file to link from and the name of the new file to link to. By default, the ln command makes hard links.
To create a hard link from ‘apple’ to ‘garden’, type:
$ ln apple garden
This command makes a hard link from an existing file, ‘apple’, to a new file, ‘garden’. You can read and edit file ‘garden’ just as you would ‘apple’; any changes you make to ‘garden’ are also written to ‘apple’ (and vice versa). If you remove the file ‘garden’, file ‘apple’ is also removed.
To create a soft link instead of a hard link, use the ‘-s’ option.
To create a soft link from ‘apple’ to ‘garden’, type:
$ ln -s apple garden
After running this command, you can read and edit ‘garden’; any changes you make to ‘garden’ will be written to ‘apple’ (and vice versa). But if you remove the file ‘garden’, the file ‘apple’ will not be removed.
To expand on what you have learned and put them into practice, I suggest to create two source (original) files, then create two (one soft and one hard) link files that will be pointing to your source files. Once you 4 files are created, you can move on exploring differences between soft and hard links by experimenting the following 6 scenarios:
In this article, we learn how to create multiple file names in Linux. Specifically, we discussed the differences between soft and hard linking in Linux and under what circumstances soft links outperform hard links. We also mentioned 6 practical scenarios for practicing and mastering how file linking in Linux works.
If you like to learn more about Linux, taking the following courses is highly recommended:
Here is the list of our 9 free self-paced courses that are highly recommended:
If you like to learn more about Linux, take the following live Linux classes is highly recommended:
If you like to learn more about Linux, reading the following articles and tutorials is highly recommended:
Matt Zand is a serial entrepreneur and the founder of 3 tech startups: DC Web Makers, Coding Bootcamps and High School Technology Services. He is a leading author of Hands-on Smart Contract Development with Hyperledger Fabric book by O’Reilly Media. He has written more than 100 technical articles and tutorials on blockchain development for Hyperledger, Ethereum and Corda R3 platforms. At DC Web Makers, he leads a team of blockchain experts for consulting and deploying enterprise decentralized applications. As chief architect, he has designed and developed blockchain courses and training programs for Coding Bootcamps. He has a master's degree in business management from the University of Maryland. Prior to blockchain development and consulting, he worked as senior web and mobile App developer and consultant, angel investor, business advisor for a few startup companies. You can connect with him on LI: https://www.linkedin.com/in/matt-zand-64047871
We offer private custom tutoring classes both online and in DC, MD and VA for almost all of our courses or bootcamps. Give us a call or email us to discuss your needs.
$50 Limited OfferREGISTER NOW