Linux soft versus hard links!
How it works
Background
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.
One-to-One Live Linux Classes
Coding Bootcamps school offers One-to-One Live Linux Classes for Beginners.
Giving a File More than OneName
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.
Further Insights
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:
- Changing the original or source file permission and see its effect on the soft and hard link
- Editing the original or source file content and its effect on both soft and hard links
- Editing soft link content or permission and its effect on original file
- Editing hard link content or permission and original file
- Removing soft link and its effect on original file
- Removing hard link and its effect on original file
Summary
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.
Resources- Self-Paced Linux Courses
If you like to learn more about Linux, taking the following courses is highly recommended:
- Introduction to Linux OS
- Intro to Linux Bash Scripting
- Advance Linux Bash Scripting
- Linux Management for System Admins
- How to manage Red Hat or CentOS Linux servers- Intermediate level
Resources- Free Courses
Here is the list of our 9 free self-paced courses that are highly recommended:
- IT Career Roadmap Explained
- Web Design with Bootstrap
- User Experience Best Practices
- Intro to Search Engine Optimization
- Web Design with WordPress
- Introduction to Drupal CMS
- Intro to Joomla CMS
- Intro to Cybersecurity
- Introduction to Cloud Technology
Resources- Live Linux Courses
If you like to learn more about Linux, take the following live Linux classes is highly recommended:
- Linux shell and bash scripting via hybrid training
- Hands-on Linux programming for system administration via hybrid training
- Red Hat Certified System Administrator Live Training Class
- Live training class for obtaining Red Hat Certified Engineer certification
- Essential Linux Commands in 6 Hours by Hands-on Exercises
- Linux Bash & Shell Scripts in 6 Hour by Hands-on Exercises
- Linux and bash scripting- Private tutoring sessions
Resources- Tutorials
If you like to learn more about Linux, reading the following articles and tutorials is highly recommended:
- Comprehensive Review of Linux File System Architecture and Management
- Comprehensive Review of How Linux File and Directory System Works
- Comprehensive list of all Linux OS distributions
- Comprehensive list of all special purpose Linux distributions
- Linux System Admin Guide- Best Practices for Making and Managing Backup Operations
- Linux System Admin Guide- Overview of Linux Virtual Memory and Disk Buffer Cache
- Linux System Admin Guide- Best Practices for Monitoring Linux Systems
- Linux System Admin Guide- Best Practices for Performing Linux Boots and Shutdowns