文件的压缩与打包

一、文件的压缩与打包

1.1常用解压命令:

ar -xvf file.tar //解压 tar包

tar -xzvf file.tar.gz //解压tar.gz

tar -xjvf file.tar.bz2   //解压 tar.bz2

tar -xZvf file.tar.Z   //解压tar.Z  compress

unrar e file.rar //解压rar

unzip file.zip //解压zip

1.2常用参数介绍:

-c: 建立压缩档案

-x:解压

-t:查看内容

-r:向压缩归档文件末尾追加文件

-u:更新原压缩包中的文件

这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。

 

-z:有gzip属性的

-j:有bz2属性的

-Z:有compress属性的

-v:显示所有过程

-O:将文件解开到标准输出

下面的参数-f是必须的

-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

二、常见案例

2.1把光盘挂载到mnt里面去

挂载
[root@localhost ~]# mount /dev/cdrom /mnt/ mount: block device /dev/cdrom is write-protected, mounting read-only [root@localhost ~]#
访问光盘 [root@localhost
~]# ls /mnt CentOS RELEASE-NOTES-cs RELEASE-NOTES-en_US RELEASE-NOTES-ja RELEASE-NOTES-ro EULA RELEASE-NOTES-cs.html RELEASE-NOTES-en_US.html RELEASE-NOTES-ja.html RELEASE-NOTES-ro.html GPL RELEASE-NOTES-de RELEASE-NOTES-es RELEASE-NOTES-nl repodata images RELEASE-NOTES-de.html RELEASE-NOTES-es.html RELEASE-NOTES-nl.html RPM-GPG-KEY-beta isolinux RELEASE-NOTES-en RELEASE-NOTES-fr RELEASE-NOTES-pt_BR RPM-GPG-KEY-CentOS-5 NOTES RELEASE-NOTES-en.html RELEASE-NOTES-fr.html RELEASE-NOTES-pt_BR.html TRANS.TBL [root@localhost ~]#

2.2compress 的使用

安装ncompress
[root@localhost ~]# yum install ncompress -y
 
复制文件
[root@localhost opt]# cp /etc/services .

压缩文件(压缩完没有源文件)
[root@localhost opt]# compress -v services
services:  -- replaced with services.Z Compression: 55.87%   (压缩比例)
[root@localhost opt]# ll
总计 168
-rw-r--r-- 1 root root 159749 03-13 21:24 services.Z
[root@localhost opt]# 

解压文件 [root@localhost opt]# ls services.Z [root@localhost opt]# uncompress services.Z [root@localhost opt]# ls services [root@localhost opt]#
压缩文件(保留源文件) [root@localhost opt]# compress
-c -v services > services.Z [root@localhost opt]# ls services services.z services.Z

2.3gzip的使用

压缩文件gzip
[root@localhost opt]# cp /etc/hosts .
[root@localhost opt]# ls
hosts
[root@localhost opt]# gzip hosts 
[root@localhost opt]# ls
hosts.gz
[root@localhost opt]# 

查看压缩文件内容 [root@localhost opt]# zcat hosts.gz # Do not remove the following line, or various programs # that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 [root@localhost opt]#
解压文件gzip
-d [root@localhost opt]# gzip -d hosts [root@localhost opt]# ls hosts [root@localhost opt]#
压缩并保留源文件 [root@localhost opt]# gzip
-c hosts > hosts.gz [root@localhost opt]# ls hosts hosts.gz [root@localhost opt]#
查看大小 [root@localhost opt]# ll
-lh 总计 16K -rw-r--r-- 1 root root 187 03-13 21:31 hosts -rw-r--r-- 1 root root 153 03-13 21:34 hosts.gz [root@localhost opt]#

2.4bzip2的使用

压缩文件bzip2
[root@localhost opt]# bzip2 hosts
[root@localhost opt]# ls
hosts.bz2  hosts.gz
[root@localhost opt]# 

解压 [root@localhost opt]# bzip2
-d hosts.bz2 [root@localhost opt]# ls hosts hosts.gz [root@localhost opt]#
查看文件大小 [root@localhost opt]# ll
-lh 总计 24K -rw-r--r-- 1 root root 187 03-13 21:31 hosts -rw-r--r-- 1 root root 166 03-13 21:38 hosts.bz2 -rw-r--r-- 1 root root 153 03-13 21:34 hosts.gz [root@localhost opt]#

2.5zip的使用

压缩文件zip
[root@localhost opt]# zip hosts.zip hosts
  adding: hosts (deflated 31%)
[root@localhost opt]# ls
hosts  hosts.zip
[root@localhost opt]# ll -lh
总计 16K
-rw-r--r-- 1 root root 187 03-13 21:47 hosts
-rw-r--r-- 1 root root 271 03-13 21:48 hosts.zip
[root@localhost opt]# 

解压 [root@localhost opt]# unzip hosts.zip
打包文件(保留源文件) [root@localhost opt]# tar cvf hosts.tar hosts hosts [root@localhost opt]# ls hosts hosts.tar hosts.zip [root@localhost opt]#
查看大小 [root@localhost opt]# ll
-lh 总计 32K -rw-r--r-- 1 root root 187 03-13 21:47 hosts -rw-r--r-- 1 root root 10K 03-13 21:53 hosts.tar -rw-r--r-- 1 root root 271 03-13 21:48 hosts.zip [root@localhost opt]#

2.6tar的使用

打包文件(不保留源文件)
[root@localhost opt]# tar -cvf hosts.tar hosts --remove-file
hosts
[root@localhost opt]# ls
hosts.tar  hosts.zip
[root@localhost opt]# 

解档 [root@localhost opt]# tar xvf hosts.tar hosts [root@localhost opt]# ls hosts hosts.tar hosts.zip [root@localhost opt]#
解压到指定目录aa [root@localhost opt]# tar
-xvf hosts.tar -C aa/ hosts [root@localhost opt]# ls aa hosts hosts.tar hosts.zip [root@localhost opt]# ls /aa ls: /aa: 没有那个文件或目录 [root@localhost opt]# ls aa hosts [root@localhost opt]# cd aa [root@localhost aa]# ls hosts [root@localhost aa]# ll
同时打包三个文件 [root@localhost opt]# cp
/etc/services /etc/passwd /etc/hosts . [root@localhost opt]# ls hosts passwd services [root@localhost opt]# tar cvf xx.tar * hosts passwd services [root@localhost opt]# ls hosts passwd services xx.tar [root@localhost opt]# tar tvf xx.tar -rw-r--r-- root/root 187 2019-03-13 22:08:35 hosts -rw-r--r-- root/root 2220 2019-03-13 22:08:35 passwd -rw-r--r-- root/root 362031 2019-03-13 22:08:35 services [root@localhost opt]#
解压所有文件 [root@localhost opt]# tar xvf xx.tar hosts passwd services [root@localhost opt]# ls hosts passwd services xx.tar [root@localhost opt]#
单独解压其中一个文件 host opt]# tar xvf xx.tar hosts hosts [root@localhost opt]# ls hosts xx.tar

猜你喜欢

转载自www.cnblogs.com/tanshouke/p/12190892.html