学习linux压缩命令压缩文档

首先想象一下,我们压缩的场景有哪些?

为什么要去压缩?
1、 文件比较大
2、 文件比较零散,想合成一个整体文件
3、 远程传递文件的时候
4、 比如U盘无法拷贝ISO这些比较的镜像文件时,那么也需要

在linux 里面也有类似WINDOWS一样的压缩软件?
Linux:tar zip gzip bzip

那么我们首先学习tar这个压缩命令,那么我们看一下选项:
C:代表什么意思?可以压缩的名字,同时也可以创建

选项t:代表是列出压缩文件里面详细内容

x:代表解开压缩文件里面的内容

z:这个指明我们具体压缩的某种格式,比如后缀是.gz

j:这个指明我们具体压缩的某种格式,比如后缀是.bz2

J:这个指明我们具体压缩的某种格式,比如后缀是.xz

那么我们做一个小实验:
[root@localhost ~]#
[root@localhost ~]# cd /home/lg
-bash: cd: /home/lg: No such file or directory
[root@localhost ~]# cd /home/
[root@localhost home]# ls
[root@localhost home]#
[root@localhost home]# mkdir lgb
[root@localhost home]# cd lgb
[root@localhost lgb]#
[root@localhost lgb]#
[root@localhost lgb]# touch test1.txt
[root@localhost lgb]#
[root@localhost lgb]#
[root@localhost lgb]# echo “123456” > test1.txt
[root@localhost lgb]#
[root@localhost lgb]#
[root@localhost lgb]# cd …
[root@localhost home]# ls
lgb
[root@localhost home]# ls
lgb
[root@localhost home]# tar cvf lgb.tar lgb/
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar
[root@localhost home]#

[root@localhost home]# tar zcvf lgb.tar.gz lgb/
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.gz
[root@localhost home]# ls -al
total 20
drwxr-xr-x. 3 root root 47 Jul 22 09:42 .
dr-xr-xr-x. 17 root root 4096 Jul 13 09:55 …
drwxr-xr-x. 2 root root 22 Jul 22 09:36 lgb
-rw-r–r--. 1 root root 10240 Jul 22 09:37 lgb.tar
-rw-r–r--. 1 root root 157 Jul 22 09:42 lgb.tar.gz

[root@localhost home]# tar jcvf lgb.tar.bz2 lgb/
tar (child): lgb/
bzip2: Cannot execlgb/test1.txt
: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@localhost home]# gz
gzexe gzip
[root@localhost home]# gz
gzexe gzip
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#
[root@localhost home]# yum install -y bzip2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package bzip2.x86_64 0:1.0.6-13.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size

Installing:
bzip2 x86_64 1.0.6-13.el7 local_repo 52 k

Transaction Summary

Install 1 Package

Total download size: 52 k
Installed size: 82 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : bzip2-1.0.6-13.el7.x86_64 1/1
Verifying : bzip2-1.0.6-13.el7.x86_64 1/1

Installed:
bzip2.x86_64 0:1.0.6-13.el7

Complete!
[root@localhost home]#
[root@localhost home]#
[root@localhost home]#

[root@localhost home]# tar jcvf lgb.tar.bz2 lgb/
lgb/
lgb/test1.txt
[root@localhost home]#
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz
[root@localhost home]# ll
total 20
drwxr-xr-x. 2 root root 22 Jul 22 09:36 lgb
-rw-r–r--. 1 root root 10240 Jul 22 09:37 lgb.tar
-rw-r–r--. 1 root root 153 Jul 22 09:47 lgb.tar.bz2
-rw-r–r--. 1 root root 157 Jul 22 09:42 lgb.tar.gz
[root@localhost home]#

[root@localhost home]# tar Jcvf lgb.tar.xz lgb/
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz
[root@localhost home]# ll
total 24
drwxr-xr-x. 2 root root 22 Jul 22 09:36 lgb
-rw-r–r--. 1 root root 10240 Jul 22 09:37 lgb.tar
-rw-r–r--. 1 root root 153 Jul 22 09:48 lgb.tar.bz2
-rw-r–r--. 1 root root 157 Jul 22 09:42 lgb.tar.gz
-rw-r–r--. 1 root root 204 Jul 22 09:48 lgb.tar.xz

那么学会了压缩,还知道怎么去解开我们的压缩文件,但是不同的压缩格式,决定了你用什么解压缩选项?
比如我们tar格式:
[root@localhost home]# tar xvf lgb.tar
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz

比如我们gz格式:
[root@localhost home]# tar zxvf lgb.tar.gz
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz
[root@localhost home]# cd lgb
[root@localhost lgb]# ls
test1.txt
[root@localhost lgb]# cat test1.txt
123456
[root@localhost lgb]#
比如我们bz2格式:

[root@localhost home]# tar jxvf lgb.tar.bz2
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz

比如我们xz格式:
[root@localhost home]# tar Jxvf lgb.tar.xz
lgb/
lgb/test1.txt
[root@localhost home]# ls
lgb lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz
[root@localhost home]#

我们有时候也需要去掌握zip工具,说白了就是我们windows下压缩的文件,有时候需要放在linux里面解压缩:

那么我们需要把windows下的这个zip文件传到linux里面去或者说linux里面压缩的文件传到windows下解开。

[root@localhost home]# zip
-bash: zip: command not found
[root@localhost home]# yum install -y zip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package zip.x86_64 0:3.0-10.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size

Installing:
zip x86_64 3.0-10.el7 local_repo 260 k

Transaction Summary

Install 1 Package

Total download size: 260 k
Installed size: 796 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : zip-3.0-10.el7.x86_64 1/1
Verifying : zip-3.0-10.el7.x86_64 1/1

Installed:
zip.x86_64 0:3.0-10.el7

Complete!
[root@localhost home]# ls
lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz test2.txt
[root@localhost home]# zip -r test2.txt test2.zip
zip warning: missing end signature–probably not a zip file (did you
zip warning: remember to use binary mode when you transferred it?)
zip warning: (if you are trying to read a damaged archive try -F)

zip error: Zip file structure invalid (test2.txt)
[root@localhost home]# zip -r test2.zip test2.txt
adding: test2.txt (stored 0%)
[root@localhost home]# ls
lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz test2.txt test2.zip
[root@localhost home]#

[root@localhost home]# unzip test2.zip /root
-bash: unzip: command not found
[root@localhost home]#
[root@localhost home]#
[root@localhost home]# yum install -y unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package unzip.x86_64 0:6.0-15.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size

Installing:
unzip x86_64 6.0-15.el7 local_repo 166 k

Transaction Summary

Install 1 Package

Total download size: 166 k
Installed size: 357 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : unzip-6.0-15.el7.x86_64 1/1
Verifying : unzip-6.0-15.el7.x86_64 1/1

Installed:
unzip.x86_64 0:6.0-15.el7

Complete!
[root@localhost home]#

root@localhost home]# unzip test2.zip /root
Archive: test2.zip
caution: filename not matched: /root
[root@localhost home]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# unzip test2.zip -d /root
unzip: cannot find or open test2.zip, test2.zip.zip or test2.zip.ZIP.
[root@localhost ~]# cd /tmp/home/
[root@localhost home]# ls
lgb.tar lgb.tar.bz2 lgb.tar.gz lgb.tar.xz test2.txt test2.zip
[root@localhost home]#
[root@localhost home]# unzip test2.zip -d /root
Archive: test2.zip
extracting: /root/test2.txt
[root@localhost home]# cd /root
[root@localhost ~]# ls
anaconda-ks.cfg test2.txt
[root@localhost ~]# cat test2.txt
hello world
[root@localhost ~]#

Guess you like

Origin blog.csdn.net/qq_40168905/article/details/115909224