linux shell中,unlink和rm命令有什么区别

如题所述

unlink 不能用于删除文件夹,rm 可以删除文件和文件夹

当删除文件时,rm 和 unlink 是完全一样的。命令详细的介绍查找可看下图

温馨提示:内容为网友见解,仅供参考
第1个回答  2014-11-16
strace 跑 rm 与 unlink,结果有不同地方.

1. strace rm 1.txt 片段

access("1.txt", W_OK) = 0
unlinkat(AT_FDCWD, "1.txt", 0) = 0

2. strace unlink 2.txt 片段

unlink("2.txt") = 0

我想可以再看看 unlink 与 unlinkat 的 manpage 说明应该有答案 ? :)

The unlinkat() system call operates in exactly the same way as either unlink(2) or rmdir(2) (depending on
whether or not flags includes the AT_REMOVEDIR flag) except for the differences described in this manual
page.

AT_REMOVEDIR
By default, unlinkat() performs the equivalent of unlink(2) on pathname. If the AT_REMOVEDIR flag
is specified, then performs the equivalent of rmdir(2) on pathname.

测试用 strace rm -r testdir 方式删除一个目录与其下的档案目录,结果为:

access("testdir", W_OK) = 0
unlinkat(AT_FDCWD, "testdir", AT_REMOVEDIR) = 0

所以结论一般命令用 rm 或是 unlink 都可以删除档案,因为都是一样的意思。系统面呼叫来说删除档案一般还是使用 unlink() 呼叫,只是有另外提供 unlinkat 提供相同机制,但是可以删除档案或是空目录。

不过我想其他系统的 rm 与 unlink 是有差异的,比方 solaris 就不一样。

-bash-3.00$ uname -a
SunOS solaris 5.10 Generic_118855-33 i86pc i386 i86pc

man unlink :

System Administration Commands link(1M)

NAME
link, unlink - link and unlink files and directories

SYNOPSIS
/usr/sbin/link existing-file new-file

/usr/xpg4/bin/link existing-file new-file

/usr/sbin/unlink file

DESCRIPTION
The link and unlink commands link and unlink files and
directories. Only super-users can use these commands on
directories.本回答被提问者和网友采纳
第2个回答  2021-09-23
一般命令用 rm 或是 unlink 都可以删除档案,因为都是一样的意思。系统面呼叫来说删除档案一般还是使用 unlink() 呼叫,只是有另外提供 unlinkat 提供相同机制,但是可以删除档案或是空目录。命令具体使用介绍请查看“Linux命令大全”
相似回答