歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux文件壓縮與歸檔

Linux文件壓縮與歸檔

日期:2017/2/28 13:43:51   编辑:Linux教程

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

compress

compress是一個古老的壓縮工具,其壓縮文件後綴為.Z。

-d: 解壓縮-c: 結果輸出至標准輸出,不刪除原文件
-v: 顯示詳情

使用示例

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

gzip

gzip壓縮後的文件後綴為.gz,如果壓縮的是tar備份文件,則擴展名為.tar.gz

gzip, gunzip, zcat - compress or expand files

-d: 解壓縮,相當於unzip
-c: 將壓縮或解壓縮的結果輸出至標准輸出
-#:1-9,指定壓縮比zcat: 不顯示解壓縮的前提下查看文本文件內容

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壓縮的文件的擴展名為.bz2

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

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壓縮後的文件擴展名為.xz

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

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

打包壓縮文件,經zip壓縮後會另外生成.zip的文件而不刪除原文件。

zip - package and compress (archive) files

-r: 遞歸處理,將指定目錄下的所有文件與子目錄一並處理
-q: 不顯示執行過程

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博客網址,保存退出,運行腳本,可以根據需要自行修改
url=http://yolynn.blog.51cto.com/

tar

tar命令可為文件或目錄創建檔案(備份文件),tar命令可將很多文件打包成一個文件,從而可結合壓縮工具實現歸檔並壓縮了。

使用語法:

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.

常用參數:

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

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

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

    -t: --list, 列出備份文件的內容

    -v: --verbose

tar用法小結:

(1) 創建歸檔

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) 結合壓縮工具實現:歸檔並壓縮

-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命令是通過重定向的方式將文件進行打包備份,還原恢復的工具,它可以解壓以.cpio或者.tar結尾的文件;換言之,cpio可以復制文件到歸檔包中,或者從歸檔包中復制文件。

使用語法:

cpio - copy files to and from archives

cpio[選項] > 文件名或者設備名
cpio[選項] < 文件名或者設備名

EXAMPLES
% ls | cpio -ov > directory.cpio   #必須要在當前工作目錄中執行ls,後面接絕對路徑會報錯

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

% cpio -iv < directory.cpio

% cpio -idv < tree.cpio

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

常用參數:

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

-i: --extract,Run in copy-in mode,解包,將打包文件解壓或將設備上的備份還原到系統

-t: 預覽,查看文件內容或者輸出到設備上的文件內容

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

-d: 解包生成目錄,在cpio還原時,自動的建立目錄

-c: 一種較新的存儲方式

使用示例

[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 # 顯示預覽
-rw-r--r--   1 root     root           23 Dec  9  2015 /etc/issue1 block
[root@centos7 /testdir]#

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

Copyright © Linux教程網 All Rights Reserved