C Programming

Unlink System Call in C

The system call is used within the C language to deletes a title or name from any file system, e.g., Linux system. It eliminates the entry or link of a specific file or directory from the file system, which shows that the file or folder has been deleted, which is not the case. If the file or folder is currently not working or opened, that file or folder may be deleted upon usage in unlink() system call. That’s the case when the file or folder link count is 0. Let’s have a look at some simple examples for the Unlink system call to understand it well. Login from the Ubuntu 20.04 system and open the terminal to get started.

Example 01:

Let’s take a new illustration to see the working of the “unlink()” system call in Linux. Let’s create a text file first in the current directory using “touch” instruction, add some data in it, and display the data as well as follows:

$ touch new.txt

$ echo “John” > new.txt

$ cat new.txt

After creating a file, we have listed the contents of a home directory as stated below. Now, the list contains the new.txt file as well.

$ ls

Let’s open the “unlink.c” C file to add up some C language code in it to Unlink the file “new.txt”.

$ nano unlink.c

The C code to unlink the new.txt file has been displayed in the image below. We have defined and included certain input-output header libraries and Posix extensions as well. Then we have initialized the main method to declare file descriptor “fd”.   Character type array has been used to get the contents of a file “new.txt”. An “if” statement has been used to check the create file error. If the file descriptor finds that the file has a link count of less than 0, it may display an error message. The else statement has been used to close the file descriptor. If the file has the unlink count not equal to 0, it will display the error message. Save this code and return.

Now, compile the file “unlink.c” that has just been updated. The compilation of a file got no errors indicates the code is all fine.

$ gcc unlink.c

After compilation, the file needs to be executed in the shell as follows:

$ ./a.out

Let’s see the contents of a Linux home directory once again using the “ls” command. The output shows that the file “new.txt” has been fully unlinked from the file system of Linux.

$ ls

Example 02:

Another way to use unlink in the C code has been shown in the image below. Open the “link.c” file and assign the file name as an argument to the system called “unlink”. The link value would be returned and saved into the variable “n”. The first print statement shows that 0 links returned indicates success, and -1 returned means failure. The second print statement will print the link value returned and shows that the “unlink” of a file has been successful.

Upon compilation and execution of a file, we have got the success message that the file has been fully unlinked as 0 indicated success.

$ gcc link.c

$ ./a.out

Example 03:

Lastly, we will see a simple example to use the unlink() system call on some files within the shell. List out the file and folders residing in the current home directory of our Ubuntu 20.04 Linux system using the command “ls”. We have three files in the directory. Let’s unlink the file “umask.c” from the current directory using the “unlink” system call in the shell as stated below.

$ ls

$ unlink umask.c

After checking the contents of a home directory once more using the “ls “ command, we have found that the link for the name “umask.c” has been removed. As the file was not opened and its link count was 0, the file has been removed from the home directory.

Conclusion:

This article has demonstrated the working of an Unlink system call in C language within the Ubuntu 20.04 system. We hope all the examples will be easy to implement on your system using terminal and C files.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content