Objectives
Allowing a single file to appear existing in multiple directories / locations via hard links or soft links.
Prerequisites
- Linux Operating System (Preferably CentOS)
Steps
Step 1: Creating a Hard Link
A hard link maintains the same set of permissions and access times of the original file.
To create a hard link, simply use the ln command.
In the example below, a hard link of testfile is created in /home/testfile2
ln testfile /home/testfile2
To check your new hard link, simply run the ls -la command.
ls -la
If you received an error upon running the ln command such as the following: –
This simply means that you are trying to establish a hard link between 2 different partitions.
Do note that hard links cannot be established between different file systems or partitions.
To resolve this, simply ensure the hard link is created within the same file system and partition. Alternatively, you may use soft links / symbolic links.
Step 2: Creating Symbolic Links
On the other hand, symbolic links / soft links provide greater flexibility compared to hard links as they can: –
1. Create links between different file systems and partitions.
2. Create links to nonexistent files.
3. Create links to directories or files.
To create a symbolic link, simply add the -s option in the ln command.
In the example below, a soft link is created to testfile3 (nonexistence & partition 1) with /home/testfile2 (exist & partition 2).
ln -s /home/testfile2 testfile3
Verify the link by running the ls -la command.
Step 3: Removing Symbolic Links
To remove a symbolic link, simply use the rm command.
In the following example, we will remove the symbolic link with testfile3.
rm testfile3
The above will trigger a warning message stating to confirm the deletion. Simply press y to proceed.
Check the directory again with ls -la, the symbolic link should be removed.