Intro To 'unlink' Command In Linux
2024-05-10 - By Robert Elder
I use the 'unlink' command to delete individual files:
ls -l
-rw-rw-r-- 1 robert robert 0 May 9 16:25 foo.txt
unlink foo.txt
ls -l
total 0
A Single-file Deletion Tool
The unlink command can delete hard links, soft links, named pipes, as well as normal files:
mkfifo my-fifo
touch my-file.txt
ln my-file.txt hard.txt
ln -s my-file.txt soft.txt
ls -l
total 0
-rw-rw-r-- 2 robert robert 0 May 9 16:28 hard.txt
prw-rw-r-- 1 robert robert 0 May 9 16:28 my-fifo
-rw-rw-r-- 2 robert robert 0 May 9 16:28 my-file.txt
lrwxrwxrwx 1 robert robert 11 May 9 16:28 soft.txt -> my-file.txt
unlink hard.txt
unlink soft.txt
unlink my-fifo
unlink my-file.txt
ls -l
total 0
Cannot Delete Directories
According to the info pages, the GNU unlink command cannot delete directories, although other versions of the 'unlink' command may be able to do so:
info unlink
...
On some systems ‘unlink’ can be used to delete the name of a
directory. On others, it can be used that way only by a privileged
user. In the GNU system ‘unlink’ can never delete the name of a
directory.
...
The 'unlink' Command Versus The 'rm' Command
The 'unlink' command can only delete one file at a time:
ls -l
total 0
-rw-rw-r-- 1 robert robert 0 May 9 16:39 a
-rw-rw-r-- 1 robert robert 0 May 9 16:39 b
-rw-rw-r-- 1 robert robert 0 May 9 16:39 c
unlink a b c
unlink: extra operand ‘b’
Try 'unlink --help' for more information.
ls -l
total 0
-rw-rw-r-- 1 robert robert 0 May 9 16:39 a
-rw-rw-r-- 1 robert robert 0 May 9 16:39 b
-rw-rw-r-- 1 robert robert 0 May 9 16:39 c
This is unlike the 'rm' command which can delete multiple files at once:
rm a b c
ls -l
total 0
The 'info' pages describe 'unlink' as a minimalist tool that avoids the bells and whistles of the 'rm' command.
info unlink
‘unlink’ deletes a single specified file name. It is a minimalist
interface to the system-provided ‘unlink’ function. *Note
(libc)Deleting Files::. Synopsis: It avoids the bells and whistles of
the more commonly-used ‘rm’ command (*note rm invocation::).
This appears to be accurate, since the 'unlink' command doesn't support any interesting flags, unlike the much more complicated 'rm' command.
Origin Of 'unlink' Command
The unlink command appeared as early as 1964 in the Multics Design Notebook Documents, which was 5 years before development of the Unix operating system had even started.
And that's why the 'unlink' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|