1. 程式人生 > >指令篇:文件與文件系統的壓縮與解壓與打包(歸檔)與解壓___gzip、zcat;bzip2、bzcat;zip; tar

指令篇:文件與文件系統的壓縮與解壓與打包(歸檔)與解壓___gzip、zcat;bzip2、bzcat;zip; tar

roo 壓縮命令 過時 bsp 演示 組合 localhost 打包 mov

一、文件與文件系統的壓縮:

  1、單文件的四種壓縮命令:

    ①、Compress  (過時了,不做過多說明)

    ②、gzip,zcat   (gzip:壓縮。zcat:查看) 

    ③、bzip2,bzcat (bzip:壓縮。bzcat:查看)

    ④、zip      (zip:壓縮。unzip:解壓)

  2、gzip,zcat 組合

    不保留源文件壓縮:gzip file  

    保留源文件壓縮:gzip -c file > file.gz

    查看壓縮比例並壓縮文件:gzip -v file

    查看壓縮文件裏面的內容:zcat file.gz

    解壓命令:gzip -d file.gz

技術分享
 1 [[email protected] opt]# gzip -c hosts > hosts.gz 
 2 [[email protected] opt]# ls
 3 hosts  hosts.gz
 4 [[email protected] opt]# rm -f hosts.gz 
 5 [[email protected] opt]# ls
 6 hosts
 7 [[email protected] opt]# gzip hosts 
 8 [[email protected]
/* */ opt]# ls 9 hosts.gz 10 [[email protected] opt]# zcat hosts.gz 11 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 12 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 13 [[email protected] opt]# gzip -d hosts.gz 14 [[email protected]
/* */ opt]# ls 15 hosts 16 [[email protected] opt]# 演示代碼

  3、bzip2,bzcat 組合。壓縮方法和上面一樣<註意後綴名>

    不保留源文件壓縮:bzip2 file  

    保留源文件壓縮:bzip2 -c file > file.bz2

    查看壓縮比例並壓縮文件:bzip2 -v file

    查看壓縮文件裏面的內容:bzcat file.bz2

    解壓命令:bzip2 -d file.bz2

  4、zip 壓縮文件。<不管壓縮與解壓,都會保留源文件>

    壓縮文件:zip file.zip file

    解壓文件:unzip file.zip

二、多文件和文件系統的打包(歸檔):命令: tar (註意:文件的的歸檔不是壓縮)

     參數:

        c: 創建一個歸檔文件

        v: 顯示創建歸檔的一個過程

        f: 指明歸檔之後文件的名字

        t: 查看歸檔文件

        x: 解壓歸檔文件(默認解壓在當前文件夾)

        C: 解壓歸檔文件(指定解壓到任意目錄)

   1、單文件歸檔:

      保留源文件歸檔:tar cvf aa.tar file

      不保留源文件歸檔:tar cvf aa.tar file --remove-files 或 tar cvf aa.tar file --remove-file

      查看一個歸檔文件:tar tvf aa.tar

      解壓一個歸檔文件:tar xvf aa.tar

技術分享
 1 [[email protected] opt]# ls
 2                         hosts
 3                         [[email protected] opt]# tar cvf aa.tar hosts 
 4                         hosts
 5                         [[email protected] opt]# ls
 6                         aa.tar  hosts
 7                         [[email protected] opt]# rm -f aa.tar 
 8                         [[email protected] opt]# tar cvf aa.tar hosts --remove-file
 9                         hosts
10                         [[email protected] opt]# ls
11                         aa.tar
12                         [[email protected] opt]# tar tvf aa.tar 
13                         -rw-r--r-- root/root       158 2017-05-21 16:13 hosts
14                         [[email protected] opt]# tar xvf aa.tar 
15                         hosts
16                         [[email protected] opt]# ls
17                         aa.tar  hosts
18                         [[email protected] opt]#
演示代碼

   

   2、多文件歸檔: 

      保留源文件歸檔:tar cvf yy.tar file1 file2 file3

      不保留源文件歸檔:tar cvf yy.tar file1 file2 file 3 --remove-file

      查看歸檔文件裏面的文件:tar tvf yy.tar

      解壓歸檔文件裏面的所有內容:tar xvf yy.tar

      解壓歸檔文件裏面的部分內容:tar xvf yy.tar file2  (解壓歸檔文件裏面的文件file2)

  註意:歸檔文件的時候可以跨文件夾歸檔,解壓歸檔文件的時候沒有試過跨文件夾歸檔

技術分享
 1                         [[email protected] opt]# ls
 2                         hosts  passwd  services
 3                         [[email protected] opt]# tar cvf yy.tar hosts passwd services 
 4                         hosts
 5                         passwd
 6                         services
 7                         [[email protected] opt]# ls
 8                         hosts  passwd  services  yy.tar
 9                         [[email protected] opt]# rm -f yy.tar 
10                         [[email protected] opt]# ls
11                         hosts  passwd  services
12                         [[email protected] opt]# tar cvf xx.tar hosts passwd services --remove-file
13                         hosts
14                         passwd
15                         services
16                         [[email protected] opt]# ls
17                         xx.tar
18                         [[email protected] opt]# tar tvf xx.tar 
19                         -rw-r--r-- root/root       158 2017-05-21 16:13 hosts
20                         -rw-r--r-- root/root      2235 2017-05-21 16:13 passwd
21                         -rw-r--r-- root/root    670293 2017-05-21 16:13 services
22                         [[email protected] opt]# tar xvf xx.tar 
23                         hosts
24                         passwd
25                         services
26                         [[email protected] opt]# ls
27                         hosts  passwd  services  xx.tar
28                         [[email protected] opt]# rm -f hosts passwd services 
29                         [[email protected] opt]# ls
30                         xx.tar
31                         [[email protected] opt]# tar xvf xx.tar hosts
32                         hosts
33                         [[email protected] opt]# ls
34                         hosts  xx.tar
35                         [[email protected] opt]# 
演示代碼

三、tar 命令進行文件的壓縮(tar本身是沒有壓縮功能的):------>該方法可以極大的減少壓縮文件的大小

      調用:gzip 或bzip2 來進行壓縮和解壓

  1、調用gzip 壓縮:tar jcvf mm.tar.bz2 hosts passwd services

        技術分享

      調用gzip解壓所有文件:tar jxvf mm.tar.bz2

        技術分享

      調用gzip解壓裏面的單個文件:tar jxvf mm.tar.bz2 hosts

        技術分享

      把 mm.tar.bz2 文件裏面的內容解壓到文件夾aa目錄下面:tar jxvf mm.tar.bz2 -C aa/

        技術分享

  2、調用gzip壓縮和解壓:(把上面的 "jxvf" --->zxvf)

      調用gzip 壓縮:tar jcvf mm.tar.bz2 hosts passwd services

      調用gzip解壓所有文件:tar jxvf mm.tar.bz2

      調用gzip解壓裏面的單個文件:tar jxvf mm.tar.bz2 hosts

      把 mm.tar.bz2 文件裏面的內容解壓到文件夾aa目錄下面:tar jxvf mm.tar.bz2 -C aa/

指令篇:文件與文件系統的壓縮與解壓與打包(歸檔)與解壓___gzip、zcat;bzip2、bzcat;zip; tar