壓縮與解壓縮
- Linux獨有壓縮格式及命令工具:
- gzip---> .gz
- bzip2---> .bz2
- xz---> .xz
- 壓縮命令格式
- gzip [選項...] 文件名
- 常用選項:-d 解壓縮
- bzip2 [選項...] 文件名
- 常用選項:-d 解壓縮
- xz [選項...] 文件名
- 常用選項:-d 解壓縮
- gzip [選項...] 文件名
- 查看壓縮文件內容
- zcat [選項...] 文件名 #查看gzip格式壓縮文件
- bzcat [選項...] 文件名
- xzcat [選項...] 文件名
[root@localhost ~]# cp /etc/services /opt
[root@localhost ~]# cd /opt
[root@localhost opt]# ll services
-rw-r--r--. 1 root root 670293 4月 17 17:06 services
[root@localhost opt]# ll -h services
-rw-r--r--. 1 root root 655K 4月 17 17:06 services
#使用gzip格式對文件進行壓縮
[root@localhost opt]# gzip services
[root@localhost opt]# ls
services.gz
[root@localhost opt]# ll -h services.gz
-rw-r--r--. 1 root root 133K 4月 17 17:06 services.gz
#不解壓查看壓縮文件內容
[root@localhost opt]# zcat services.gz
#解壓文件
[root@localhost opt]# gzip -d services.gz
#使用bzip2格式對文件進行壓縮
[root@localhost opt]# bzip2 services
[root@localhost opt]# ls
services.bz2
[root@localhost opt]# ll -h services.bz2
-rw-r--r--. 1 root root 122K 4月 17 17:06 services.bz2
#不解壓查看文件內容
[root@localhost opt]# bzcat services.bz2
#解壓文件
[root@localhost opt]# bzip2 -d services.bz2
#使用xz格式對文件進行壓縮
[root@localhost opt]# xz services
[root@localhost opt]# ls
services.xz
[root@localhost opt]# ll -h services.xz
-rw-r--r--. 1 root root 98K 4月 17 17:06 services.xz
#解壓文件
[root@localhost opt]# xz -d services.xz
tar打包工具
-
tar命令用在linux下用於對文件/目錄打包,使用 tar 程序打出來的包常稱為 tar 包,tar 包文件通常都是以 .tar 結尾
-
tar 命令格式:tar 選項 打包后名字 被打包文件
-
常用選項:
- -c 創建打包文件
- -f 指定打包后的文件名稱
- -z 調用gzip壓縮工具 -J 調用xz壓縮工具 -j 調用bzip2壓縮工具
- -t 列出打包文檔內容
- -x 釋放打包文件
- -C 指定解壓路徑
- -v 顯示詳細信息
-
tar命令范例
#同時打包多個文件/目錄並使用gzip格式壓縮
[root@localhost opt]# tar -czf xxx.tar.gz /etc/passwd /etc/fstab /home
#將壓縮包數據解壓到/media目錄
[root@localhost opt]# tar -xf xxx.tar.gz -C /media/
[root@localhost opt]# ls /media/etc
[root@localhost opt]# rm -rf xxx.tar.gz
#同時打包多個文件/目錄並使用xz格式壓縮
[root@localhost opt]# tar -cJf xx.tar.xz /etc/hostname /etc/services /home
#錯誤語法,f選項要放到所有選項右邊
[root@localhost opt]# tar -ft xx.tar.xz
tar: 您必須從"-Acdtrux"或是"--test-label"選項中指定一個
請用“tar --help”或“tar --usage”獲得更多信息。
#不解壓查看壓縮包數據
[root@localhost opt]# tar -tf xx.tar.xz
etc/hostname
#將壓縮包數據解壓到/tmp目錄
[root@localhost opt]# tar -vxf xx.tar.xz -C /tmp
[root@localhost opt]# ls /tmp
#同時打包多個文件/目錄並使用bzip2格式壓縮
[root@localhost opt]# tar -cjf abc.tar.bz2 /etc/hostname /etc/group /home
#解壓縮
[root@localhost opt]# tar -xf abc.tar.bz2 -C /media/