檔案打包壓縮
自我感覺,檔案壓縮打包這塊很好學,別看壓縮命名多,好幾個不常用,常用的幾個選項基本都通用,就會產生一個很好的結果,只要學一個命令的選項,剩下的幾個命令只要記住名字就可以了,
本篇的重點在tar命令,大部分網上的檔案都是經過打包壓縮處理過的,
linux中常見的壓縮檔案的擴展名:*.gz *.bz2 *.xz *.tar *.tar.gz *.tar.bz2 *.tar.xz *.tgz *.Z *.tar.Z
0.compress/uncompress compress 壓縮檔案不會保留源檔案,壓縮后的檔案后綴是.Z -d 解壓縮,相當于uncompress,可以重定向以保留源檔案 -c 輸出壓資訊到螢屏,可以重定向以保留源檔案 -v 顯示詳情 zcat命令 可以查看.Z和.gz的壓縮檔案內容 此命令太過久遠了,基本上已經很少見*.Z的壓縮檔案了,而且使用gzip命令也可以解壓*.Z壓縮檔案, 1.gzip/gunzip gzip壓縮檔案會洗掉源檔案,生成.gz為后綴的壓縮檔案, gzip [option]....FILE..... -c 把壓縮檔案資料流輸出到螢屏上,可以使用重定向保存到檔案中,用于保留源檔案, -d 解壓縮,相當于gunzip -k 保留源檔案,僅在centos8以后的版本中有效 -v 壓縮后,可以顯示出壓縮比 -t 用于測驗壓縮包的完整性 -# 按照指定的壓縮比壓縮,默認為6,支持1-9 zcat、zmore、zless命令 在不解壓.gz后綴的壓縮檔案的情況下查看壓縮檔案內容,zcat FILE.GZ zgrep命令 可以在解壓的情況下直接搜索關鍵字 zgrep 關鍵字 /PATH/TO/COMPRESS.gz
示例:
1 #演示在不解壓的情況下搜索關鍵字 2 [root@CentOS8 tmp]# zgrep http services.gz 3 # http://www.iana.org/assignments/port-numbers 4 http 80/tcp www www-http # WorldWideWeb HTTP 5 http 80/udp www www-http # HyperText Transfer Protocol 6 http 80/sctp # HyperText Transfer Protocol 7 https 443/tcp # http protocol over TLS/SSL 8 https 443/udp # http protocol over TLS/SSL 9 https 443/sctp # http protocol over TLS/SSL 10 gss-http 488/tcp 11 gss-http 488/udp 12 13 14 #-c選項和管道符結合使用,主要用于保存源檔案 15 [root@CentOS8 tmp]# gzip -c services > services.gz 16 [root@CentOS8 tmp]# ll 17 total 824 18 -rwx------. 1 root root 1379 May 2 08:53 ks-script-drjaqc7p 19 -rw-r--r-- 1 root root 692241 May 3 17:35 services 20 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz
2.bzip2/bunzip2 bzip2壓縮檔案會洗掉源檔案,生成.bz2為后綴的壓縮檔案, bzip2 [option]....FILE..... -c 把壓縮檔案資料流輸出到螢屏上,可以使用重定向保存到檔案中,用于保留源檔案, -k 壓縮后保留源檔案,同時生成壓縮檔案, -d 解壓縮,相當于bunzip2 -# 按照指定的壓縮比壓縮,默認為6,支持1-9 -q 靜默 壓縮 -v 壓縮后,顯示出壓縮比 -r 把目錄下的檔案分別進行獨立壓縮 bzcat/bzmore/bzless命令 在不解壓.bz2后綴的壓縮檔案的情況下查看壓縮檔案內容,和zcat用法相同, bzcat FILE.BZ2 bzgrep命令 在不解.bz2后綴的壓縮檔案的情況下搜索壓縮包內容 bzgrep 關鍵字 /PATH/TO/COMPRESS.bz2
示例:
1 #保留源檔案壓縮,壓縮后顯示壓縮比資訊 2 [root@CentOS8 tmp]# bzip2 -kv services 3 services: 5.334:1, 1.500 bits/byte, 81.25% saved, 692241 in, 129788 out. 4 [root@CentOS8 tmp]# ll 5 total 952 6 -rwx------. 1 root root 1379 May 2 08:53 ks-script-drjaqc7p 7 -rw-r--r-- 1 root root 692241 May 3 18:15 services 8 -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 9 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz 10 11 [root@CentOS8 tmp]# bunzip2 services.bz2
3.xz/unxz xz壓縮檔案會洗掉源檔案,生成.xz為后綴的壓縮檔案, xz [option]....FILE..... -c 把壓縮檔案資料流輸出到螢屏上,可以使用重定向保存到檔案中,用于保留源檔案, -k 壓縮后保留源檔案,同時生成壓縮檔案, -d 解壓縮,相當于unxz -# 按照指定的壓縮比壓縮,默認為6,支持1-9 -q 靜默 壓縮 -v 壓縮后,顯示壓縮比等資訊 xzcat/xzmore/xzless命令 在不解壓.xz后綴的壓縮檔案的情況下查看壓縮檔案內容,和zcat用法相同,xzcat FILE.XZ xzgrep命令 在不解壓的情況下搜索壓縮包內容 xzgrep 關鍵字 /PATH/TO/COMPRESS.xz
示例:
#保留源檔案壓縮,并且輸出壓縮比等資訊 [root@CentOS8 tmp]# xz -kv services services (1/1) 100 % 103.4 KiB / 676.0 KiB = 0.153 [root@CentOS8 tmp]# ll total 1056 -rw-r--r-- 1 root root 692241 May 3 18:15 services -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz -rw-r--r-- 1 root root 105872 May 3 18:15 services.xz [root@CentOS8 tmp]# rm services #洗掉源檔案 rm: remove regular file 'services'? y [root@CentOS8 tmp]# xz -dk services.xz #保留源檔案解壓縮 [root@CentOS8 tmp]# ll total 1056 -rw-r--r-- 1 root root 692241 May 3 18:15 services -rw-r--r-- 1 root root 129788 May 3 18:15 services.bz2 -rw-r--r-- 1 root root 142549 May 3 18:02 services.gz -rw-r--r-- 1 root root 105872 May 3 18:15 services.xz #使用xzgrep搜索壓縮包 [root@CentOS8 tmp]# xzgrep http services.xz # http://www.iana.org/assignments/port-numbers http 80/tcp www www-http # WorldWideWeb HTTP http 80/udp www www-http # HyperText Transfer Protocol http 80/sctp # HyperText Transfer Protocol https 443/tcp # http protocol over TLS/SSL https 443/udp # http protocol over TLS/SSL https 443/sctp # http protocol over TLS/SSL #使用-l選項顯示長格式資訊 [root@CentOS8 tmp]# xz -l services.xz Strms Blocks Compressed Uncompressed Ratio Check Filename 1 1 103.4 KiB 676.0 KiB 0.153 CRC64 services.xz
4.zip/unzip zip一個及壓縮打包于一身的壓縮命令,而且還是各個作業系統通用的壓縮命令,支持windows,unix,liunx, zip [OPTION]... 壓縮后的檔案名.zip 要進行壓縮的源檔案 備注:如果要用zip進行壓縮目錄,需要指明目錄下的檔案名,如果不指明則會指壓縮目錄本身,例子:要使用 zip pam.d.zip /etc/pam.d/* 5.tar 用于打包的工具,能夠把多個檔案打包成一個檔案,tar打包或者展開不會洗掉源檔案 1.創建檔案 tar -c -f -[jJz] /PATH/TO/SOMEFILE.tar FILE.... 備注:-[jJz]通常只在創建壓縮檔案的時候使用,解壓縮tar會自動呼叫對應的壓縮工具實作解壓 2.展開檔案 tar -x -f -[jJz] /PATH/TO/SOMEFILE.tar -C /PATH/TO/DIR 備注:在展開的時候其實是可以不用加-j/-z/-J選項,tar會自動呼叫對應的解壓工具解壓, 3.不展開顯示打包檔案的串列 tar -t -f -[jJz] /PATH/TO/SOMEFILE.tar 備注:在展開的時候其實是可以不用加-j -z -J 選項的,tar會自動呼叫對應的解壓工具解壓, -c 創建打包檔案 -x 展開打包檔案 -t 列出打包檔案的檔案串列,而不展開 -f 指定要創建或展開的打包檔案路徑 -j 呼叫bzip2壓縮工具 -J 呼叫xz壓縮工具 -z 呼叫gzip壓縮工具 -Z 呼叫compress壓縮工具進行壓縮 不允許同時出現,-j -J -z -Z -v 顯示程序 -p 保留源檔案的原始屬性,只有管理員有權限 -P 保留絕對路徑,就是允許貝恩資料中含有根目錄,默認情況下是自動取消根目錄 --xattrs 保留源檔案的擴展屬性 -C 把展開的檔案保存到指定檔案下,默認為當前目錄, --exclude=/PATH/TO/{SOMEDIR|SOMEFILE} 排除某些目錄或檔案不壓縮
示例:
1 #使用-z壓縮 2 [root@CentOS8 tmp]# tar -zcvf etc.gz /etc/* 3 tar: Removing leading `/' from member names 4 /etc/adjtime 5 tar: Removing leading `/' from hard link targets 6 /etc/aliases 7 /etc/alternatives/ 8 ... 9 #使用-j壓縮,并保留源檔案屬性 10 [root@CentOS8 tmp]# tar -jcvpf etc.bz2 /etc/* 11 tar: Removing leading `/' from member names 12 /etc/adjtime 13 tar: Removing leading `/' from hard link targets 14 /etc/aliases 15 /etc/alternatives/ 16 /etc/alternatives/libnssckbi.so.x86_64 17 ... 18 #使用-J壓縮,并保留源檔案屬性和facl屬性,這里沒有使用-v選項 19 [root@CentOS8 tmp]# tar --xattrs -Jcpf etc.xz /etc/* 20 tar: Removing leading `/' from member names 21 tar: Removing leading `/' from hard link targets 22 [root@CentOS8 tmp]# ll 23 total 11508 24 -rw-r--r-- 1 root root 3654689 May 3 19:05 etc.bz2 25 -rw-r--r-- 1 root root 5053462 May 3 19:03 etc.gz 26 -rw-r--r-- 1 root root 3069780 May 3 19:07 etc.xz 27 28 #使用-C選項指定解壓目錄 29 [root@CentOS8 tmp]# mkdir test 30 [root@CentOS8 tmp]# tar xf etc.gz -C /tmp/test
6.cpio 老牌打包解包工具 -o output模式,打包,將標準輸入傳入的檔案名打包發送到標準輸出 -i input模式,解包 -t 預覽,查看標準輸入傳入的打包檔案中的包含的檔案串列 cpio -tv < NAME.cpio -A 向已存在的歸檔檔案中追加檔案 -v 顯示詳細程序 -d 如果有檔案夾自動創建,常與-i一起使用,用于解壓時自動創建目錄 cpio -idv < etc.cpio -F filename 使用指定的檔案名代替標準輸入或輸出 -I filename 對指定的歸檔檔案名解壓 -O filename 輸出到指定歸檔檔案名
1 #示例演示cpio不會區分絕對路徑和相對路徑 2 [root@CentOS8 /]# find ./etc | cpio -H newc -o > /tmp/etc.cpio 3 40756 blocks 4 [root@CentOS8 /]# find /etc | cpio -H newc -o > /tmp/etc2.cpio 5 40757 blocks 6 [root@CentOS8 /]# cd /tmp 7 [root@CentOS8 tmp]# ls 8 boot.cpio etc2.cpio etc.cpio 9 [root@CentOS8 tmp]# cpio -tv < etc.cpio 10 drwxr-xr-x 78 root root 0 May 3 18:14 etc 11 lrwxrwxrwx 1 root root 19 May 2 08:51 etc/mtab -> ../proc/self/mounts 12 -rw-r--r-- 1 root root 615 May 2 08:50 etc/fstab 13 -rw------- 1 root root 0 May 2 08:50 etc/crypttab 14 -rw-r--r-- 1 root root 69 May 3 17:31 etc/resolv.conf 15 ,,, 16 [root@CentOS8 tmp]# cpio -tv < etc2.cpio 17 drwxr-xr-x 78 root root 0 May 3 18:14 /etc 18 lrwxrwxrwx 1 root root 19 May 2 08:51 /etc/mtab -> ../proc/self/mounts 19 -rw-r--r-- 1 root root 615 May 2 08:50 /etc/fstab 20 -rw------- 1 root root 0 May 2 08:50 /etc/crypttab 21 -rw-r--r-- 1 root root 69 May 3 17:31 /etc/resolv.conf 22 drwxr-xr-x 8 root root 0 May 2 08:51 /etc/dnf 23 #對比發現,使用find /etc生成cpio檔案中竟然會保留根目錄,如果解壓的時候就會覆寫對應根目錄下的檔案,很危險 24 #而使用find ./etc生成的cpio檔案中沒有保留根目錄, 25 26 #解壓etc.cpio 27 [root@CentOS8 tmp]# ls 28 boot.cpio etc2.cpio etc.cpio 29 [root@CentOS8 tmp]# mkdir test 30 [root@CentOS8 tmp]# cd test 31 [root@CentOS8 test]# cpio -id <../etc.cpio 32 40756 blocks 33 [root@CentOS8 test]# ls 34 etc
關于時間成本: 通過下面小示例可以看到,使用gzip、bzip2、xz壓縮同一個檔案,耗費的時間長短,xz消耗的時間最長,gzip消耗的時間最短,
1 [root@CentOS8 tmp]# rm -fr * 2 [root@CentOS8 tmp]# ls 3 [root@CentOS8 tmp]# cp /etc/services . 4 [root@CentOS8 tmp]# ls 5 services 6 [root@CentOS8 tmp]# time gzip -k services ;time bzip2 -k services ;time xz -k services 7 8 real 0m0.016s #使用gzip壓縮消耗的時間0.016s 9 user 0m0.013s 10 sys 0m0.003s 11 12 real 0m0.036s #使用bzip2壓縮消耗的時間0.036s 13 user 0m0.025s 14 sys 0m0.010s 15 16 real 0m0.184s #使用xz壓縮消耗的時間0.184s 17 user 0m0.107s 18 sys 0m0.077s 19 [root@CentOS8 tmp]# ll 20 total 1052 21 -rw-r--r-- 1 root root 692241 May 3 18:36 services 22 -rw-r--r-- 1 root root 129788 May 3 18:36 services.bz2 23 -rw-r--r-- 1 root root 142549 May 3 18:36 services.gz 24 -rw-r--r-- 1 root root 105872 May 3 18:36 services.xz
總結一下:
linux中常見的壓縮檔案的擴展名:*.gz *.bz2 *.xz *.tar *.tar.gz *.tar.bz2 *.tar.xz *.tgz *.Z *.tar.Z
常見的壓縮命令有:gzip bzip2 xz,其中壓縮比率最好的是xz,但是消耗的時間也是最高的,需要綜合考慮時間成本和CPU性能,
幾個選項基本上都通用:-d 解壓 -v 顯示詳細程序 -c 輸出內容到螢屏 -k保留源檔案
tar可以打包檔案和目錄,打包時可以呼叫gzip bzip2 xz進行壓縮
壓縮: tar -[zjJ]cv -f filename.后綴 要被壓縮的檔案或目錄
查詢: tar -[zjJ]tv -f filename.后綴
解壓: tar -[zjJ]xv -f filename.后綴 -C /PATH/TO/SOMEDIR
cpio命令在壓縮的時候需要配合find命令一起使用,注意使用相對路徑
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/87953.html
標籤:Linux
