Linux-解压缩

Linux-解压缩

压缩、解压缩命令

 //压缩格式
        gz,bz2,xz,zip,Z

gzip

压缩后的文件以gz结尾

压缩完成后会删除原文件

[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1
[root@localhost ~]# gzip 1
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 22 Nov 14 19:52 1.gz

-d 解压缩,解压完成后会删除原文件

[root@localhost ~]# gzip -d 1.gz
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

gunzip

解压完成后会删除原文件

[root@localhost ~]# gzip 1
[root@localhost ~]# gunzip 1.gz
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

bzip2

压缩后的文件以bz2结尾(需要安装)

压缩完成后会删除原文件

bzip2是一款比gzip有着更大压缩比的压缩工具,使用格式近似

[root@localhost ~]# mount /dev/cdrom /mnt
mount: /mnt: /dev/sr0 already mounted on /mnt.
[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/bzip2-1.0.6-26.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/bzip2-1.0.6-26.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:bzip2-1.0.6-26.el8               ################################# [100%]
[root@localhost ~]# which bzip2
/usr/bin/bzip2
[root@localhost ~]# bzip2 1
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 14 Nov 14 19:52 1.bz2

-d 解压缩,解压完成后会删除原文件

[root@localhost ~]# bzip2 -d 1.bz2
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

bunzip2

解压完成后会删除原文件

[root@localhost ~]# bzip2 1
[root@localhost ~]# bunzip2 1.bz2
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

!!!注意:文件后缀并不能代表文件格式!!!

[root@localhost ~]# file 1.bz2 
1.bz2: bzip2 compressed data, block size = 900k
[root@localhost ~]# mv 1.bz2 1.gz
[root@localhost ~]# file 1.gz
1.gz: bzip2 compressed data, block size = 900k			//后缀修改成.gz后还是bzip2格式
[root@localhost ~]# mv 1.gz 1
[root@localhost ~]# file 1
1: bzip2 compressed data, block size = 900k			//后缀去掉后还是bzip2格式

xz

压缩后的文件以xz结尾

压缩完成后会删除原文件

比bzip2有着更大压缩比的压缩工具,使用格式近似

[root@localhost ~]# xz 1
[root@localhost ~]# ll
total 4
-rw-r--r--. 1 root root 32 Nov 14 19:52 1.xz

-d 解压缩,解压完成后会删除原文件

[root@localhost ~]# xz -d 1.xz
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

unxz

解压完成后会删除原文件

[root@localhost ~]# xz 1
[root@localhost ~]# unxz 1.xz
[root@localhost ~]# ll
total 0
-rw-r--r--. 1 root root 0 Nov 14 19:52 1

tar

归档工具,只归档不压缩

-c 创建归档文件

-f 操作的归档文件

[root@localhost ~]# ls
1  10  2  3  4  5  6  7  8  9
[root@localhost ~]# tar -cf test.tar 1 2 3 4 5
[root@localhost ~]# ls
1  10  2  3  4  5  6  7  8  9  test.tar			//压缩不会删除原文件
[root@localhost ~]# file test.tar 
test.tar: POSIX tar archive (GNU)
[root@localhost ~]# mkdir a
[root@localhost ~]# cp test.tar a
[root@localhost ~]# cd a
[root@localhost a]# tar xf test.tar
[root@localhost a]# ls
1  2  3  4  5  test.tar 				//解压不会删除原文件

-x 还原归档

-f 操作的归档文件

[root@localhost a]# tar -xf test.tar 
[root@localhost a]# ls
1  2  3  4  5  test.tar

-v 显示归档过程

[root@localhost ~]# tar -cvf test.tar 1 2 3 4 5
1
2
3
4
5

-C 将展开的归档文件保存至指定目录下

[root@localhost ~]# tar -xf test.tar -C /opt/
[root@localhost ~]# ls /opt/
1  2  3  4  5

-tf 不展开归档,直接查看归档了哪些文件

[root@localhost ~]# tar -tf test.tar 
1
2
3
4
5
-zcf

归档并调用gzip压缩

-zxf

调用gzip解压缩并展开归档

[root@localhost ~]# tar -zcf test.tar.gz 1 2 3 4 5
[root@localhost ~]# file test.tar.gz 
test.tar.gz: gzip compressed data, last modified: Sat Nov 14 12:52:07 2020, from Unix, original size 10240
[root@localhost ~]# ll -h test.tar*
-rw-r--r--. 1 root root 10K Nov 14 20:45 test.tar
-rw-r--r--. 1 root root 156 Nov 14 20:54 test.tar.bz2
-jcf

归档并调用bzip2压缩

-jxf

调用bzip2解压缩并展开归档

[root@localhost ~]# tar -jcf test.tar.bz2 1 2 3 4 5
[root@localhost ~]# file test.tar.bz2 
test.tar.bz2: bzip2 compressed data, block size = 900k
[root@localhost ~]# ll -h test.tar*
-rw-r--r--. 1 root root 10K Nov 14 20:45 test.tar
-rw-r--r--. 1 root root 156 Nov 14 20:54 test.tar.bz2
-rw-r--r--. 1 root root 144 Nov 14 20:52 test.tar.gz
-Jcf

归档并调用xz压缩

-Jxf

调用xz解压缩并展开归档

[root@localhost ~]# tar -Jcf test.tar.xz 1 2 3 4 5
[root@localhost ~]# file test.tar.xz 
test.tar.xz: XZ compressed data
[root@localhost ~]# ll -h test.tar*
-rw-r--r--. 1 root root 10K Nov 14 20:45 test.tar
-rw-r--r--. 1 root root 156 Nov 14 20:54 test.tar.bz2
-rw-r--r--. 1 root root 144 Nov 14 20:52 test.tar.gz
-rw-r--r--. 1 root root 188 Nov 14 20:58 test.tar.xz

*压缩出来的文件大小应该是:gz>bz2>xz

之所以上面这个例子不是这个情况,是因为文件太小了没有再压缩的必要

[root@localhost ~]# mkdir linux
[root@localhost ~]# cd linux/
[root@localhost linux]# dd if=/dev/zero of=linux bs=1024M count=2
2+0 records in
2+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 53.0851 s, 40.5 MB/s
[root@localhost linux]# ll -h
total 2.0G
-rw-r--r--. 1 root root 2.0G Nov 14 21:06 linux
[root@localhost linux]# tar -zcf linux.tar.gz linux
[root@localhost linux]# tar -jcf linux.tar.bz2 linux
[root@localhost linux]# tar -Jcf linux.tar.xz linux
[root@localhost linux]# ll -h
total 2.1G
-rw-r--r--. 1 root root 2.0G Nov 14 21:06 linux
-rw-r--r--. 1 root root 1.6K Nov 14 21:08 linux.tar.bz2
-rw-r--r--. 1 root root 2.0M Nov 14 21:07 linux.tar.gz
-rw-r--r--. 1 root root 306K Nov 14 21:09 linux.tar.xz
//三种压缩出来对比可以明显的发现gz>bz2>xz

zip

既归档又压缩的工具,zip可以压缩目录(需要安装unzip和zip)

gz、bz2、xz都只能压缩文件,zip压缩后不会删除原文件

[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/unzip-6.0-43.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/unzip-6.0-43.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:unzip-6.0-43.el8                 ################################# [100%]
[root@localhost ~]# rpm -ivh /mnt/BaseOS/Packages/zip-3.0-23.el8.x86_64.rpm 
warning: /mnt/BaseOS/Packages/zip-3.0-23.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:zip-3.0-23.el8                   ################################# [100%]
[root@localhost ~]# zip test.zip 1 2 3 4 5
  adding: 1 (stored 0%)
  adding: 2 (stored 0%)
  adding: 3 (stored 0%)
  adding: 4 (stored 0%)
  adding: 5 (stored 0%)
[root@localhost ~]# ll test.zip 
-rw-r--r--. 1 root root 672 Nov 14 21:18 test.zip
unzip

解压

[root@localhost ~]# unzip test.zip 
Archive:  test.zip
 extracting: 1                       
 extracting: 2                       
 extracting: 3                       
 extracting: 4                       
 extracting: 5              

查看文本

cat

拼接文件内容并输出至标准输出(屏幕)

[root@localhost ~]# echo 'hello world' > abc
[root@localhost ~]# echo 'hello yqh' > def
[root@localhost ~]# cat abc
hello world
[root@localhost ~]# cat abc def
hello world
hello yqh

-n 显示行号

[root@localhost ~]# cat -n abc def
     1	hello world
     2	hello yqh

tac

连接文件并倒序打印内容至标准输出

[root@localhost ~]# tac abc def
hello world
hello yqh

四种查看方式

more    //全屏查看文本文件内容,只能从前往后,不能从后往前。
	//文件内容显示完后自动退出
less    //全屏查看文本文件内容,可从前往后亦可从后往前。
head    //从头部开始打印文件内容,默认打印10行
-n      //指定要打印的行数,可以是负数,就是看倒数几行
tail    //查看文本文件尾部内容
-n      //指定要打印的行数,可以是负数,就是看倒数几行

拓展:如何看只看第五行?
[root@localhost ~]# head -5 anaconda-ks.cfg  | tail -1
clearpart --none --initlabel
原文地址:https://www.cnblogs.com/yuqinghao/p/13993936.html