1. 程式人生 > >2018.4.16 四周第一次課

2018.4.16 四周第一次課

壓縮工具介紹 gzip壓縮工具 bizp2壓縮工具 xz壓縮工具

file命令
概念:file命令可以查看一個文件或者目錄屬於什麽的。
[root@localhost tmp]# file 1.txt.gz  #查看這個文件是什麽類別的
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Apr 14 14:43:49 2018

壓縮包介紹

概念:在Windows下我們接觸最多的壓縮文件 .rar 格式;
但在Linux下,不能識別這種格式,他有自己獨特的壓縮工具;
但.zip格式的文件在Windows和Linux下都可以使用;
Windows常見的壓縮文件:.rar 、.zip 、.7Z
Linux常用的壓縮文件:.zip 、.gz 、.bz2 、.xz 、.tar 、.gz 、
.tar 、.bz2 、.tar.xz
壓縮文件,不僅能節省磁盤空間,而且在傳輸能節省網絡帶寬。

gzip壓縮工具(不能壓縮目錄)

命令格式:
gzip [-d#] filename,其中#為1-9的數字
-d 該參數在解壓縮時使用
-# 表示壓縮等級,1為最差,9為最好,6為默認

準備工作

創建一個目錄

[root@localhost tmp]# mkdir d6z
[root@localhost tmp]# cd d6z/

find搜索一個大的文件出來

[root@localhost d6z]# find /etc/ -type f -name "*conf"

find 搜索,/etc/目錄下,-type f 指定搜索類型為f普通文檔的文件,-name "*conf"查找名字以為conf結尾的所有文件。

/etc/resolv.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/version-groups.conf
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf

把搜索的這些文件的內容,輸出到一個文件裏

[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;

find 搜索,/etc/目錄下,-type f 指定搜索類型為f普通文檔的文件,-name "*conf"查找名字以為conf結尾的所有文件,-exec推動後面的命令,cat {} 針對所有文件挨個查看 {}表示列出來的文件,>> 追加到 1.txt文件裏 \;

[root@localhost d6z]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost d6z]# !du
du -sh 1.txt #查看這個文件的大小
2.2M 1.txt
[root@localhost d6z]# wc -l 1.txt #查看這個文件的行數總共有多少
37765 1.txt

接下來用gzip壓縮它
[root@localhost d6z]# gzip 1.txt
[root@localhost d6z]# ls
1.txt.gz
技術分享圖片
[root@localhost d6z]# du -sh 1.txt.gz #再來查看這個文件的大小
372K 1.txt.gz

解壓gzip包

[root@localhost d6z]# gzip -d 1.txt.gz #選項-d在解壓縮時使用
[root@localhost d6z]# ls #查看文件
1.txt
[root@localhost d6z]# du -sh 1.txt #查看大小,這裏跟之前的不一樣,是因為這個文件裏本來就有虛空間。
1.5M 1.txt

查看壓縮文件的信息,用file

[root@localhost d6z]# file 1.txt.gz
1.txt.gz: gzip compressed data, was "1.txt", from Unix, last modified: Sat Apr 14 14:14:22 2018

查看壓縮文件的內容,用zcat

[root@localhost d6z]# zcat 1.txt.gz

壓縮時候,指定到一個目錄,並且不讓這個原文件消失

[root@localhost d6z]# gzip -d 1.txt.gz #先給之前壓縮的文件解壓
gzip: 1.txt.gz: No such file or directory
[root@localhost d6z]# gzip -c 1.txt > /tmp/1.txt.gz

gzip解壓縮 -c指定 1.txt文件,>重鏡像到,/tmp/1.txt.gz

[root@localhost d6z]# ls
1.txt
[root@localhost d6z]# ls /tmp/1.txt.gz
/tmp/1.txt.gz
技術分享圖片

解壓時,不刪除,指定一個地方

[root@localhost d6z]# gzip -d -c /tmp/1.txt.gz > /tmp/d6z/2.txt
技術分享圖片

bzip2壓縮工具 (不支持目錄壓縮)這個比gzip壓縮更狠一點

概念:操作與gzip一樣,只是比gzip壓縮的更狠一點,占用的cpu越多。
bzip2命令格式為:
bzip2 [-dz] filename
參數選項只有 -d(解壓縮)  -z(壓縮)
壓縮級別分別是1~9,默認是9級。

yum安裝源碼包bzip2

[root@localhost d6z]# yum install -y bzip2

xz壓縮工具(不能壓縮目錄)

概念:操作跟gzip和bizp2是一樣的。 這個比gzip和dzip2都狠。
參數-d (解壓縮) -z(壓縮)

具體示例如下:

[root@localhost d6z]# xz 1.txt #壓縮1.txt文件
[root@localhost d6z]# ls #查看
1.txt.xz 2.txt
[root@localhost d6z]# xz -d 1.txt.xz #解壓縮1.txt.xz文件
[root@localhost d6z]# ls #查看
1.txt 2.txt
[root@localhost d6z]# xz -c 1.txt > /tmp/3.txt.xz #重鏡像1.txt文件到/tmp目錄下新生成3.txt.xz,保留原文件
[root@localhost d6z]# ls /tmp/
1.txt.gz systemd-private-d4565e90fd384c749a36d51b19e3e377-chronyd.service-91Lo0Y
2.txt systemd-private-d4565e90fd384c749a36d51b19e3e377-vgauthd.service-XKotz4
3.txt.xz systemd-private-d4565e90fd384c749a36d51b19e3e377-vmtoolsd.service-NZwSqa
d6z yum.log
ks-script-5ToQJK
[root@localhost d6z]# xz -d -c /tmp/3.txt.xz >3.txt
[root@localhost d6z]# ls
1.txt 2.txt 3.txt
技術分享圖片

2018.4.16 四周第一次課