Atlantic.Net Blog

How to Create Soft and Hard Links in Linux

Hitesh Jethva
by Atlantic.Net (273 posts) under Tutorials, VPS Hosting
0 Comments

A link in a Linux-based operating system points to a file or a directory. Links allow more than one file name to refer to the same file. There are two types of links, Soft Links and Hard Links. In simple terms, a soft link is an actual link to the original file, while a hard link is a mirror copy of the original file.

If you delete the original file. you can not access its contents via its soft link. On the other hand, if you delete the original file you can still access its contents via its hard link. The hard link has the same inode number and file permissions while the soft link has the different inode number and file permissions.

In this post, we will show you how to create a soft and hard link in Linux.

Prerequisites

  • A fresh Ubuntu 20.04 server on the Atlantic.Net Cloud Platform
  • A root password configured on your server

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Ubuntu 20.04 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

Create a Soft Link

First, create a directory named softlink with the following command:

mkdir softlink

Next, create a file named normal_link.txt:

cd softlink
echo "This is a softlink" > normal_link.txt

Next, create a soft link of the file normal_link.txt:

ln -s normal_link.txt soft_link.txt

Now, check the content of both files:

cat normal_link.txt

Output:

This is a softlink

And

cat soft_link.txt

Output:

This is a softlink

As you can see, both files have the same content.

Now, check the inode number and permissions of both files:

ls -lia

You should see that both files have different inode numbers and permission.

15866192 drwxrwxr-x  2 vyom vyom 4096 Apr 19 15:10 .
15466497 drwxrwxrwt 13 root root 4096 Apr 19 15:10 ..
15866186 -rw-rw-r--  1 vyom vyom   19 Apr 19 15:09 normal_link.txt
15866189 lrwxrwxrwx  1 vyom vyom   15 Apr 19 15:10 soft_link.txt -> normal_link.txt

Now, remove the original file with the following command:

rm -rf normal_link.txt

Now, check the content of the soft link file:

cat soft_link.txt

Output:

cat: soft_link.txt: No such file or directory

As you can see, there is no such file or directory.

If you want to remove the soft link, run the following command:

unlink soft_link.txt

Create a Hard Link

First, create a directory named hardlink with the following command:

mkdir hardlink

Next, create a file named normal_link.txt with the following command:

cd hardlink
echo "This is a hard link" > normal_link.txt

Next, create a hard link of the file with the following command:

ln normal_link.txt hard_link.txt

Now, check the content of both files:

cat normal_link.txt

Output:

This is a hard link

And

cat hard_link.txt

Output:

This is a hard link

Next, check the inode and permission of both files:

ls -lia

You should see that both files have the same inode and permission:

15866186 drwxrwxr-x  2 vyom vyom 4096 Apr 19 15:16 .
15466497 drwxrwxrwt 14 root root 4096 Apr 19 15:16 ..
15866190 -rw-rw-r--  2 vyom vyom   20 Apr 19 15:16 hard_link.txt
15866190 -rw-rw-r--  2 vyom vyom   20 Apr 19 15:16 normal_link.txt

Now, try to add some content to the original file:

echo "I am adding some content" >> normal_link.txt

Now, check the content of the hard link:

cat hard_link.txt

You should see that the changes we made on the original file are updated in both files.

This is a hard link
I am adding some content

Now, remove the original file:

rm -rf normal_link.txt

Now, check the content of the hard link:

cat hard_link.txt

You should still see the content of the file:

This is a hard link
I am adding some content

If you want to remove the hard link, run the following command:

unlink hard_link.txt

Conclusion

In the above guide, you learned how to create a soft and hard link on Linux. Try creating soft and hard links on your VPS hosting account with Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G3.2GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources