天天看點

Linux基礎知識-壓縮、解壓縮與歸檔~19-07.29

1、壓縮&解壓縮

(zip下載下傳)

mount /dev/sr0 /media/

yum mackcache

yum install zip

1.1、zip&uzip

  • zip 壓縮檔案名 (test.zip) 要被壓縮的檔案名
  • zip -m test1.zip test.txt 把test2.txt添加到tset1.zip
  • zip -d test1.zip test2.txt 把test2.txt從test1.zip删除
  • zip test1.zip *.txt -x test10.txt 除test10以外,将所有.txt檔案全部壓縮到test1.zip
  • zip -r /dir 遞歸壓縮
  • unzip test1.zip
  • unzip test1.zip -d /壓縮目錄
  • unzip -v test1.zip 不壓縮,檢視壓縮内容

1.2、gzip,gunzip

  • gzip 被壓縮的檔案 壓縮後,源檔案不保留
  • gzip -c test.txt >test.txt.gz 壓縮,保留原檔案
  • gzip -r 目錄 壓縮目錄下的所有内容
  • gunzip test.txt.gz
  • gunzip -c test.txt.gz > /root/test.txt 解壓縮到特定的目錄下,并且保留源檔案
  • gunzip === gzip -d
  • 不解壓檢視

    [[email protected] test1]# zless test5.txt.gz

    [[email protected] test1]# zless test5.txt.gz

1.3、bzip2,bunzip2

  • bzip2 被壓縮的檔案名 ,壓縮後,源檔案不保留
  • bzip2 -c test.txt > test.txt.bz2 壓縮,保留源檔案
  • bunzip2 test.txt.bz2
  • bunzip2 === bzip2 -d
  • 不解壓檢視

    [[email protected] test1]# bzcat test5.txt.bz2

1.4、xz,unxz

  • xz 被壓縮的檔案名 ,壓縮後,源檔案不保留

    xz dir/*

  • unxz test.txt.xz
  • xz -d test.txt.xz

    xz -d dir/*

  • List itemxzcat test.txt.xz 檢視壓縮後的檔案内容

1.5、tar(用在自己建立的檔案)

  • tar -cf etc.tar /etc/

    -c 建立一個歸檔檔案

    -f 指向一個歸檔檔案

    /etc/ 歸檔檔案

  • tar -czf boot.tar.gz /boot/

    -z 以gzip形式将打包的檔案壓縮

  • tar -cjf boot.tar.bz2 /tmp/

    -j bzip2的形式将打包的檔案壓縮

  • tar -tf etc.tar

    -t 檢視歸檔檔案内容

  • tar --delete etc/hosts -f etc.tar

    –delete 删除etc.tar下的 etc/hosts

  • tar -f etc.tar -r /root/anaconda-ks.cfg.bak

    -r 向打封包件追加新内容 -r 要新加的内容

  • tar tvf etc.tar |grep hosts

    -v 檢視詳細内容

  • tar xzf boot.tar.gz

    -x 解壓

    -xz 解壓gzip壓縮到歸檔檔案

  • tar xjf boot.tar.bz

    -xf 解壓bzip2壓縮的歸檔檔案

  • tar xjf tmp.tar.bz2 -C test/

    -C 指定解壓路徑

  • tar -czvf test.tar.gz file* --remove-files

    –remove-file 打包壓縮的同時删除原檔案

  • tar xzvf test.tar.gz && rm -rf test.txt.gz

    && 兩步,前一步解壓檔案,後一步把源檔案删除

1.6、cpio

(cpio用于系統檔案歸檔與壓縮)

  • -t 看歸檔内容
  • -F 指定歸檔檔案
  • -o: copy-out模式
  • -A 向歸檔的檔案中追加新檔案
  • -d 自動建立目錄
  • find ./ -depth |cpio -ov -F tree1.cpio

    把目前的檔案,歸檔到tree1.cpio,terr.cpio裡面本身有

  • find ./ -depth |cpio -ov -F /tmp/tree1.cpio

    把目前檔案歸檔到/tmp/目錄下

  • find ./ -depth -print0 |cpio -null -ov -F /tmp/tree.cpio

    –null 解析空字元

  • cpio -t -F tree.cpio 或者cpio -t < tree.cpio

    檢視tree.cpio下的歸檔内容

  • cpio -t -F /tmp/home.cpio /root/*

    檢視home.cpio歸檔檔案裡的 /root/下的内容(沒有隐藏檔案)

  • cpio -t -F /tmp/home.cpio /root/.*

    檢視home.cpio歸檔檔案裡的 /root/下的内容(全隐藏檔案)

  • cpio -t -F /tmp/home.cpio /root/{.,}

    檢視home.cpio歸檔檔案裡的 /root/下的内容(所有檔案)

  • ls /root/test.txt |cpio -oA -F /tmp/home.cpio

    向歸檔檔案中追加檔案

  • find /boot-depth -print0 |cpio -oA -F /tmp/home.cpio

    添加新目錄

  • cpio -idv -F /tmp/home.cpio

    提取檔案

  • find~ -depth -print0 |cpio --null -pvd /tmp/abc

    複制~下檔案到/tmp/abc/root/