linux shell中,unlink和rm命令有什麼區別
阿新 • • 發佈:2019-02-15
strace 跑 rm 與 unlink,結果有不同地方.
1. strace rm 1.txt 片段
2. strace unlink 2.txt 片段
我想可以再看看 unlink 與 unlinkat 的 manpage 說明應該有答案 ? :)
測試用 strace rm -r testdir 方式刪除一個目錄與其下的檔案目錄,結果為:
所以結論一般命令用 rm 或是 unlink 都可以刪除檔案,因為都是一樣的意思。系統面呼叫來說刪除檔案一般還是使用 unlink() 呼叫,只是有另外提供 unlinkat 提供相同機制,但是可以刪除檔案或是空目錄。
不過我想其他系統的 rm 與 unlink 是有差異的,比方 solaris 就不一樣。
man 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.
我想詳細部份需要自行測試:p
轉http://www.chinaunix.net/jh/4/920666.html