1. 程式人生 > >Linux中zip和tar處理軟鏈接的差異與選擇

Linux中zip和tar處理軟鏈接的差異與選擇

Linux zip tar 處理

Linux中zip和tar處理軟鏈接的差異與選擇

  1. 系統環境
    cat /etc/redhat-release 
    CentOS Linux release 7.3.1611 (Core)
    getenforce
    Disabled
  2. 安裝zip,unzip
    #直接yum安裝
    yum install -y zip unzip
  3. 創建實驗文件
    #cd到tmp目錄下
    cd /tmp
    #創建目錄及文件
    mkdir test
    echo "this is a test file" > test.txt
    cd ./test
    echo "datagrand.com" > datagrand
    #創建軟鏈接文件
    ##這裏的軟連接文件,我創建兩種,一種源文件是在tmp目錄下的,一種源文件是在/tmp/test目錄下。具體創建流程如下:
    ln -s /etc/passwd /tmp/passwd
    ln -s /tmp/test/datagrand /tmp/datagrand
    #查看實驗用的所有文件
    pwd
    /tmp
    ls -l
    lrwxrwxrwx 1 root root 19 5月  16 18:33 datagrand -> /tmp/test/datagrand
    lrwxrwxrwx 1 root root 11 5月  16 18:31 passwd -> /etc/passwd
    drwxr-xr-x 2 root root 23 5月  16 18:33 test
    -rw-r--r-- 1 root root 20 5月  16 18:30 test.txt
  4. zip實戰
    #當前路徑
    pwd
    /
    #打包詳情
    zip -r tmp.zip ./tmp
    adding: tmp/ (stored 0%)
    adding: tmp/test/ (stored 0%)
    adding: tmp/test/datagrand (stored 0%)
    adding: tmp/test.txt (stored 0%)
    adding: tmp/passwd (deflated 60%)
    adding: tmp/datagrand (stored 0%)
    #解包
    unzip tmp.zip
    ll
    總用量 4
    drwxrwxrwx 9 root root  232 5月  16 19:00 tmp
    -rw-r--r-- 1 root root 3219 5月  16 19:00 tmp.zip
    cd  ./tmp
    -rw-r--r-- 1 root root   14 5月  16 18:33 datagrand
    -rw-r--r-- 1 root root 1454 4月  20 18:58 passwd
    drwxr-xr-x 2 root root   23 5月  16 18:33 test
    -rw-r--r-- 1 root root   20 5月  16 18:30 test.txt
    #說明
    (1)使用unzip,原打包文件還是存在的,如上例tmp.zip。
    直接使用zip打包,軟連接會消失,原來的軟鏈接文件被源文件的內容所代替,相當於原來的軟鏈接變成了硬鏈接。
    #使用參數-y
    ##為使zip能夠保留軟鏈接
    zip -ry tmp2.zip tmp
    unzip tmp2.zip
    ll
    lrwxrwxrwx 1 root root 19 5月  16 19:22 datagrand -> /tmp/test/datagrand
    lrwxrwxrwx 1 root root 11 5月  16 19:22 passwd -> /etc/passwd
    drwxr-xr-x 2 root root 23 5月  16 18:33 test
    -rw-r--r-- 1 root root 20 5月  16 18:30 test.txt
    #說明
    zip使用參數-y,可以保留原文件中的軟鏈接。
  5. tar實戰
    #cd到tmp目錄
    cd /tmp
    #tar打包壓縮
    tar -zcvf tmp3.tgz .
    #解壓縮
    tar zxvf tmp3.tgz 
    #查看解壓後的文件
    lrwxrwxrwx 1 root root  19 5月  16 18:33 datagrand -> /tmp/test/datagrand
    lrwxrwxrwx 1 root root  11 5月  16 18:31 passwd -> /etc/passwd
    drwxr-xr-x 2 root root  23 5月  16 18:33 test
    -rw-r--r-- 1 root root  20 5月  16 18:30 test.txt
    -rw-r--r-- 1 root root 478 5月  16 19:28 tmp3.tgz
    #說明
    (1)tar解壓縮後,原壓縮文件還是存在的,如上所示。
    (2)使用tar打包壓縮可以保留原文件中的軟鏈接。
  6. 總結
    鑒於上面的測試,我們可以看出,兩者均是可以保留原文件中的軟鏈接的,可根據自己的喜好來使用。

Linux中zip和tar處理軟鏈接的差異與選擇