Skip to content
  • Linkedin
  • Youtube
  • Pinterest
  • Home
  • Privacy Policy
  • About
  • Contact
Programmingoneonone

Programmingoneonone

Programmingoneonone is a website that publishes daily tutorials, methods, guides, and articles on IT, Education, and technology.

  • Home
  • Human Values
  • DSA
  • IoT Tutorials
  • Interview Questions and Answers
  • Toggle search form
hard and synbolic links in linux

Hard links and Symbolic links

Posted on 3 June 2023 By YASH PAL No Comments on Hard links and Symbolic links

Unix files consist of two parts: the data part and the filename part. The data part is called an ‘inode’. The inode carries the map of, where the data and the file permissions for the data exist.

More than one filename can reference the same inode number; these files are said to be hard linked’.

There’s a special file type whose data part carries a path to another file. Since it is a special file, the OS recognizes the data as a path, and redirects to reads and writes so that, instead of accessing the data within the special file, they access the data in the file named by the data in the special file. This special file is called a ‘soft link’ or a ‘symbolic link’.

When a directory is built, it is initially populated with the filename parts of two special files and files. The filename part for the file is populated with the inode# of the directory file in which the entry has been made; is a hard link to the file that implements the current directory.

The filename part for the ‘..’ file is populated with the inode # of the directory file that contains the filename part of the current directory file. ‘..’ is a hard link to the file that implements the immediate parent of the current directory.

The ‘ln’ command knows how to build hard links and soft links; the ‘mkdir’ command knows how to build directories. There are restrictions on what can be hard-linked that are not applicable to soft links, softlinks have other restrictions not shared by hard links.

ln

It is used to establish more than one link for a file.

$ln abc newabc

This establishes one more link for the file abc in the form of the name newabc. If we take a long listing we find that these file show 2 links.

ln in action

[root@localhost] $ ls -lia
total 3

7347  drwxr-xr-x  12  root  users  1024   May 12 21:18
9180  drwxr-xr-x 19   root  users  2048  May 12 21:18

[root@localhost]$ echo”how r u”>name.file
[root@localhost]$ ls -lia
total 4
7347  drwxr-xr-x  12  root users     1024 May 12 21:18
9180  drwxr-xr-x 19   root users    2048 May 12 21:18
7348  -rw-r—       1     root users    15 May 12 21:18 name.file

[root@localhost]$ cat name.file
how r u
Now, make a hardlink to the file
[root@localhost]$ in name.file had.file
[root@localhost]$ ls -lia
total 5
7347   drwxr-xr-x    12 root users    1024 May 12 21:18
9180   drwxr-xr-x    19 root users    2048 May 12 21:18
7348   -rw-r—          1 root users      15 May 12 21:18 name.file
7348   -rw-r—          2 root users      15 May 12 21:18 hd.file

[root@localhost]$ cat hd.file
how r u
We see that:
(a) hd file shares the same inode (7348) as basic.file
(b) hd file shares the same data as basic file.

If we change the permissions on basic.file:
[root@localhost]$ chmod a+w basic.file

[root@localhost]$ ls -lia
total 5

7347 drwxr-xr-x 12 root users 1024 May 12 21:18
9180 drwxr-xr-x 19 root users 2048 May 12 21:18
7348 -rw-rw-rw- 1 root users 15 May 12 21:18 name.file
7348 -rw-rw-rw- 2 root users 15 May 12 21:18 hd.file

then the same permissions change on hardlink.file.
The two files (name.file and hd.file) share the same inode and data, but have different file names.
Now make a softlink to the file:

[root@localhost]$ ln -s name.file sft.file
[root@localhost]$ ls -lia
total 5

7347 drwxr-xr-x 12 root users 1024 May 12 21:18
9180 drwxr-xr-x 19 root users 2048 May 12 21:18 ..
7348 -rw-rw-rw- 1 root users 15 May 12 21:18 name.file
7348 -rw-rw-rw- 2 root users 15 May 12 21:18 hd.file
7349 lrwxrwxrwx 1 root users 15 May 12 21:24 sft.file -> name.file

[root@localhost]$ cat sft.file
how r u
Here, we see that although softlink. File accesses the same data as basic.file and hardlink.file, it does not share the same inode (7349 vs 7348), nor does it exhibit the same file permissions. It shows a new permission bit: the ‘I’ (softlink) bit.

If we delete name.file:
[root@localhost]$ rm name.file
[root@localhost]$ ls -lia
total 4

7347 drwxr-xr-x 12 root users 1024 May 12 21:18
9180 drwxr-xr-x 19 root users 2048 May 12 21:18 ..
7348 -rw-rw-rw- 1 root users 15 May 12 21:18 hd.file
7349 lrwxrwxrwx 1 root users 15 May 12 21:24 sft.file -> name.file

Then we lose the ability to access the linked data through the softlink:
[root@localhost]$ cat sft.file
cat: softlink.file: No such file or directory

But, we can access to the original data through the hardlink:
[root@localhost]$ cat hd.file
how r u

We will notice that when we deleted the original file, the hardlink did not vanish. Similarly, if we had deleted the softlink, the original file would not have vanished.

When deleting files, the data part isn’t disposed of until all the filename parts have been deleted. There’s a count in the inode that indicates how many filenames point to this file, and that count is decremented by 1 each time one of those filenames are deleted When the count makes it to zero, the inode and its associated data are deleted.

Soft or symbolic is more of a short – cut to the original file. If we delete the original file the shortcut fails and if we delete the short cut nothing happens to the original. While the hard link is a mirror copy of the original.

Advantage of linking

One file is to be shared in black and white with several users. This avoids duplication of the same file contents in different directories.

$cp -l abc newabc

This command works the same as the ln command.

Soft/Symbolic Links

  • Pointers to programs, files, or directories located elsewhere.
  • If the original program, file, or directory is renamed, moved, or deleted then the soft link is broken.
  • If we type ls -F we can see while files are soft links.
  • To create a soft link called sft.file that points to a file called name.file, use $ ln -s name.file sft.file

Hard Links

  • Pointers to programs and files, but NOT directories.
  • If the original program or file is renamed, moved, or deleted, the hard link is NOT broken.
  • Hard links cannot span disk drives, so we CANNOT have a hard link on/dev/hdb that refers to a program or file on/dev/hda.
  • To create a hard link called hd.file that points to a file called name file, use this $ln name.file hd.file
Computer Science Tutorials, Linux Tags:computer science, Linux

Post navigation

Previous Post: Changing File Access Permissions in Linux
Next Post: Environment and Path Setting in Linux

Related Tutorials

programming languages for machine learning and data science Top Programming Languages For Machine Learning Computer Science Tutorials
Introduction to Statistics for Data Science: Building a Solid Foundation Computer Science Tutorials
How to Become a Successful Data Engineer in the Data Science Field – Complete Guide Computer Science Tutorials
Is Python a good language for Machine Learning/AI? Computer Science Tutorials
basics of boolean algebra Its Operators, Laws, and Examples Basics of Boolean Algebra: Its Operators, Laws, and Examples Boolean Algebra
x winodws in linux X Windows system in Linux Computer Science Tutorials

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick your Subject

  • Internet of Things
  • Data Structures/Algorithms
  • Interview Preparation
  • Human Values
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the … Read more
  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet, … Read more
  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis … Read more
  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges … Read more
  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle … Read more
  • Algorithms Tutorials
  • Basic Programming
  • Boolean Algebra
  • C Programming Tutorials
  • C++ Tutorials
  • Compiler Design Tutorials
  • Computer Networks Tutorials
  • Computer Organization Tutorials
  • Computer Science Tutorials
  • Data Structures Tutorials
  • DBMS Tutorials
  • Developer Guide
  • Digital Communication
  • Digital Logic Tutorials
  • Internet of Things Tutorials
  • Internet Tutorials
  • Interview questions answers
  • Java Tutorials
  • Javascript Tutorials
  • Linux
  • Machine Learning Tutorials
  • Operating Systems Tutorials
  • Programming Tutorials
  • Projects
  • Tips&Tricks
  • Tools
  • VBScript Tutorials
  • Java Interview Questions and Answers (2023)
    Thinking of becoming a Java developer? I must say it’s a good choice! Java is continuously named the most popular programming language. And the … Read more
  • Iot(Internet of things) in healthcare
    IoT in Healthcare
    IoMT (Internet of Medical Things) stands for devices that can collect and exchange data – either with users or other devices via the internet, … Read more
  • four stages of iot solution for industry
    IoT for Industry
    In this post, we are going to learn about use cases of IoT for Industry and four stages for providing IoT solutions. Machine Diagnosis … Read more
  • Iot for agricultural
    IoT in Agriculture
    IoT technology has realized smart wearables, connected devices, automated machines, and driverless cars. However, in agriculture, the IoT has brought the greatest impact. Amongst the challenges … Read more
  • Iot for logistics
    IoT in Logistics and Supply Chain
    IoT applications for smart logistics and supply chain systems:  Logistics Fleet Tracking  To track the locations of the vehicles in real time, the vehicle … Read more

Copyright © 2023 Programmingoneonone.

Powered by PressBook Blog WordPress theme