我爱编程Linux 课堂笔记

2018-05-25 课堂笔记

2018-05-28  本文已影响0人  chocolee911

目录

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

1. 常见压缩格式


2. gzip

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

2.1 特点

2.2 选项

2.3 用法

[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                                               #压缩后明显变小了
[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
[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 特点

3.2 选项

3.3 用法

[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
[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
[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]#
[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 特点

4.2 选项

4.3 用法

[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
[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
[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]#
[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 特点

5.2 选项

5.3 用法

[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
[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
[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:解包

6.3 用法

[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
[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
[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
[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/
上一篇 下一篇

猜你喜欢

热点阅读