C program to create hard link and soft link
Last Updated :
30 Nov, 2021
There are two types of links, i.e., a soft link and a hard link to a file. C library has a function link() that creates a new hard link to an existing file. The function symlink() to create a soft link. If the link file/path already exists, it will not be overwritten. Both function link() and symlink() return 0 on success. If any error occurs, then -1 is returned. Otherwise, 'errno' (Error Number) is set appropriately.
Soft Link: A soft link (also known as a Symbolic link) acts as a pointer or a reference to the file name. It does not access the data available in the original file. If the earlier file is deleted, the soft link will point to a file that does not exist anymore.
Hard Link: A hard link acts as a copy (mirrored) of the selected file. It accesses the data available in the original file.
If the earlier selected file is deleted, the hard link to the file will still contain the data of that file.
Function to create a Hard Link:
L = link(FILE1, FILE2), creates a hard link named FILE2 to an existing FILE1.
where, L is the value returned by the link() function.
Function to create a Soft Link:
sL = symlink(FILE1, FILE2), creates a soft link named FILE2 to an existing FILE1.
where, sL is the value returned by the symlink() function
Program 1: Below is the C program to create a hard link to an existing file:
C
// C program to create an Hard Link
// to the existing file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Driver Code
int main(int argc, char* argv[])
{
// Link function
int l = link(argv[1], argv[2]);
// argv[1] is existing file name
// argv[2] is link file name
if (l == 0) {
printf("Hard Link created"
" succuessfuly");
}
return 0;
}
Output:
Program 2: Below is the C program to create a Soft link to an existing file:
C
// C program to create an Soft Link
// to the existing file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// Driver Code
int main(int argc, char* argv[])
{
// Symlink function
int sl = symlink(argv[1], argv[2]);
// argv[1] is existing file name
// argv[2] is soft link file name
if (sl == 0) {
printf("Soft Link created"
" succuessfuly");
}
return 0;
}
Output:
Similar Reads
How to Create a Linked List in C? Write a C program to create a linked list.A linked list is a sequential data structure that stores the data in non-contiguous memory locations unlike array. In a linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in the
5 min read
How to Create a Doubly Linked List in C? A doubly linked list is a type of linked list in which each node contains a pointer to both the next node and the previous node. This allows traversal in both forward and backward directions. Each node in a doubly linked list stores data, a pointer to the next node, and a pointer to the previous nod
4 min read
How to Create a Circular Linked List in C? The circular linked list is a version of a linked list where the last node does not point to the NULL, but instead, it points back to the first node making a circular loop-like structure. In this article, we will explore how to create a circular linked list in C. What is a Circular Linked List?A cir
3 min read
How to Create a Dynamic Library in C? In C, a dynamic library is a library that is loaded at runtime rather than compile time. We can create our own dynamic libraries and use them in our programs. In this article, we will learn how to create a dynamic library in C. Example: Input:Hello, geeksforgeek!Output:Hello, geeksforgeek!Creating a
2 min read
Create Directory or Folder with C/C++ Program Problem: Write a C/C++ program to create a folder in a specific directory path. This task can be accomplished by using the mkdir() function. Directories are created with this function. (There is also a shell command mkdir which does the same thing). The mkdir() function creates a new, empty director
2 min read