Linux从青铜到王者-基础命令篇-文件的归档和压缩

279 阅读4分钟

这是我参与更文挑战的第10天, 活动详情查看:更文挑战

大家好,我是互联网老辛,今天我们来聊聊关于文件的归档和压缩命令:

工作中我们用到的文件的归档和压缩命令有很多,我们今天着重来介绍以下内容:

  • tar命令进行文件的归档和压缩
  • zip管理压缩文件

归档和压缩文件的好处:节约硬盘的资源 ,加快文件传输速率

我们先来看tar命令

1 . tar: 作用打包压缩文件

用法: tar 【参数】 file

参数:

参数作用
-ccreate创建文件
-x-extract [ˈekstrækt] 提取 解压还原文件
-v--verbose显示执行详细过程
-f--file指定备份文件
-t--list 列出压缩包中包括哪些文件,不解包,查看包中的内容
-C(大写)--directory 指定解压位置

举例:

1) 把当前的路径下的文件打包,命名为loacl.tar
[root@zmgaosh ~]# tar -cvf local.tar ./
[root@zmgaosh ~]# ls
2     a.txt  b.txt.patch  ***local.tar***  passwd1  zmedu.txt
a.sh  b.txt  file         passwd     test     zmeduv2.txt

解压缩:

[root@zmgaosh ~]# mkdir test1
[root@zmgaosh ~]# mv local.tar test1/
[root@zmgaosh ~]# cd test1
[root@zmgaosh test1]# tar xvf local.tar 

[root@zmgaosh test1]# ls
2     a.txt  b.txt.patch  file       passwd   test       zmeduv2.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  zmedu.txt
[root@zmgaosh test1]# 

2) 指定解压位置 -C
[root@zmgaosh ~]# ls
2     a.txt  b.txt.patch  file       passwd   test   zmedu.txt
a.sh  b.txt  etc.tar      local.tar  passwd1  test1  zmeduv2.txt
[root@zmgaosh ~]# tar xvf local.tar  -C /opt/    #解压到/opt下

[root@zmgaosh ~]# ls /opt/   #查看是否解压成功
2  a.sh  a.txt  b.txt  b.txt.patch  etc.tar  file  passwd  passwd1  test  test1  xinsz1  zmedu.txt  zmeduv2.txt
[root@zmgaosh ~]# 

3) 不解包查看tar包中的内容
[root@zmgaosh ~]# tar tvf local.tar 
dr-xr-x--- root/root         0 2020-06-20 19:57 ./
-rw------- root/root      5461 2020-06-19 20:55 ./.viminfo
-rw-r--r-- root/root        91 2020-06-19 20:17 ./passwd
-rwxrwxrwx root/root       157 2020-06-17 23:22 ./a.sh
-rw-r--r-- root/root       176 2013-12-29 10:26 ./.bashrc

2. tar 归档+压缩

语法: tar czvf newfile.tar.gz

常用参数:

-z, --gzip 以gzip方式压缩 扩展名: tar.gz -j : 以bz2方式压缩的 扩展名:tar.bz2 -J : 以xz 方式压缩 扩展名:tar.xz

举例:

1.创建tar.gz的包
[root@zmedu16 ~]# tar cvf etc.tar /etc 
[root@localhost test]# tar zcvf etc.tar.gz /etc  #归档,注意备份的名字后缀
[root@localhost test]# tar zxvf etc.tar.gz   #解压缩

2. 创建tar.bz2包
语法: #tar jcvf newfile.tar.bz2  SOURCE
[root@zmedu16 ~]#  tar -jcvf etc.tar.bz2 /etc   
[root@zmedu16 ~]#  tar -jxvf etc.tar.bz2 /etc    #解压缩
[root@zmedu16 ~]#  tar jxvf etc.tar.bz2 -C  /opt    #解压到opt目录下

3. 创建.tar.xz的包
[root@zmedu16 ~]#  tar -Jcvf etc.tar.xz /etc
[root@zmedu16 ~]#  tar -xvf etc.tar.xz    #tar.xz 这类包,解压缩
或:
[root@zmedu16 ~]#  tar -Jxvf etc.tar.xz  

三种压缩方式中, tar.gz tar.bz2是比较常用的 tar.xz 压缩比例最高,压缩时间最长,压缩完文件最小

3. zip 管理压缩文件

zip是压缩程序,unzip是解压程序

安装zip [root@zmgaosh zip]# yum install zip

压缩:

[root@zmgaosh zip]# zip passwd.zip /etc/passwd
  adding: etc/passwd (deflated 61%)
[root@zmgaosh zip]# ls
passwd.zip

解压缩:

[root@zmgaosh zip]# unzip passwd.zip 
Archive:  passwd.zip
  inflating: etc/passwd              
[root@zmgaosh zip]# ls
etc  passwd.zip

如果要指定目标解压目录可以加参数-d

[root@zmgaosh zip]# unzip passwd.zip  -d /opt/

小技巧

如何查看看压缩文件的内容

1. 使用vim直接查看压缩包的内容

[root@zmgaosh zip]# vim passwd.zip

image.png

可以看到,当我们使用vim打开一个压缩包,在压缩包里会出现压缩的命令比如这里是ZIP, 可以看到当时使用的压缩命令。

这里的压缩命令就是zipfile /root/zip/passwd.zip

同样的如果是其他压缩命令,我们也可以使用这个方法来去查看

2. 使用file文件查看
[root@zmgaosh zip]# file passwd.zip 
passwd.zip: Zip archive data, at least v2.0 to extract

关于file里,我们常用的参数:

-b  列出辨识结果时,不显示文件名称。

-c  详细显示指令执行过程,便于排错或分析程序执行的情形。

-f<名称文件>  指定名称文件,其内容有一个或多个文件名称时,让file依序辨识这些文件,格式为每列一个文件名称。

-L  直接显示符号连接所指向的文件的类别。

-m<魔法数字文件>  指定魔法数字文件。

-v  显示版本信息。

-z  尝试去解读压缩文件的内容。

[root@localhost ~]# file install.log
install.log: UTF-8 Unicode text

[root@localhost ~]# file -b install.log      <== 不显示文件名称
UTF-8 Unicode text

[root@localhost ~]# file -i install.log      <== 显示MIME类别。
install.log: text/plain; charset=utf-8

[root@localhost ~]# file -b -i install.log
text/plain; charset=utf-8

显示符合链接的文件类型,也可以使用file

`[root@localhost ~]# ls -l /var/mail lrwxrwxrwx 1 root root 10 08-13 00:11 /var/mail -> spool/mail

[root@localhost ~]# file /var/mail /var/mail: symbolic link to `spool/mail'

[root@localhost ~]# file -L /var/mail /var/mail: directory

[root@localhost ~]# file /var/spool/mail /var/spool/mail: directory

[root@localhost ~]# file -L /var/spool/mail /var/spool/mail: directory`

总结

Linux下压缩解压命令还有很多,但最常见的还是tar命令和zip命令

比如:

1) ,gz的包:

解压:gunzip filename.gz

2).bz2 的包

bzip2 -d filename.bz2

3).tar.bz2的包

tar jxvf filename.tar.bz2

4).bz的包

bzip2 filename.bz

5).Z的包

uncompress filename.Z

6).rar的包

rar x filename.rar

但在公司中,我们用的比较多的,是unzip和tar,这两个命令一定要精通。今天是我参加更文的第十天,我们明天见。