The unlink command doesn't get as much attention as some of the other GNU Core Utilities. It has one simple function, to remove a file or symbolic link from a Linux file system. In this article we will discuss how to use the unlink command, and the differences between unlink and the rm command.
Unlink vs rm Command
The unlink command differs in several ways from the rm command. Under the hood it uses the same unlink()
system call. However there are functional differences that users will notice.
rm Command:
- Performs safety checks (confirmation prompts if no write permissions)
- Can provide feedback via STDOUT
- Capable of removing multiple files
- Can remove directories
- Additional options to expand functionality
unlink Command:
- No safety checks
- Does not provide additional options
- Cannot remove multiple files
- Does not remove directories
Why Use unlink Instead of rm?
I have been a Linux admin for over two decades and I really never had a need for the unlink command. However, I did some research and the only decent use case I can find was in an old thread, quoted below.
If you try to
rm
an object to which you don't have write permissions (which are irrelevant to your ability to remove it: the containing directory's permissions are!)rm
nevertheless refuses unless-f
is specified.rm
normally complains if the file doesn't exist, as doesunlink
; however with-f
,rm
does not complain........Suppose you want to just remove a regular file regardless of what its own permissions are. Furthermore, suppose you want the command to fail if the file doesn't exist, or any other reason. Neither
- Kaz/Fritzrm file
norrm -f file
meets the requirements.rm file
will refuse if the file isn't writable. Butrm -f file
will neglect to complain if the file is missing.unlink file
does the job.
After reading the above, I can see where unlink
would be useful, especially in a shell scripting context.
Using the Unlink Command to Remove a File
The unlink command is used to remove a single file and will not accept multiple arguments. It has no options other than --help
and --version
. The syntax is simple, invoke the command and pass a single filename as an argument to remove that file.
[savona@putor ~]$ unlink test.log
You can pass it a wildcard or globbing pattern, but only if that pattern expands to a single file. For example, we can use rm to remove all text files using a wildcard like so:
[savona@putor ~]$ rm *.txt
If we pass a wildcard to unlink, you will receive an extra operand error. This is because the wildcard represents more than a single file. In this example it expands to three files.
[savona@putor ~]$ ls -lrt
total 0
-rw-rw-r--. 1 savona savona 0 Mar 12 23:59 test.txt
-rw-rw-r--. 1 savona savona 0 Mar 12 23:59 article.txt
-rw-rw-r--. 1 savona savona 0 Mar 12 23:59 putorius.txt
[savona@putor ~]$ unlink *.txt
unlink: extra operand ‘putorius.txt’
Try 'unlink --help' for more information.
If the wildcard represents a single file, all is well.
[savona@putor ~]$ ls -l
total 0
-rw-rw-r--. 1 savona savona 0 Mar 12 23:59 test.txt
[savona@putor ~]$ unlink *.txt
[savona@putor ~]$
NOTE: Unlink cannot be used to remove a directory.
[savona@putor ~]$ unlink dir1
unlink: cannot unlink 'dir1': Is a directory
Using the Unlink Command to Remove a Symbolic Link
You can remove a symbolic link the same way you would remove a file.
[savona@putor ~]$ unlink link.txt
When you use unlink to remove a symbolic link, the linked file is left intact.
[savona@putor ~]$ ls -lrt
total 0
-rw-rw-r--. 1 savona savona 0 Mar 13 00:08 test.txt
lrwxrwxrwx. 1 savona savona 8 Mar 13 00:08 link.txt -> test.txt
[savona@putor ~]$ unlink link.txt
[savona@putor ~]$ ls -lrt
total 0
-rw-rw-r--. 1 savona savona 0 Mar 13 00:08 test.txt
Conclusion
Unlink is a simple tool for removing a single file. However, for most purposes it's functionality has be replaced with the more popular rm command. Furthermore, rm
is better suited for everyday use because of it's safety checks and robust options like recursion, verbose output and ability to remove directories.
Resources and Links
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
2 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
- Uncategorized (1)
I'm on Fedora. Installed Brotli using this instructions, added loadmodule inn /etc/nginx/nginx.conf. But when I restart Nginx, I get this error:
`
Jan 04 19:42:05 jet.***.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jan 04 19:42:05 jet.***.com nginx[38513]: nginx: [emerg] module "/usr/share/nginx/modules/ngx_http_brotli_filter_module.so" ve>
Jan 04 19:42:05 jet.***.com nginx[38513]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jan 04 19:42:05 jet.***.com systemd[1]: nginx.service: Control process exited, code=exited, status=1/FAILURE
Jan 04 19:42:05 jet.***.com systemd[1]: nginx.service: Failed with result 'exit-code'.
Jan 04 19:42:05 jet.***.com systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
`
Any ideas which step is missing?
This article is about the unlink command, nothing to do with ngix.