當前位置:首頁 » 操作系統 » linuxunlink

linuxunlink

發布時間: 2022-03-15 17:22:58

A. linux shell中,unlink和rm命令有什麼區別

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 提供相同機制,但是可以刪除檔案或是空目錄。

B. PHP 怎樣處理用unlink刪除文件出現的異常

拒絕訪問,貌似linux下吧,應該是你所在的用戶許可權不夠,如果有ftp許可權的,就把欲刪除的文件的許可權改成777

C. linux shell中,unlink和rm命令有什麼區別

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.

D. linux下中的 rm 命令選項-i 什麼意思

-i:進行任何刪除操作前必須先確認。

用戶可以用rm命令刪除不需要的文件。該命令的功能為刪除一個目錄中的一個或多個文件或目錄,它也可以將某個目錄及其下的所有文件及子目錄均刪除。

(4)linuxunlink擴展閱讀

linux下rm命令使用詳解

rm命令的一般形式為:rm[選項]... 目錄... 刪除指定的<文件>(即解除鏈接)。

-d:刪除可能仍有數據的目錄 (只限超級用戶)。


-f:略過不存在的文件,不顯示任何信息。

-i:進行任何刪除操作前必須先確認。

-r/R:同時刪除該目錄下的所有目錄層。

-v :詳細顯示進行的步驟。

-help:顯示此幫助信息並離開。

--version:顯示版本信息並離開。

範例:

刪除所有rb語言程序檔;刪除前逐一詢問確認:rm-i *.rb。

將 Finished 子目錄及子目錄中所有檔案刪除:rm -r Finished。

將 Finished 子目錄及子目錄中所有檔案刪除,並且不用--確認:rm -rf Finished。

E. LINUX 怎麼刪除 文件link

每一個文件,都可以通過一個struct stat的結構體來獲得文件信息,其中一個成員st_nlink代表文件的鏈接數。
當通過shell的touch命令或者在程序中open一個帶有O_CREAT的不存在的文件時,文件的鏈接數為1。

通常open一個已存在的文件不會影響文件的鏈接數。open的作用只是使調用進程與文件之間建立一種訪問關系,即open之後返回fd,調用進程可以通過fd來read 、write 、 ftruncate等等一系列對文件的操作。
close()就是消除這種調用進程與文件之間的訪問關系。自然,不會影響文件的鏈接數。在調用close時,內核會檢查打開該文件的進程數,如果此數為0,進一步檢查文件的鏈接數,如果這個數也為0,那麼就刪除文件內容。

link函數創建一個新目錄項,並且增加一個鏈接數。
unlink函數刪除目錄項,並且減少一個鏈接數。如果鏈接數達到0並且沒有任何進程打開該文件,該文件內容才被真正刪除。如果在unlilnk之前沒有close,那麼依舊可以訪問文件內容。

綜上所訴,真正影響鏈接數的操作是link、unlink以及open的創建。
刪除文件內容的真正含義是文件的鏈接數為0,而這個操作的本質完成者是unlink。close能夠實施刪除文件內容的操作,必定是因為在close之前有一個unlink操作。

舉個例子簡單說明:通過shell touch test.txt
1、stat("test.txt",&buf);
printf("1.link=%d\n",buf.st_nlink);//未打開文件之前測試鏈接數

2、fd=open("test.txt",O_RDONLY);//打開已存在文件test.txt
stat("test.txt",&buf);
printf("2.link=%d\n",buf.st_nlink);//測試鏈接數

3、close(fd);//關閉文件test.txt
stat("test.txt",&buf);
printf("3.link=%d\n",buf.st_nlink);//測試鏈接數

4、link("test.txt","test2.txt");//創建硬鏈接test2.txt
stat("test.txt",&buf);
printf("4.link=%d\n",buf.st_nlink);//測試鏈接數

5、unlink("test2.txt");//刪除test2.txt
stat("test.txt",&buf);
printf("5.link=%d\n",buf.st_nlink);//測試鏈接數

6、重復步驟2 //重新打開test.txt

7、unlink("test.txt");//刪除test.txt
fstat(fd,&buf);
printf("7.link=%d\n",buf.st_nlink);//測試鏈接數

8、close(fd);//此步驟可以不顯示寫出,因為進程結束時,打開的文件自動被關閉。

順次執行以上8個步驟,結果如下:
1.link=1
2.link=1 //open不影響鏈接數
3.link=1 //close不影響鏈接數
4.link=2 //link之後鏈接數加1
5.link=1 //unlink後鏈接數減1
2.link=1 //重新打開 鏈接數不變
7.link=0 //unlink之後再減1,此處我們改用fstat函數而非stat,因為unlilnk已經刪除文件名,所以不可以通過 文件名訪問,但是fd仍然是打開著的,文件內容還沒有被真正刪除,依舊可以使用fd獲得文件信息。
執行步驟8,文件內容被刪除。。。。

F. linux下怎麼刪除kernel

sys_unlink 為系統調用, 若刪除失敗可以檢查 errno 的值, 可能滴原因譬如路徑、許可權、正忙等 ... 若已經安裝了 strace 這類東東, 可以用 strace rm file 滴方式看到都調用了那些動態鏈接滴函數 ...

G. linux用什麼標識,描述和控制文件

Linux的文件標識符,和Windows的就是文件名不同,它相當於文件的一個指針。在linux的c語言中,除了用通用的《【C】文件讀寫問題》(點擊打開鏈)中介紹過的fopen等c語言通用操作文件以外,其Linux應用請參考《【Linux】利用C語言文件流復制單一文件),還能用文件標識符還來操作文件。這也是Linux中C語言,最底層,最原始控制文件的方式,其函數open,read,write,close已經完美地表明這一點。同時,在Linux無論是文件、設備和管道,甚至是個可操作對象對可以視作文件來對待,具體表現為都可以用這個文件標識符來操作他們。
文件標識符非常獨特,並不是指針,其變數類型就是大家非常常用的int。
至於這個東西怎麼用,具體請看如下文件讀寫程序:

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>//文件控制
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE 512//緩沖區的大小
const char* FILEPATH="/tmp/file.txt";//文件目錄
int main(void) {
int fd;//文件標識符,
char *s = "被折騰的文字";
char buffer[MAXSIZE+1];//用來接字元的緩沖區
int size;//讀入的文件長度
if((fd=open(FILEPATH,O_CREAT|O_TRUNC|O_RDWR,0777))<0){
//O_CREAT如果指定文件不存在,則創建這個文件,O_EXCL如果要創建的文件已存在,則返回 -1,並且修改 errno 的值
//O_APPEND每次寫操作都寫入文件的末尾,O_TRUNC如果文件存在,並且以只寫/讀寫方式打開,則清空文件全部內容
//O_RDONLY只讀模式,O_WRONLY只寫模式,O_RDWR讀寫模式
//0777為最高許可權
perror("打開文件失敗!");
exit(1);
}
if((write(fd,s,strlen(s)))<0){
perror("寫入文件失敗!");
exit(1);
}
//lseek(fd,0,SEEK_SET);//即把文件指針移至buf文件的開始處
lseek(fd,-strlen(s),SEEK_END);//即把文件指針往從結尾處向前移動strlen(s)個字元
if((size=read(fd,buffer,MAXSIZE))<0){
perror("讀入文件失敗!");
exit(1);
}else{
buffer[size]='\0';//字元串數組封口
printf("%s\n",buffer);
}
if(close(fd)<0) {
perror("關鍵文件失敗!");
exit(1);
}
unlink(FILEPATH);//刪除文件
exit(0);
}
這個程序的一大堆頭文件,是沒辦法的,因為所用到的函數需要的基本頭文件就是這么多,但這些頭文件都是Linux的基本頭文件,能夠拿來直接用。

程序首先打開並同時利用open的參數創建一個文件,之後向這個文件,通過read函數寫入一個名為「緩沖區」buffer的字元數組,也就是字元串string的一些東西,然後,寫入完畢,因為文件操作游標將會移到文件最後,所以我們要先將其移迴文件頭,再利用write函數讀入這個文件的內容到buffer,並列印到屏幕,最後再用close關閉文件標識符與文件的連接,並利用unlink刪除這個文件,如果沒有close,unlink將不起作用,因為這個程序正在佔用這個/tmp/file.txt,無法刪除,如果要強制刪除可以考慮remove函數。
所以,上述代碼的運行結果如下圖:

最後,這個文件自然是要被刪除的了,肯定是沒有的:

上述程序很簡單,但我們更多應該關注這里文件標識符的作用。fd這個int就是文件標識符,相當於FILE *的作用,但他就是一個int。實質上,這個int非常獨特,同open函數,int fd裡面存著要被操作文件的地址,但它卻又不是int *,之後的write和read函數都要根據這個fd所指明的方向來,你可以發現write,read參數都有一個地方,填入了fd,可要求填入的,卻是一個int變數,這在windows裡面是沒有的,同時不了解文件標識符的人,看到write和read的使用可能是雲里霧里的,之後的close就更不用說了,就是清楚這個fd與被操作文件的關聯關系。
unlink函數則和文件標識符無關,需要一個文件路徑的參數。

H. linux rm命令怎麼用

linux rm命令用於刪除文件,其使用步驟如下:

需要准備的材料分別是:電腦、linux連接工具。

1、首先連接上linux主機,進入命令行狀態。

I. C語言unlink函數

這個提問說明了兩個問題

  1. 你不會查手冊,命令man 2 unlink有這個函數詳細的說明

  2. 你不了解Linux的文件系統

unlink的文檔是這樣描述的:

unlink() deletes a name from the filesystem. If that name was the last link to a file and no processes have the file open the file is deleted and the space it was using is made available for reuse.
If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.
If the name referred to a symbolic link the link is removed.
If the name referred to a socket, fifo or device the name for it is removed but processes which have the object open may continue to use it.
首先你要明確一個概念,一個文件是否存在取決於它的inode是否存在,你在目錄里看到的是目錄項里一條指向該inode的鏈接,而不是文件的本身.

當你調用unlink的時候他直接把目錄項里的該條鏈接刪除了,但是inode並沒有動,該文件還是存在的,這時候你會發現,目錄里找不到該文件,但是已經打開這個文件的進程可以正常讀寫.只有當打開這個inode的所有文件描述符被關閉,指向該inode的鏈接數為0的情況下,這個文件的inode才會被真正的刪除.

從unlink的名字上就應該能判斷出來,unlink含義為取消鏈接,remove才是刪除的意思

熱點內容
安卓手機看如懿傳怎麼看 發布:2024-09-25 17:11:18 瀏覽:790
pythonnumpy矩陣 發布:2024-09-25 17:11:07 瀏覽:664
個性化linux 發布:2024-09-25 17:10:01 瀏覽:586
解壓好文件後如何安裝系統文件 發布:2024-09-25 16:25:09 瀏覽:164
編程字體排行 發布:2024-09-25 16:15:32 瀏覽:795
幸運之杖安卓在哪裡下載 發布:2024-09-25 16:13:26 瀏覽:254
execsqlinsertinto 發布:2024-09-25 16:08:56 瀏覽:624
相對權重演算法 發布:2024-09-25 15:57:13 瀏覽:591
gl8es買哪個配置 發布:2024-09-25 15:47:02 瀏覽:724
python創建文本 發布:2024-09-25 15:47:00 瀏覽:12