CompTIA Linux+ XK0-005 - 1.2 - Soft and Hard Links
Links provide a way to reference and access files, allowing for efficient organization and resource sharing. In this guide, we will explore soft links (symbolic links) and hard links, including their differences and how to create them.
Lesson Setup
A separate folder to practice the objective in can be setup:
Create & change into lesson directory. Set up files and folders for linking to in upcoming exercises:
mkdir -p ~/links-practice/dir1
cd ~/links-practice
echo "Soft Link Practice File" > dir1/file1
echo "Hard Link Practice File" > dir1/file2
Soft Links (Symbolic Links)
Soft links, also known as symbolic links, create separate entries that point to the original file or directory. They act as shortcuts or aliases to the target file or directory. Soft links are represented as separate files with their own names and locations.
Soft links can cross filesystem boundaries, meaning they can reference files or directories located on different file systems or partitions.
Creating a Soft Link
To create a soft link, use the -s
option with the ln
command, followed by the source file and the desired name and location of the soft link. Using the -v
option will display the link name that gets created and the file/directory it links to.
Creating a soft link called soft-link-to-file1
that points to dir1/file1
:
ln -v -s dir1/file1 soft-link-to-file1
ls -l soft-link-to-file1
cat soft-link-to-file1
The output of the ls
command shows that soft-link-to-file1
is a symbolic link (l
in the first column) with the arrow (->
) indicating its target (dir1/file1
). Using the cat
command against the link validates that it really does display the contents of file1
.
Creating a soft link called soft-link-to-dir1
that points to folder dir1
:
ln -v -s dir1 soft-link-to-dir1
ls -l soft-link-to-dir1
cd soft-link-to-dir1
pwd
pwd -P
The output of the ls
command shows that soft-link-to-dir1
is a symbolic link (l
in the first column) with the arrow (->
) indicating its target (dir1
). Using the cd
command against the link validates that we can change directory, however we need to confirm what actual directory we are in.
The pwd
command on its own will show that you are in the directory with the link name soft-link-to-dir1
, but when using the -P
option the pwd
command will display the physical directory you are in, dir1
.
Hard Links
Hard links, unlike soft links, directly reference the data blocks of the original file or directory. They create additional names that point to the same physical file. All hard links share the same file attributes and metadata.
When listing the contents of a directory containing a hard link, the link appears as a separate entry with its own name, but it points to the same physical file or directory.
It is important to note that deleting a hard link does not delete the original file as long as there are other hard links pointing to it. The data remains accessible until all hard links are removed.
Hard links cannot cross filesystem boundaries. They can only reference files located within the same file system or partition.
Creating a Hard Link
To create a hard link, use the ln
command followed by the source file and the desired name and location of the hard link. Using the -v
option will display the link name that gets created and the file/directory it links to.
Creating a hard link called hard-link-to-file2
that points to dir1/file2
:
cd ~/links-practice
ln -v dir1/file2 hard-link-to-file2
ls -l hard-link-to-file2
cat hard-link-to-file2
The output of the ls
command shows that hard-link-to-file2
hard link (number 2
after the file permissions). Using the cat
command against the link validates that it really does display the same contents as file2
as they both point to the same data.
Removing Links
To remove a soft or hard link, you can use the unlink
command followed by the name of the link.
Remove the file and directory soft links created earlier:
unlink soft-link-to-file1
unlink soft-link-to-dir1
ls -l soft-link-to-file1 soft-link-to-dir1
unlink
has removed the links, confirmed by running the ls
command which will give No such file or directory
messages.
Remove the hard link created earlier:
unlink hard-link-to-file2
ls -l hard-link-to-file2
unlink
has removed the link, confirmed by running the ls
command which will give a No such file or directory
message.