溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

文件壓縮與歸檔

發(fā)布時(shí)間:2020-07-17 12:38:28 來(lái)源:網(wǎng)絡(luò) 閱讀:382 作者:YOUNG_GEEK 欄目:大數(shù)據(jù)

數(shù)據(jù)壓縮,就是在不丟失數(shù)據(jù)信息的前提下減少數(shù)據(jù)量的一種技術(shù)。

compress

compress是一個(gè)古老的壓縮工具,其壓縮文件后綴為.Z。

-d: 解壓縮-c: 結(jié)果輸出至標(biāo)準(zhǔn)輸出,不刪除原文件
-v: 顯示詳情

使用示例

[root@centos7 /testdir]#compress passwd     # 壓縮
[root@centos7 /testdir]#ls
passwd.Z
[root@centos7 /testdir]#compress -d passwd  # 解壓

gzip

gzip壓縮后的文件后綴為.gz,如果壓縮的是tar備份文件,則擴(kuò)展名為.tar.gz

gzip, gunzip, zcat - compress or expand files

-d: 解壓縮,相當(dāng)于unzip
-c: 將壓縮或解壓縮的結(jié)果輸出至標(biāo)準(zhǔn)輸出
-#:1-9,指定壓縮比zcat: 不顯示解壓縮的前提下查看文本文件內(nèi)容

gunzip用于解壓縮

zcat用于查看

使用示例

[root@centos7 /testdir]#gzip passwd 
[root@centos7 /testdir]#ls
passwd.gz  passwd.Z
[root@centos7 /testdir]#
[root@centos7 /testdir]#zcat passwd.gz > passwd
[root@centos7 /testdir]#ls
passwd.gz  passwd  passwd.Z
[root@centos7 /testdir]#

bzip2

bzip2壓縮的文件的擴(kuò)展名為.bz2

-k: keep,保留原文件
-d: 解壓縮
-#:1-9,壓縮比,默認(rèn)為6bzcat: 不顯示解壓縮的前提下查看文件文件內(nèi)容

bunzip用于解壓縮

bzcat用于查看不解壓縮

使用示例

[root@centos7 /testdir]#bzip2 passwd
[root@centos7 /testdir]#ls
passwd.gz  passwd.bz2  passwd.Z[
root@centos7 /testdir]#bzcat passwd.bz2 > passwd
[root@centos7 /testdir]#ls
passwd.gz  passwd  passwd.bz2  passwd.Z
[root@centos7 /testdir]#

xz

xz壓縮后的文件擴(kuò)展名為.xz

-k: keep,保留原文件
-d: 解壓縮
-#:1-9,壓縮比,默認(rèn)為6xzcat: 不顯示解壓縮的前提下查看文件文件內(nèi)容

unxz用于解壓縮

xzcat用于查看

使用示例

[root@centos7 /testdir]#xz passwd
[root@centos7 /testdir]#ls
passwd.bz2  passwd.gz  passwd.xz  passwd.Z
[root@centos7 /testdir]#
[root@centos7 /testdir]#xzcat passwd.xz > passwd
[root@centos7 /testdir]#ls
passwd  passwd.bz2  passwd.gz  passwd.xz  passwd.Z
[root@centos7 /testdir]#

zip

打包壓縮文件,經(jīng)zip壓縮后會(huì)另外生成.zip的文件而不刪除原文件。

zip - package and compress (archive) files

-r: 遞歸處理,將指定目錄下的所有文件與子目錄一并處理
-q: 不顯示執(zhí)行過(guò)程

unzip用于解壓縮

zcat用于查看

使用示例

[root@centos7 /testdir]#zip -q passwd ./passwd
[root@centos7 /testdir]#ls
passwd  passwd.bz2  passwd.gz  passwd.xz  passwd.Z  passwd.zip
[root@centos7 /testdir]#

看看大概的壓縮情況:

[root@centos7 /testdir]#ll
total 192
-rw-r--r--. 1 root root 164065 Aug 19 09:06 message.zip
-rw-r--r--. 1 root root   4129 Aug 19 08:46 passwd
-rw-r--r--. 1 root root   1526 Aug 19 08:30 passwd.bz2
-rw-r--r--. 1 root root   1539 Aug 19 08:39 passwd.gz
-rw-r--r--. 1 root root   1540 Aug 19 08:45 passwd.xz
-rw-r--r--. 1 root root   2151 Aug 19 08:16 passwd.Z
-rw-r--r--. 1 root root   1676 Aug 19 09:02 passwd.zip
[root@centos7 /testdir]#

zcat

zcat命令可查看壓縮的文件,但并不解壓。

[root@bash ~]# zcat b.zip
#!/bin/bash
#在url中寫入你的51cto博客網(wǎng)址,保存退出,運(yùn)行腳本,可以根據(jù)需要自行修改
url=http://yolynn.blog.51cto.com/

tar

tar命令可為文件或目錄創(chuàng)建檔案(備份文件),tar命令可將很多文件打包成一個(gè)文件,從而可結(jié)合壓縮工具實(shí)現(xiàn)歸檔并壓縮了。

使用語(yǔ)法:

tar [OPTION...] [FILE]...

EXAMPLES
   tar -cf archive.tar foo bar
          # Create archive.tar from files foo and bar.

   tar -tvf archive.tar
          # List all files in archive.tar verbosely.

   tar -xf archive.tar
          # Extract all files from archive.tar.

常用參數(shù):

    -c: --creat, 創(chuàng)建新的備份文件
    
    -C dir:在特定的目錄解壓縮

    -f: --file=ARCHIVE, 指定備份文件

    -x: --extract, --get, 從備份文件中還原文件

    -t: --list, 列出備份文件的內(nèi)容

    -v: --verbose

tar用法小結(jié):

(1) 創(chuàng)建歸檔

tar -c -f /PATH/TO/SOMEFILE.tar FILE...
tar cf/PATH/TO/SOMEFILE.tar FILE...

(2) 查看歸檔文件中的文件列表

tar -t -f /PATH/TO/SOMEFILE.tar(3) 展開歸檔
tar -x -f /PATH/TO/SOMEFILE.tar
tar -x -f /PATH/TO/SOMEFILE.tar -C /PATH/

(4) 結(jié)合壓縮工具實(shí)現(xiàn):歸檔并壓縮

-j: bzip2, -z: gzip, -J: xz

打包成tar包:

tar -cvf passwd.tar passwd   僅打包,不壓縮
tar -zcvf passwd.tar.gz passwd  打包并以gzip壓縮
tar -jcvf passwd.tar.bz2 passwd   打包并以bzip2壓縮
tar -Jcvf passwd.tar.xz passwd    打包并以xz壓縮

使用示例

[root@centos7 /testdir]#tar -cf passwd.tar passwd
[root@centos7 /testdir]#ls
passwd  passwd.tar

[root @centos7 /testdir]#tar -zcf passwd.tar.gz passwd
[root@centos7 /testdir]#ls
passwd  passwd.tar  passwd.tar.gz

[root@centos7 /testdir]#tar -jcf passwd.tar.bz2 passwd
[root@centos7 /testdir]#ls
passwd  passwd.tar  passwd.tar.bz2  passwd.tar.gz

[root@centos7 /testdir]#tar -Jcf passwd.tar.xz passwd
[root@centos7 /testdir]#ls
passwd  passwd.tar  passwd.tar.bz2  passwd.tar.gz  passwd.tar.xz
[root@centos7 /testdir]#
[root@centos7 /testdir]#tar -tvf passwd.tar   # 查詢
-rw-r--r-- root/root     10240 2016-08-19 09:27 passwd
[root@centos7 /testdir]#tar -tvf passwd.tar.gz
-rw-r--r-- root/root     10240 2016-08-19 09:27 passwd
[root@centos7 /testdir]#
[root@centos7 /testdir]#tar xf passwd.tar  # 解壓
[root@centos7 /testdir]#ls
passwd  passwd.tar  passwd.tar.bz2  passwd.tar.gz  passwd.tar.xz
[root@centos7 /testdir]#tar xf passwd.tar.gz
[root@centos7 /testdir]#ls
passwd  passwd.tar  passwd.tar.bz2  passwd.tar.gz  passwd.tar.xz
[root@centos7 /testdir]#
[root@centos7 /testdir]#ll
total 44
-rw-r--r--. 1 root root 10240 Aug 19 09:27 passwd
-rw-r--r--. 1 root root 20480 Aug 19 10:52 passwd.tar
-rw-r--r--. 1 root root   116 Aug 19 10:53 passwd.tar.bz2
-rw-r--r--. 1 root root   120 Aug 19 10:52 passwd.tar.gz
-rw-r--r--. 1 root root   180 Aug 19 10:53 passwd.tar.xz

cpio

cpio命令是通過(guò)重定向的方式將文件進(jìn)行打包備份,還原恢復(fù)的工具,它可以解壓以.cpio或者.tar結(jié)尾的文件;換言之,cpio可以復(fù)制文件到歸檔包中,或者從歸檔包中復(fù)制文件。

使用語(yǔ)法:

cpio - copy files to and from archives

cpio[選項(xiàng)] > 文件名或者設(shè)備名
cpio[選項(xiàng)] < 文件名或者設(shè)備名

EXAMPLES
% ls | cpio -ov > directory.cpio   #必須要在當(dāng)前工作目錄中執(zhí)行l(wèi)s,后面接絕對(duì)路徑會(huì)報(bào)錯(cuò)

% find . -print -depth | cpio -ov > tree.cpio

% cpio -iv < directory.cpio

% cpio -idv < tree.cpio

% find . -depth -print0 | cpio --null -pvd new-dir

常用參數(shù):

-o: --create,Run in copy-out mode,將文件拷貝打包成文件或者將文件輸出到設(shè)備上

-i: --extract,Run in copy-in mode,解包,將打包文件解壓或?qū)⒃O(shè)備上的備份還原到系統(tǒng)

-t: 預(yù)覽,查看文件內(nèi)容或者輸出到設(shè)備上的文件內(nèi)容

-v: 顯示打包過(guò)程中的文件名稱。

-d: 解包生成目錄,在cpio還原時(shí),自動(dòng)的建立目錄

-c: 一種較新的存儲(chǔ)方式

使用示例

[root@centos7 /]#find ./etc |cpio -o > etc.cpio  # 備份/etc目錄

wKiom1e3MfKArn2SAABJ8zL76mY046.png

[root@centos7 /testdir]#find /etc/issue |cpio -o >issue.cpio1 block
[root@centos7 /testdir]#lsissue.cpio
[root@centos7 /testdir]#cpio -tv <issue.cpio # 顯示預(yù)覽
-rw-r--r--   1 root     root           23 Dec  9  2015 /etc/issue1 block
[root@centos7 /testdir]#

cpio在打包備份時(shí)用的是絕對(duì)路徑,且cpio無(wú)法直接讀取文件,它需要每個(gè)文件或目錄的完整路徑名才能讀取識(shí)別,故cpio命令一般與find配合使用。


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI