linux檔案目錄串連

來源:互聯網
上載者:User

標籤:style   blog   class   c   tar   http   

 

linux系統下提供ln指令來進行檔案連結。檔案連結主要分為永久連結和軟連結。

永久連結:由於linux下的檔案是通過索引節點(Inode)來識別檔案,永久連結可以認為是一個指標,指向檔案索引節點的指標,系統並不為它重新分配inode。每添加一個一個永久連結,檔案的連結數就加1。

可以用:ln命令來建立永久連結。文法:

  1. ln [options] existingfile newfile  
  2. ln[options] existingfile-list directory  

 

 

軟連結(符號連結):

軟連結克服了永久連結的不足,沒有任何檔案系統的限制,任何使用者可以建立指向目錄的符號連結。因而現在更為廣泛使用,它具有更大的靈活性,甚至可以跨越不同機器、不同網路對檔案進行連結。

  1. $ln –s file1 file1soft

程式執行:

link()函數和symlink()

 unlink("/opt/ipnc");


 sprintf(cSrc,"/opt/ipnc%d",iDirNum);
 sprintf(cDst,"/opt/ipnc");
 symlink(cSrc, cDst);

原型:

#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);
ssize_t readlink(const char *path, char *buf, size_t bufsiz);


說明:
symlink() 函數建立稱為 newpath 的符號連結,該符號連結指向稱為 oldpath 的檔案。符號連結可以跨越檔案系統,並且不會對執行對 oldpath 是否存在的任何檢查。一旦建立符號連結,就可以用 readlink() 函數來讀取它的內容,以查看它指向何處。

readlink() 函數允許我們確定為 path 的符號連結指向什麼內容。路徑名的第一個 bufsiz 位元組複製到使用者提供的緩衝區 buf。當路徑名複製到 buf  時,它沒有按照我們期望的那樣以 NULL 終止,因此必須添加自己的終止標記。

範例程式碼:

#include <unistd.h>
#include <stdio.h>

int main()
{
    
    char buf[128];

    if (symlink ("/home/Programming/system/symlink.c", "/home/groad/symlink_test.c") == -1)
        perror ("symlink");

    readlink ("/home/groad/symlink_test.c", buf, 127);

    buf[128] = ‘\0‘;
    printf ("%s\n", buf);

    return (0);
}

運行與輸出:

 ./symlink 
/home/Programming/system/symlink.c

之間的區別:

永久連結就是備份,軟串連就是捷徑

http://blog.csdn.net/hairetz/article/details/4168296

http://blog.csdn.net/stilling2006/article/category/746887

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.