2018-05-25 课堂笔记

作者: chocolee911 | 来源:发表于2018-05-28 11:53 被阅读0次

    目录

    1. 常见压缩格式
    2. gzip
    3. bzip2
    4. xz
    5. zip
    6. tar

    1. 常见压缩格式

    • Windows:.rar.zip.7z

    • Linux:.zip.gz.bz2.xz.tar.gz.tar.bz2.tar.xz


    2. gzip

    Linux 上最经典也是最常见的压缩软件,但是已经有些年头了,算法、压缩率等已不及 bzip2、xz、zip 等

    2.1 特点

    • 压缩比与其他后起之秀相比还是比较差的
    • 还是目前见到的使用最多的压缩工具
    • 无法压缩目录
    • 默认情况下,压缩或解压缩完成后,都会将原文件删除
    • 查看压缩文件只能使用 zcat 命令
    • 默认压缩比为 6

    2.2 选项

    • -d:解压
    • -#:指定压缩比(1~9,默认为6)
    • -c:重定向至屏幕(常用于保留原文件)
    • -v:显示压缩过程
    • -t:显示压缩比

    2.3 用法

    • 压缩
      gzip <file>
    [root@localhost tmp]# find /etc/ -type f -name "*.conf" -exec cat {} >> 1.txt \;
    [root@localhost tmp]# ll
    总用量 132
    -rw-r--r--. 1 root root 131592 5月  27 17:15 1.txt
    [root@localhost tmp]# du -sh 1.txt
    132K    1.txt
    [root@localhost tmp]# gzip 1.txt ; ll
    总用量 36
    -rw-r--r--. 1 root root 36836 5月  27 17:15 1.txt.gz        # 原文件被删除了,自动命名为相同文件名加.gz
    [root@localhost tmp]# du -sh 1.txt.gz
    36K 1.txt.gz                                               #压缩后明显变小了
    
    • 解压缩
      gzip -d <file.gz>gunzip <file.gz>
    [root@localhost tmp]# gzip -d 1.txt.gz ; ll
    总用量 132
    -rw-r--r--. 1 root root 131592 5月  27 17:15 1.txt
    [root@localhost tmp]# gunzip 1.txt.gz ; ll
    总用量 260
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    
    • 保留原文件或改名
      gzip -c <file> > <rename.gz>
    [root@localhost tmp]# gzip -c 1.txt > 100.gz ; ll         # 压缩时保留原文件并自行命名
    总用量 332
    -rw-r--r--. 1 root root  73397 5月  27 17:25 100.gz
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    
    [root@localhost tmp]# gzip -d -c 100.gz > new.txt ; ll    #解压时保留原文件并自行命名
    总用量 592
    -rw-r--r--. 1 root root  73397 5月  27 17:25 100.gz
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root 263184 5月  27 17:27 new.txt
    

    3. bzip2

    除了压缩算法更优良外,其他基本与 gzip 相同

    3.1 特点

    • 无法压缩目录
    • 默认情况下,压缩或解压缩完成后,都会将原文件删除,但-k选项可保留原文件
    • 查看压缩文件只能使用 bzcat 命令
    • 默认压缩比为 9

    3.2 选项

    • -d:解压
    • -#:指定压缩比(1~9,默认为9)
    • -k:保留原文件
    • -c:重定向至屏幕(常用于保留原文件)
    • -v:显示压缩过程
    • -t:显示压缩比

    3.3 用法

    • 压缩
      bzip2 <file>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 1.txt ; ll
    总用量 44
    -rw-r--r--. 1 root root 43017 5月  27 17:20 1.txt.bz2
    [root@localhost tmp]# du -hs 1.txt.bz2
    44K 1.txt.bz2
    
    • 解压缩
      bzip2 -d <file.bz2>bunzip2 <file.bz2>
    [root@localhost tmp]# ls
    1.txt.bz2
    [root@localhost tmp]# bzip2 -d 1.txt.bz2
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# gzip 1.txt
    [root@localhost tmp]# ls
    1.txt.gz
    [root@localhost tmp]# bunzip2 1.txt.gz ;ll
    bunzip2: Can't guess original name for 1.txt.gz -- using 1.txt.gz.out
    bunzip2: 1.txt.gz is not a bzip2 file.
    总用量 72
    -rw-r--r--. 1 root root 73397 5月  27 17:20 1.txt.gz
    
    • 保留原文件
      bzip2 -k <file>bzip2 -d -k <file.bz2>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 -k 1.txt ; ll
    总用量 304
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt                # 并没有删掉原文件
    -rw-r--r--. 1 root root  43017 5月  27 17:20 1.txt.bz2
    
    ## 解压试试
    [root@localhost tmp]# ls
    1.txt.bz2
    [root@localhost tmp]# bzip2 -dk 1.txt.bz2 ; ls
    1.txt  1.txt.bz2
    [root@localhost tmp]#
    
    • 保留原文件且改变文件名
      bzip2 -c <file> > <rename.bz2>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# bzip2 -c 1.txt > 2.txt.bz2 ; ls
    1.txt  2.txt.bz2
    [root@localhost tmp]#
    

    4. xz

    压缩算法更先进,用法与gzipbzip2 相同

    4.1 特点

    • 无法压缩目录
    • 默认情况下,压缩或解压缩完成后,都会将原文件删除
    • 查看压缩文件只能使用 xzcat 命令

    4.2 选项

    • -d:解压
    • -#:指定压缩比(1~9,默认为6)
    • -k:保留原文件
    • -c:重定向至屏幕(常用于保留原文件)
    • -v:显示压缩过程

    4.3 用法

    • 压缩
      xz <file>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# du -sh 1.txt
    260K    1.txt
    [root@localhost tmp]# xz 1.txt ; ll
    总用量 36
    -rw-r--r--. 1 root root 33892 5月  27 17:20 1.txt.xz
    [root@localhost tmp]# du -sh 1.txt.xz
    36K 1.txt.xz
    
    • 解压缩
      xz -d <file.xz>unxz <file.xz>
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# xz -d 1.txt.xz ; ll
    总用量 260
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# unxz 1.txt.xz ; ll
    总用量 260
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    
    • 保留原文件
      xz -k <file>xz -d -k <file.xz>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# xz -k 1.txt ; ll
    总用量 296
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt              #并没有删掉原文件
    -rw-r--r--. 1 root root  33892 5月  27 17:20 1.txt.xz
    
    
    ## 解压试试
    [root@localhost tmp]# ls
    1.txt.xz
    [root@localhost tmp]# xz -dk 1.txt.xz ; ll
    总用量 296
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root  33892 5月  27 17:20 1.txt.xz
    [root@localhost tmp]#
    
    • 保留原文件且改变文件名
      xz -c <file> > <rename.xz>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# xz -c 1.txt > 2.txt.xz ; ll
    总用量 296
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root  33892 5月  27 18:37 2.txt.xz
    

    5. zip、unzip

    目前所知,唯一能压缩目录的纯压缩软件
    用法与gzipbzip2xz 不同

    5.1 特点

    • 能够压缩目录
    • 默认不删除原文件,这样可能导致解压时会覆盖原文件
    • 配套解压工具 unzip 需要单独安装
    • 无类似zcatbzcatxzcat查看文件内容的工具,仅有-l选项可查看压缩包中的文件列表

    5.2 选项

    • zip:
      -r:压缩目录
      -#:1-9,压缩比(默认为6)

    • unzip:
      -l:查看压缩包中文件列表

    5.3 用法

    • 压缩文件
      zip <rename.zip> <file>
    [root@localhost tmp]# ls
    1.txt
    [root@localhost tmp]# zip 1.txt.zip 1.txt ; ll
      adding: 1.txt (deflated 72%)
    总用量 332
    -rw-r--r--. 1 root root 263184 5月  27 17:20 1.txt
    -rw-r--r--. 1 root root  73533 5月  27 18:48 1.txt.zip
    
    • 压缩目录
      zip -r <rename.zip> <dir>
    [root@localhost tmp]# tree
    .
    └── test
        ├── 1
        └── 2
    
    1 directory, 2 files
    [root@localhost tmp]# zip -r test.zip test/
      adding: test/ (stored 0%)
      adding: test/1 (stored 0%)
      adding: test/2 (stored 0%)
    [root@localhost tmp]# ls
    test  test.zip
    [root@localhost tmp]# unzip -l test.zip
    Archive:  test.zip
      Length      Date    Time    Name
    ---------  ---------- -----   ----
            0  05-27-2018 18:52   test/
            0  05-27-2018 18:52   test/1
            0  05-27-2018 18:52   test/2
    ---------                     -------
            0                     3 files
    
    • 解压缩

    由于默认不删原文件,解压时将可能碰到需要覆盖原文件的情况
    unzip <file.zip>

    [root@localhost tmp]# ls
    test  test.zip
    [root@localhost tmp]# unzip test.zip
    Archive:  test.zip
    replace test/1? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
     extracting: test/1
    replace test/2? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
     extracting: test/2
    
    • 指定解压的目标目录
      unzip <file.zip> -d <dst_dir>
    [root@localhost tmp]# ls
    aaa  test  test.zip
    [root@localhost tmp]# unzip test.zip -d aaa/ ; tree aaa
    Archive:  test.zip
       creating: aaa/test/
     extracting: aaa/test/1
     extracting: aaa/test/2
    aaa
    └── test
        ├── 1
        └── 2
    
    1 directory, 2 files
    

    6. tar

    最常见的打包工具,并且能获得 gzip、bzip2、xz 的支持,打包后进一步降低使用空间

    6.1 特点

    • 可以打包并压缩目录
    • 可获得压缩工具的支持
    • 命令分打包、查看、解包三种

    6.2 选项

    *-c: 创建包
    *-t:查看包
    *-x:解包

    • -C:解包至指定位置
      *-v:显示详细过程
      *-f:后接 tar 包的名字
      *-z:获取 gzip 的支持
      *-j:获取 bzip2 的支持
      *-J:获取 xz 的支持
      *--exclude=:打包时,排除个别文件或目录

    6.3 用法

    • 打包
      tar -cvf <file.tar> <file1> <file2> <dir1>
    [root@localhost tmp]# tar -cvf www.tar aaa/ test/ test.zip
    aaa/
    aaa/test/
    aaa/test/1
    aaa/test/2
    test/
    test/1
    test/2
    test.zip
    
    • 打包且压缩
      tar -vzvf <file.tar.gz> <file1> <dir1>
    [root@localhost tmp]# tar -czvf etc.tar.gz /etc/
    tar: 从成员名中删除开头的“/”
    /etc/
    /etc/fstab
    /etc/crypttab
    /etc/mtab
    .
    .
    .
    [root@localhost tmp]# ls
    etc.tar.gz
    
    • 打包,耽排除个别文件
    [root@localhost tmp]# ls
    1  100  2  etc.tar.bz2
    [root@localhost tmp]# tar -czvf /tmp.tar.bz2  /tmp/ --exclude=1
    tar: 从成员名中删除开头的“/”
    /tmp/
    /tmp/.X11-unix/
    /tmp/.ICE-unix/
    /tmp/.XIM-unix/
    /tmp/.font-unix/
    /tmp/.Test-unix/
    /tmp/etc.tar.bz2
    /tmp/2
    /tmp/100
    [root@localhost tmp]# tar -tzf /tmp.tar.bz2
    tmp/
    tmp/.X11-unix/
    tmp/.ICE-unix/
    tmp/.XIM-unix/
    tmp/.font-unix/
    tmp/.Test-unix/
    tmp/etc.tar.bz2
    tmp/2
    tmp/100
    
    • 解包
      tar -xzvf <file.tar.gz>
    [root@localhost tmp]# ls
    etc.tar.gz
    [root@localhost tmp]# tar -xzf etc.tar.gz
    [root@localhost tmp]# ll
    总用量 6544
    drwxr-xr-x. 74 root root    4096 5月  27 17:53 etc
    -rw-r--r--.  1 root root 6692238 5月  27 19:11 etc.tar.gz
    
    • 解包至指定位置
    [root@localhost tmp]# ll
    总用量 5704
    -rw-r--r--. 1 root root 5837206 5月  27 19:18 etc.tar.bz2
    [root@localhost tmp]# ll /root/
    总用量 0
    [root@localhost tmp]# tar -xjf etc.tar.bz2 -C /root/
    [root@localhost tmp]# ll /root/
    总用量 8
    drwxr-xr-x. 74 root root 4096 5月  27 17:53 etc
    
    • 查看包内容
      tar -tjf <file.tar.bz2>
    [root@localhost tmp]# ls
    etc.tar.bz2
    [root@localhost tmp]# tar -tjf etc.tar.bz2
    etc/
    etc/fstab
    etc/crypttab
    etc/mtab
    etc/pki/
    etc/pki/rpm-gpg/
    

    相关文章

      网友评论

        本文标题:2018-05-25 课堂笔记

        本文链接:https://www.haomeiwen.com/subject/brabjftx.html