[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: What is the difference between unlink and rm -f?
From: |
Eric Blake |
Subject: |
Re: What is the difference between unlink and rm -f? |
Date: |
Wed, 29 Jan 2020 07:22:37 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 |
On 1/29/20 3:45 AM, Peng Yu wrote:
Hi,
It seems to me unlink and rm -f are the same if the goal is the delete
files. When are they different? Thanks.
On really old systems, it was possible to hard link directories
together. This had to be done carefully to avoid creating file system
loops, so generally it was limited to only the root user (in fact,
before the mkdir(2) syscall was invented, directories were first
implemented by mkdir(1) as a root program that manually manipulated raw
directory entries with mknod in such a manner as to create proper
directory hard links on your behalf:
https://en.wikipedia.org/wiki/Mkdir). But if you had a situation with
hard linked directories, and want to remove a directory entry no matter
whether it is a file or a directory or even missing, while still getting
at any errno message from the kernel: 'unlink' does the job perfectly,
while 'rm' fails (it does not remove directories even if unlink could)
and 'rm -f' squelches error messages (which doesn't help if your goal is
to see the result of the syscall).
These days, all systems have mkdir(2) and rmdir(2) syscalls, and you can
no longer use mknod to create directories nor create directory hard
links. So you are right that the original distinction between the two
programs is much less significant than it used to be.
And asking here shows that you didn't google very hard, as I quickly
found this answer to the same question:
https://unix.stackexchange.com/questions/151951/what-is-the-difference-between-rm-and-unlink
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org