1. 程式人生 > 其它 >Linux 檔案目錄壓縮與解壓命令

Linux 檔案目錄壓縮與解壓命令

Linux 檔案目錄壓縮與解壓命令

compress壓縮: compress是個歷史悠久的壓縮程式,檔案經它壓縮後,其名稱後面會多出 ".Z" 的副檔名。

[root@localhost ~]# yum install -y ncompress
[root@localhost ~]# compress --help

命令語法:[ compress [選項] 檔案或目錄 ]

        -f              #強制覆蓋掉目標檔案
        -c              #將結果送到標準輸出,無檔案被改變
        -r              #遞迴的操作方式
        -b 數字         #壓縮效率是一個介於9~16
        -d              #對檔案進行解壓縮而非壓縮
        -v              #顯示指令執行過程

通過使用compress -c命令壓縮一個檔案.

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# compress -c bash > bash.Z

[root@localhost ~]# ls -lh
total 1.6M
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash
-rw-r--r--. 1 root root 596K Nov 16 06:38 bash.Z

通過使用compress -d

命令解壓一個檔案.

[root@localhost ~]# ls -lh
total 596K
-rw-r--r--. 1 root root 596K Nov 16 06:38 bash.Z

[root@localhost ~]# compress -d bash.Z

[root@localhost ~]# ls -lh
total 944K
-rw-r--r--. 1 root root 942K Nov 16 06:38 bash

zip壓縮: zip命令壓縮的拓展名.zip 各種系統都支援zip的壓縮格式,所以在一定程度上,是可以通用的.

[root@localhost ~]# yum install -y zip unzip
[root@localhost ~]# zip --help

命令語法:[ zip/unzip [選項] 檔案或目錄 ]

        -r              #遞迴壓縮,連同子目錄一同壓縮
        -S              #包含系統和隱藏檔案
        -v              #顯示指令執行過程
        -q              #不顯示指令執行過程

通過使用zip -r -v 命令將/etc/目錄全部內容壓縮.

[root@localhost ~]# zip -r -v lyshark.zip /etc/

[root@localhost ~]# ls -lh
total 12M
-rw-r--r--. 1 root root 12M Nov 16 09:46 lyshark.zip

使用unzip -l命令查詢一個壓縮包中的檔案.

[root@localhost ~]# unzip -l lyshark.zip

使用unzip -d命令將檔案解壓到指定目錄.

[root@localhost ~]# unzip lyshark.zip -d /tmp/

[root@localhost ~]# ls -lh /tmp/
total 12K
drwxr-xr-x. 84 root root 8.0K Nov  6 11:02 etc

gzip壓縮: gzip是Linux系統預設支援的壓縮格式,其可以與tar命令結合使用,gzip對文字檔案有60%~70%的壓縮率。

[root@localhost ~]# gzip --help

命令語法:[ gzip/zcat [選項] 檔案或目錄 ]

        -a              #使用ASCII文字模式
        -d              #解開壓縮檔案
        -f              #強行壓縮檔案
        -l              #列出壓縮檔案的相關資訊
        -n              #壓縮時,不儲存原來的檔名稱及時間戳
        -N              #壓縮時,儲存原來的檔名稱及時間戳
        -q              #不顯示警告資訊
        -r              #遞迴處理
        -v              #顯示指令執行過程

通過使用gzip -rv命令壓縮一個檔案.

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# gzip -rv bash
bash:    51.2% -- replaced with bash.gz

[root@localhost ~]# ls -lh
total 460K
-rwxr-xr-x. 1 root root 460K Sep 26  2017 bash.gz

通過使用gzip -l/zcat -l命令查詢一個檔案.

[root@localhost ~]# gzip -l bash.gz

         compressed        uncompressed  ratio uncompressed_name
             470300              964544  51.2% bash

[root@localhost ~]# zcat -l bash.gz
         compressed        uncompressed  ratio uncompressed_name
             470300              964544  51.2% bash

通過使用gzip -dv / zcat命令解壓一個檔案.

[root@localhost ~]# ls -lh
total 460K
-rwxr-xr-x. 1 root root 460K Sep 26  2017 bash.gz

[root@localhost ~]# gzip -dv bash.gz
bash.gz:         51.2% -- replaced with bash

[root@localhost ~]# zcat -d bash.gz >bash_zcat

bzip2壓縮: bzip2命令用於建立和管理,包括解壓縮,使用這個命令必須要先安裝,因為這個命令並沒有整合安裝.

[root@localhost ~]# yum install -y bzip2
[root@localhost ~]# bzip2 --hlep

命令語法:[ bzip2/bzcat [選項] 檔案或目錄 ]

        -c              #輸出壓縮解壓過程
        -d              #執行解壓縮
        -k              #解壓後保留原始檔案
        -v              #壓縮或解壓時詳細輸出
        -z              #強制執行壓縮

通過使用bzip2 -kzv命令將bash檔案壓縮.

[root@localhost ~]# ls -lh
total 944K
-rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# bzip2 -kzv bash
  bash:     2.164:1,  3.697 bits/byte, 53.79% saved, 964544 in, 445742 out.

通過使用bzip2 -kdv命令解壓縮bash.bz2檔案.

[root@localhost ~]# ls -lh
total 436K
-rwxr-xr-x. 1 root root 436K Sep 26  2017 bash.bz2

[root@localhost ~]# bzip2 -kdv bash.bz2
  bash.bz2: done

tar 歸檔並壓縮: 該命令常常與gz,bz2等壓縮命令結合使用。

[root@localhost ~]# tar --hlep

命令語法:[ tar [選項] 檔案或目錄 ]

        -c              #新建打包
        -t              #檢視打包檔案
        -x              #解壓打包檔案
        -j              #通過bz2進行壓縮與解壓
        -z              #通過gz進行壓縮與解壓
        -J              #通過xz進行壓縮與解壓
        -v              #顯示壓縮與解壓過程
        -C              #指定解壓到哪裡

通過使用tar -czvf xzvf tvf命令壓縮解壓與查詢.

[root@localhost ~]# tar -czvf lyshark.tar.gz /etc/*        #將/etc/*目錄下的內容壓縮成lyshark.tar.gz
[root@localhost ~]# tar -tvf lyshark.tar.gz                #查詢包中的內容
[root@localhost ~]# tar -xzvf lyshark.tar.gz -C /etc/      #將壓縮包,解壓到/etc/目錄下

通過使用tar -cjvf xjvf tvf命令壓縮解壓與查詢.

[root@localhost ~]# tar -cjvf lyshark.tar.bz2 /etc/*        #將/etc/*目錄下的內容壓縮成lyshark.tar.bz2
[root@localhost ~]# tar -tvf lyshark.tar.bz2                #查詢包中的內容
[root@localhost ~]# tar -xjvf lyshark.tar.bz2 -C /etc/      #將壓縮包,解壓到/etc/目錄下

通過使用tar -cJvf xJvf tvf命令壓縮解壓與查詢.

[root@localhost ~]# tar -cJvf lyshark.tar.xz /etc/*         #將/etc/*目錄下的內容壓縮成lyshark.tar.xz
[root@localhost ~]# tar -tvf lyshark.tar.xz                 #查詢包中的內容
[root@localhost ~]# tar -xJvf lyshark.tar.xz -C /etc/       #將壓縮包,解壓到/etc/目錄下

cpio塊級壓縮: cpio用來建立或者還原備份的工具程式,cpio命令可以備份任何東西包括裝置檔案。

[root@localhost ~]# cpio --help

壓縮語法:[ cpio –cvBo > [檔案|裝置] ]

        -c              #使用portable format儲存方式
        -v              #顯示過程
        -B              #設定輸入輸出塊為512bytes
        -o              #進入copy-out模式(壓縮)

檢視語法:[ cpio –ivct < [cpio檔案] ]

        -i              #將資料從檔案複製到系統中
        -c              #使用portable format儲存方式
        -t              #檢視CPIO壓縮包內容

還原語法:[ cpio –idvcu < [cpio檔案] ]

        -i              #進入copy-in模式(解壓)
        -d              #恢復到指定位置(視壓縮方式恢復)
        -v              #顯示過程
        -c              #使用portable format儲存方式
        -u              #替換所有檔案,無提示

使用cpio -cvBo命令,來備份/etc/目錄

[root@localhost ~]# find /etc/ -print | cpio -cvBo > /root/etc.cpio

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

使用cpio -cvBo命令,將/etc/目錄下的內容備份到/sdb磁碟

[root@localhost ~]# find /etc/ -print | cpio -cvBo > /dev/vdb

使用cpio -ivct命令查詢,壓縮包內容

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

[root@localhost ~]# cpio -ivct < etc.cpio

使用cpio -idvcu還原檔案到/etc/目錄

[root@localhost ~]# ls -lh
total 109M
-rw-r--r--. 1 root root 109M Nov 16 10:50 etc.cpio

[root@localhost ~]# cpio -idvcu < /root/etc.cpio

製作ISO映象: mkisofs命令用來將指定的目錄與檔案做成ISO 9660格式的映像檔案。

[root@localhost ~]# mkisofs --help

命令語法:[ mkisofs [選項] 檔案或目錄 ]

        -o              #後面接映象名
        -r              #連同檔案屬性一起記錄
        -v              #顯示構建過程
        -m              #排除指定檔案
        -V              #新建volume

通過mkisofs製作一個映象將/etc目錄製作成lyshark.iso.

[root@localhost ~]# mkisofs -r -v -o /root/lyshark.iso /etc/

[root@localhost ~]# ls -lh
total 123M
-rw-r--r--. 1 root root 123M Nov 16 11:14 lyshark.iso

磁碟測速與拷貝: dd命令用於磁碟測速與備份,該命令很底層所以可以完成很多備份工具無法完成的功能。

[root@localhost ~]# dd --help

備份語法:[ dd [if=原始檔] [of=輸出] [count=計數] ]

        if=檔名        #輸入檔名
        of=檔名        #輸出檔名
        bs=bytes        #讀入計數
        count=blocks    #僅拷貝blocks個塊
        /dev/zero       #是字元裝置會不斷返回0值位元組
        /dev/null       #空裝置,相當於一個垃圾桶

通過使用dd進行磁碟測速,測磁碟讀寫速度(可分別測試1024,2048,4096,8192)

[root@localhost ~]# dd if=/dev/zero of=/dev/null bs=1024 count=10000
10000+0 records in
10000+0 records out
10240000 bytes (10 MB) copied, 0.00852247 s, 1.2 GB/s

通過使用dd進行磁碟常規備份

#將本地的 /dev/sda 整盤備份到 /dev/hdd
[root@localhost ~]# dd if=/dev/sda /of=/dev/hdd

#將 /dev/sda 全盤資料備份成指定路徑的image檔案
[root@localhost ~]# dd if=/dev/sda of=/tmp/image

#將image備份檔案恢復到指定盤/dev/sdb
[root@localhost ~]# dd if=/tmp/image of=/dev/sdb

#備份 /dev/sda 全盤資料,並利用gzip工具進行壓縮,儲存到/tmp/image.gz
[root@localhost ~]# dd if=/dev/sda | gzip > /tmp/image.gz

#恢復,將壓縮的備份檔案 /tmp/image.gz ,恢復到制定盤 /dev/sdb
[root@localhost ~]# gizp -dc /tmp/image.gz | dd of=/dev/sdb

通過使用dd命令備份磁碟MBR,與恢復分割槽MBR.

#備份MBR,備份磁碟 /dev/sda 開始的512個位元組大小的MBR資訊到指定檔案/tmp/mbr512
#其中:count=1 指僅拷貝一個塊;bs=512 指塊大小為512個位元組
[root@localhost ~]# dd if=/dev/sda of=/tmp/mbr512 count=1 bs=512
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.00015245 s, 3.4 MB/s

#恢復MBR,將備份 /tmp/mbr512 的MBR資訊寫到磁碟開始部分 /dev/sda
[root@localhost ~]# dd if=/tmp/mbr512 of=/dev/sda
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.000201242 s, 2.5 MB/s

通過使用dd命令拷貝記憶體資料與光碟資料到指定位置。

#拷貝記憶體內容到磁碟
[root@localhost ~]# dd if=/dev/mem of=/tmp/mem_image bs=1024(指定塊大小為1k)
dd: error reading ‘/dev/mem’: Bad address
632+0 records in
632+0 records out
647168 bytes (647 kB) copied, 0.001591 s, 407 MB/s

#拷貝光碟內容到指定資料夾,並儲存為/tmp/LyShark.iso檔案
[root@localhost ~]# dd if=/dev/sr0 of=/tmp/LyShark.iso
1249+0 records in
1248+0 records out
638976 bytes (639 kB) copied, 0.368021 s, 1.7 MB/s

#利用隨機數填充磁碟,徹底銷燬磁碟 /dev/sdb 資料
[root@localhost ~]# dd if=/dev/urandom of=/dev/sdb
文章出處:https://www.cnblogs.com/LyShark/p/15750023.html
版權宣告:本部落格文章與程式碼均為學習時整理的筆記,部落格中除去明確標註有參考文獻的文章,其他文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!

如果您惡意轉載本人文章並被本人發現,則您的整站文章,將會變為我的原創作品,請相互尊重 !