Have you ever thought about shortcuts when navigating through the linux filesystem? I am not referring to creating desktop shortcuts though. I accidentally bumped into the
link also called
ln command and the concept amazed me. I decided to write on it.
The ln utility creates a new directory entry (linked file) which has the same modes as the original file. It is useful for maintaining multiple copies of a file in many places at once without using up storage for the copies; instead, a link points to the original copy. There are two types of links; hard links and symbolic links. How a link points to a file is one of the differences between a hard and symbolic link.
Does the above quote makes sense to you? To some, perhaps not. Note that the above definition is what shows on the
man page of the linux shell.
ln creates links between files
The above is my preferred definition. But what is a link?
A link is an entry in your filesystem that connects a file name to the actual data on disk. This implies that with multiple file names, we can have all of them point to a single data on disk using a link. Let’s illustrate:
# Create a simple file and name it first.txt echo "A sample file" > first.txt # Display the contents of the file cat first.txt
In creating this file, the filesystem wrote the data to disk (talking about the actual bytes). This is nothing special. Another thing also happened! The filesystem also linked this data(now on disk) to the filename
first.txt. Confusing? The trick is to notice that the filename
first.txt and the actual data
A sample file(the actual bytes of data) are 2 separate entries in the filesystem. This implies that renaming does not alter the actual data on disk.
At this point, it is safe to ask questions. What happens to the data and the link when we delete something? How can we create manual links?
The popular command for deleting a file/directory is the
rm command. Basically, deleting a file means deleting one of the links to its data. No wonder after calling
cat on the given file name we get the well known results:
cat: first.txt: No such file or directory
Similarly, we can also delete a file by
unlink first.txt is same as
I hope it makes sense now. Don’t worry if you still don’t get it. Perhaps, learning how to create links will make things clearer. How then do we manually create
links? Let’s get into some commands to examine this:
# Create a simple file and name it first.txt echo "A sample file" > first.txt # Display the contents of the file cat first.txt # Create another link to a filename link first.txt second.txt # Display the contents of the second file cat second.txt # Append data to the first file echo "Some extra data" >> first.txt # Display contents of first.txt cat first.txt # Confirm changes from second.txt cat second.txt
Is it not amazing? Both files reflected the changes. Why? Because they both are
linked to the actual file on disk where the change was applied. It is clear now that by using the
ln or the
link command, a link to the actual data on disk was created successfully. We can both smile.
We can only access data on disk only if there is a link to it. When files are removed, we are only removing links but the actual data is still persisted on disk.
Hmm, still coming up with questions? What if I am working on a partitioned hard drive? Will it work across different different partitions? The answer is no. Linking only works in the same filesystem.
Now the hard stuffs. If you had browsed the
man page for
link, you would probably come across
hard links and maybe assumed there is also
soft links. Well, its kind of similar to hard and soft liquor and you are right. Both do
soft links really exist? What are they?
It is very relevant to note that what we have talked about so far is
hard links. A
soft link preferrably called
symlinks for short, links to another link instead of linking to the actual data on disk. Whew!! A mouthful eh. No worries. Let’s illustrate with some commands:
# Create a simple file and name it first.txt echo "A sample file" > first.txt # Display the contents of the file cat first.txt # Create another link to a filename link -s first.txt second.txt # Display the contents of the second file cat second.txt
You may have noted that symlinks are created with the
-s flag. One key difference is that removing the file that symlinks point to will break the link. Confused? Let’s delve into more commands:
# Remove first.txt rm first.txt # Display second.txt cat second.txt
What happened after removing
first.txt? The famous result:
cat: second.txt: No such file or directory
second.txt still exists, we should not be confused with the error response.
second.txt is a broken symlink. The specialty of symlinks comes into play when we deal with directories. Hard links cannot be used on directories but soft links can.
ln -s documents/ docs
The above command creates a symlink to the documents directory with the name
docs. So when we work and manipulate files in the
docs directory, its more like we are in the
Now the conclusion. I am sure you had lots of fun trying to understand
ln. I personally use them for creating shortcuts on my filesystem. You know what? When setting up your server blocks with nginx, links will be invaluable. You want to know? Go search online!