tar/gzip/zip文件打包、压缩命令
一、tar打包備份工具
1.命令功能
tar 將多個文件或目錄打包在一起,可用通過調用gzip或zip實現壓縮、解壓的命令;tar不僅可以多多個文件進行打包,還可以對多個文件打包后進行壓縮。
2.語法格式
tar? option? file
tar? ?選項?? 文件或目錄
選項說明
| 選項選項 | 選項選項說明 |
| c | 創建新的tar包 * |
| v | 顯示詳細tar執行過程* |
| f | 指定壓縮的文件名字* |
| t | 不解壓查看tar包的內容* |
| p | 保持文件的原有屬性* |
| j | 通過bzip2命令壓縮或解壓* |
| z | 通過gzip壓縮或解壓* |
| x | 解壓tar包* |
| C | 指定解壓的目錄路徑* |
| ? | ? |
3.使用范例
范例1 備份/etc目錄
| 1 2 3 4 5 6 7 8 9 10 11 12 | [root@cxf chu]# tar -zcvf etc.gz? /etc/ ...... [root@cxf chu]# tar -zcvf network.gz /etc/sysconfig/ ? tar: 從成員名中刪除開頭的“/” /etc/sysconfig/ /etc/sysconfig/clock /etc/sysconfig/keyboard [root@cxf chu]# ls -l 總用量 9420 -rw-r--r-- 1 root root 9589948 5月?? 8 02:53 etc.gz -rw-r--r-- 1 root root?? 49488 5月?? 8 03:01 network.gz |
說明:tar –zcvf etc.gz? /etc/?
z:gzip壓縮文件;c:創建tar包;v:顯示tar執行過程;f:指定壓縮文件名
etc.gz:壓縮文件名;/etc/ 打包源文件
范例2 查看壓縮包的內容
| 1 2 3 4 | [root@cxf chu]# tar -ztvf network.gz drwxr-xr-x root/root???????? 0 2018-05-07 01:28 etc/sysconfig/ -rw-r--r-- root/root??????? 21 2017-07-31 18:53 etc/sysconfig/clock -rw-r--r-- root/root??????? 63 2017-07-31 18:53 etc/sysconfig/keyboard |
說明:tar –ztvf network.gz
z:gzip解壓或壓縮文件,此處解壓文件;t:不解壓查看tar包文件;v:顯示tar執行過程;f:指定壓縮的文件名。
范例3 解開壓縮包
| 1 2 3 4 5 6 | [root@cxf chu]# tar -zxf network.gz [root@cxf chu]# ls?? etc? etc.gz? network.gz [root@cxf chu]# ls -l etc 總用量 4 drwxr-xr-x 7 root root 4096 5月?? 7 01:28 sysconfig |
說明:解壓后有一個etc目錄,etc目錄下才是sysconfig目錄;故打包目錄時,一般先cd到打包。目錄的上一級目錄中后在進行打包。
示例4 打包/etc/目錄下的所有的普通文件
| 1 2 3 | [root@cxf /]# tar zcvf etc.tar.gz `find etc/ -type f` [root@cxf /]# ls -l etc.tar.gz -rw-r--r-- 1 root root 9578175 5月?? 8 03:26 etc.tar.gz |
二、 gzip 壓縮或解壓文件
1.命令功能
gzip命令的作用是將一個大的文件通過壓縮算法,變成一個小的文件,gzip文件不能直接壓縮目錄,如果要壓縮目錄,需要先將目錄打包成一個文件,在壓縮。
2.語法格式
gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ...? ]
或gzip? option? file
| 選項 | 選項說明 |
| -c | 將內容輸出到標準輸出,不改變原始文件 **** |
| -d | 解壓文件 |
| -v | 顯示執行過程 |
| -r | 對目錄下的所有文件遞歸進行壓縮操作 |
| -數字(1-9) | 指定壓縮率,默認是6,值越大壓縮率越高 |
| -t | 測試,檢查壓縮文件是否完整。 |
| -l | 列出壓縮文件的內容信息 |
3.使用范例
| 1 2 3 | [root@cxf test]# touch {1..6}.txt [root@cxf test]# ls 1.txt? 2.txt? 3.txt? 4.txt? 5.txt? 6.txt |
示例1 把test目錄下的每個文件都單獨壓縮成.gz文件
| 1 2 3 | [root@cxf test]# gzip *.txt [root@cxf test]# ls 1.txt.gz? 2.txt.gz? 3.txt.gz? 4.txt.gz? 5.txt.gz? 6.txt.gz |
示例2 不解壓,顯示每個壓縮文件的信息。
| 1 2 3 4 5 6 7 8 9 | [root@cxf test]# gzip -l *.gz ?????????compressed??????? uncompressed? ratio uncompressed_name ?????????????????38????????????????? 12? 50.0% 1.txt ?????????????????40????????????????? 14? 42.9% 2.txt ?????????????????44????????????????? 30? 60.0% 3.txt ?????????????????26?????????????????? 0?? 0.0% 4.txt ?????????????????26?????????????????? 0?? 0.0% 5.txt ?????????????????26?????????????????? 0?? 0.0% 6.txt ????????????????200????????????????? 56 -200.0% (totals) |
示例3 解壓文件,顯示解壓過程。
| 1 2 3 4 5 6 7 | [root@cxf test]# gzip -dv *.gz 1.txt.gz:??????? 50.0% -- replaced with 1.txt 2.txt.gz:??????? 42.9% -- replaced with 2.txt 3.txt.gz:??????? 60.0% -- replaced with 3.txt 4.txt.gz:???????? 0.0% -- replaced with 4.txt 5.txt.gz:???????? 0.0% -- replaced with 5.txt 6.txt.gz:???????? 0.0% -- replaced with 6.txt |
三、 zip 打包和壓縮文件
1.命令功能
zip是linux和windows等多平臺通用的壓縮格式。zip比gzip更強的是zip命令壓縮文件不會刪除源文件,還可以壓縮目錄。
2.語法格式
zip? option? file
| 選項 | 選項說明 |
| -r | 將指定目錄下的所有文件和子目錄一并壓縮 |
| -x | 壓縮文件時排查某個文件 |
| -q | 不顯示壓縮信息 |
3.使用范例
示例1 壓縮文件
| 1 2 3 4 5 6 7 8 9 | [root@cxf test]# cp /etc/services . [root@cxf test]# ls services [root@cxf test]# zip services.zip ./services ??adding: services (deflated 80%) [root@cxf test]# ls -l 總用量 756 -rw-r--r-- 1 root root 641020 5月? 12 01:59 services -rw-r--r-- 1 root root 127362 5月? 12 01:59 services.zip |
示例2 壓縮目錄
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@cxf test]# cd / [root@cxf /]# zip tmp.zip ./tmp/ ??adding: tmp/ (stored 0%) [root@cxf /]# zip -r tmp.zip ./tmp/ updating: tmp/ (stored 0%) ??adding: tmp/yum_save_tx-2018-04-24-15-13M35BLW.yumtx (deflated 93%) ??adding: tmp/yum_save_tx-2017-09-04-18-02kOlm9G.yumtx (deflated 26%) ??adding: tmp/yum_save_tx-2018-05-03-18-09pp_ew1.yumtx (deflated 57%) ??adding: tmp/.ICE-unix/ (stored 0%) ??adding: tmp/oldboy.txt (deflated 27%) ??adding: tmp/yum_save_tx-2018-04-24-16-35GSVJdg.yumtx (deflated 68%) ??adding: tmp/yum_save_tx-2017-09-04-18-01uVYqM6.yumtx (deflated 27%) ??adding: tmp/yum_save_tx-2017-09-15-02-36xHd5QO.yumtx (deflated 94%) ??adding: tmp/yum_save_tx-2018-04-24-16-34EZkcRv.yumtx (deflated 28%) ??adding: tmp/yum.log (stored 0%) |
示例3 排查壓縮
| 1 2 3 4 5 6 7 8 9 10 11 12 | [root@cxf /]# zip -r tmp1.zip ./tmp/ -x tmp/services.zip ??adding: tmp/ (stored 0%) ??adding: tmp/yum_save_tx-2018-04-24-15-13M35BLW.yumtx (deflated 93%) ??adding: tmp/yum_save_tx-2017-09-04-18-02kOlm9G.yumtx (deflated 26%) ??adding: tmp/yum_save_tx-2018-05-03-18-09pp_ew1.yumtx (deflated 57%) ??adding: tmp/.ICE-unix/ (stored 0%) ??adding: tmp/oldboy.txt (deflated 27%) ??adding: tmp/yum_save_tx-2018-04-24-16-35GSVJdg.yumtx (deflated 68%) ??adding: tmp/yum_save_tx-2017-09-04-18-01uVYqM6.yumtx (deflated 27%) ??adding: tmp/yum_save_tx-2017-09-15-02-36xHd5QO.yumtx (deflated 94%) ??adding: tmp/yum_save_tx-2018-04-24-16-34EZkcRv.yumtx (deflated 28%) ??adding: tmp/yum.log (stored 0%) |
總結
以上是生活随笔為你收集整理的tar/gzip/zip文件打包、压缩命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统命令make、clean、
- 下一篇: 重载和const形参