> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Unlink Command in Linux
- URL: https://linuxhandbook.com/unlink-command/
- Published: 2022-07-27T11:57:53.000Z
- Updated: 2022-10-17T07:35:06.000Z
- Description: Learn about the unlink command, an alternate method of deleting files in the Linux terminal.
- Author: Abhishek Prakash
- Tags: Commands

How do you [delete files in the Linux terminal](https://linuxhandbook.com/remove-files-directories/)? You use the rm command.

That's what people usually do. But another command can be used for deleting files and links in Linux.

The command is called `unlink` and though it may sound like it is for [deleting links](https://linuxhandbook.com/delete-links/) only in Linux, it can also delete files.

After all, the delete process in Linux is basically unlinking. When you delete a file, the Linux kernel finds that it corresponds to inode number X. It will remove the file entry from its directory's listing and reduce the inode X’s link count by 1\. Now that inode X’s link count is 0, the kernel knows that there is no one linking to this [inode](https://linuxhandbook.com/inode-linux/), so it is safe to remove the inode and delete the data block associated with it.

Let's see how to use the unlink command.

## Unlink command

The unlink command is used for removing files and links. You can use it to delete both soft and [hard links](https://linuxhandbook.com/hard-link/).

It is one of the simplest commands that have no options other than `--help` and `--version`.

To delete a file with unlink, use it like this:

```
unlink filename
```

To delete a link:

```
unlink link_name
```

 You won't see any output when successfully deleting a file or link.

![Use unlink command for deleting file in Linux](https://linuxhandbook.com/content/images/2022/07/delete-file-using-unlink-linux-command.png)

Basically, that's about it for the unlink command. It cannot do anything else. 

### You cannot delete multiple files or links at once

While the rm command can delete multiple files at once, unlink can only remove one file at a time. You cannot give more than one files to it at once. You cannot use globbing patterns as well.

If you try something like that, it throws an error and doesn't delete any file.

```
abhishek@LHB:~/test$ unlink *.txt
unlink: extra operand ‘file2.txt’
Try 'unlink --help' for more information.
```

The same goes for deleting multiple links at once.

### You cannot delete directories with unlink

The GNU implementation of unlink command cannot delete a directory. It throws an error if you try removing a folder.

```
abhishek@LHB:~$ unlink new_dir
unlink: cannot unlink 'new_dir': Is a directory
```

## Difference between rm and unlink commands

The unlink command uses unlink system command while rm command uses unlinkat system call. Both system calls are almost the same.

There are a few obvious differences between the two commands and you can see that already.

The unlink command only handles one file or link at a time. The rm command can handle multiple at a time.

The rm command can delete directories with the recursive option. The unlink command cannot delete a directory. 

The rm command performs a security check. If you don't have write permission on a file, it will ask you to confirm it interactively or use the force option `-f`. With unlink, there is no safety checks. It will delete the write-protected file.

![Unlink doesn't do a safety check unlike the rm command](https://linuxhandbook.com/content/images/2022/07/no-safety-checks-with-unlink-command.png)

Unlink doesn't have additional functionalities or options, unlike the rm command.

There could be a few use cases where you would prefer using the unlink command over rm. Imagine that you want to 'force remove' a file irrespective of the write protection, but you want the script to fail if the file doesn't exist. The rm -f won't show any errors if the file doesn't exist but unlink will throw an error.