学习笔记(九)——压缩和打包
2015-07-21 20:47
267 查看
1.压缩与解压缩:压缩与解压缩常用的方式有gz, bz2, xz, zip,下面分别介绍一下这几种方式:(1)gzip:该命令可以将文件压缩为 gz 格式:[root@localhost~]# gzip file.log压缩后,当前目录下的 file.log文件会消失,压缩后的文件名为file.log.gz 。压缩时可以手动指定压缩级别,压缩级别从 1 到 9,数字越大压缩比例越高,默认的压缩级别为 6 ,一般使用默认压缩级别即可,使用如下方式指定压缩级别: [root@localhost~]# gzip -1 file.log使用 -d 参数可以将 gz 格式的压缩文件解压缩:[root@localhost~]# gzip -d file.log.gz也可以使用 gunzip命令解压缩,效果相同:[root@localhost~]# gunzip file.log.gz解压缩后,当前目录下的 file.log.gz 文件会消失,解压缩后的文件名为 file.log 。如果想指定解压缩后文件的存放路径和文件名,需要使用 -c 参数,结合输出重定向来完成:[root@localhost~]# gzip -d file.log.gz -c > /tmp/newfile.log通过这种方式解压缩,不仅可以指定解压缩后文件的存放路径和解压缩后的文件名,同时不会影响file.log.gz 文件,即解压完成后原压缩文件依然存在,可以在一条命令中对多个文件进行压缩和解压缩:[root@localhost~]# gzip file1.log file2.log //压缩[root@localhost~]# gzip -d file1.log.gz file2.log.gz //压缩这里的压缩不是将两个文件压缩为一个文件,而是分别压缩,即压缩后为两个文件 file1.log.gz 和 file2.log.gz如果想查看一个 gz 压缩文件中的内容,使用zcat 命令:[root@localhost~]# zcat file.log.gz注:gzip 压缩并不能将目录中的所有文件压缩为一个,只能使用 -r 参数对目录中的文件分别压缩:[root@localhost~]# gzip -r newfolder/这样在newfolder/ 目录下的所有文件都会被单独压缩成 gz 格式。使用 -l 参数,可以查看 gz 压缩文件的压缩比例等情况:[root@localhost~]# gzip -l file.gzcompressed uncompressed ratio uncompressed_name 47 19 -10.5% file(2)bzip2:和 gzip 用法几乎相同,对应的解压缩命令为bunzip2 。区别是压缩的比例略有不同,相应的参数也少了,没有-l 、 -r 参数,但依然可以使用 -c 参数。如果想查看一个 bz2压缩文件中的内容,使用bzcat 命令:[root@localhost~]# bzcat file.log.bz2bzip2 有一个独有的参数 -k ,使用该参数也能够在压缩后保留原文件:[root@localhost~]# bzip2 -k file.log[root@localhost~]# ls[root@localhost~]# …… file.log …… file.log.bz2 ……(3)xz:和 gzip 用法几乎相同,没有对应的解压缩命令,解压缩时使用-d 参数即可。区别是压缩的比例略有不同,相应的参数也少了,没有 -r 、-c 参数,-l 参数基本不使用。(4)zip:[root@localhost~]# zip /tmp/learn/file.log.zip file.log相对于前三种压缩方式来说, zip 方式有些特殊。首先,在 minimal 安装时不会安装zip 和 unzip 命令,需要通过yum 命令安装:[root@localhost~]# yum install –y zip unzip其次,zip 命令可以用来压缩目录,需要使用–r 参数:[root@localhost~]# zip –r etc.zip /etc/再次,zip 命令可以直接指定压缩后文件的存放目录和名称,不必借助其他参数,且压缩 / 解压缩后原文件依然存在,不受影响(不必像 gzip 和 bzip2 命令一样,想保留原文件还需要使用 -c 结合输出重定向)。最后,zip 命令可以将多个文件压缩为一个文件:[root@localhost~]# zip twofile.zip file1 file2
2.打包与解包:
打包不像压缩一样会明显减小文件所占用的空间,打包的意义是方便文件管理和传输。使用 tar 命令进行打包与解包。将 file1file2 file3 三个文件打包为 file.tar 文件:[root@localhost~]# tar -cvf file.tar file1 file2 file3如果在打包时想排除一些文件和目录,需要使用 --exclude 参数:[root@localhost ~]# tar -cvf lucy.tar --exclude “.sh” --exclude“.i586” --exclude “init3.d” /etc注:此处 --exclude后直接跟要排除的文件和目录,不用使用等号形式,即 --exclude=”.sh” 。将 file.tar 文件解包,默认会解包到当前目录,如果想指定解包后文件的目录,使用-C 参数:[root@localhost~]# tar -xvf file.tar //解包到当前目录[root@localhost ~]#tar -xvf file.tar -C /tmp/ //目录必须存在,不会自动创建目录
查看打包文件中都包含哪些文件:
[root@localhost ~]# tar -tf test.tar
test1.txttest2.txt如果想查看打包文件中所包含文件的详细信息,需要多使用一个 -v 参数:[root@localhost ~]# tar –tvf test.tar-rw-r--r-- root/root 1982015-03-28 10:36 test1.txt-rw-r--r-- root/root 1660322015-03-28 09:08 test2.txt删除打包文件中的已有文件:[root@localhost~]# tar --delete test1.txt -f test.tar更新打包文件中的文件:[root@localhost~]# tar -uvf test.tar test1.txt执行上条命令后,无论此时 test.tar 文件中是否包含 test1.txt 文件,都会将其添加进 test.tar 文件中,如果已含有 test1.tar ,查看 test.tar:[root@localhost~]# tar -tf test.tartest1.txttest1.txttest2.txt不难看出,更新后打包文件中出现了两个 test1.tar ,但在解压时,新更新进去的文件会覆盖掉原有的同名文件。
3.打包与压缩结合使用:这是在实际应用中最常用的一种方式。可以与 tar 结合使用的压缩格式有 gz 、 bz2 、 xz ,差别不大,具体来看一下。2.1 tar 与 gz 结合:(1)打包 + 压缩:[root@localhost ~]# tar -zcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -zxvf test.tar.gz //可以使用 -C 参数2.2 tar 与 bz2 结合:(1)打包 + 压缩:[root@localhost ~]# tar -jcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -jxvf test.tar.gz //可以使用 -C 参数2.3 tar 与 xz 结合:(1)打包 + 压缩:[root@local
a8da
host ~]# tar -Jcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -Jxvf test.tar.gz //可以使用 -C 参数
打包和压缩的内容可以结合之前的另一篇博客——《权当开篇——Linux学习之路(二)》——一块学习,下面给出博客链接:http://xitongjiagoushi.blog.51cto.com/9975742/1620232
本文出自 “细桶假狗屎” 博客,请务必保留此出处http://xitongjiagoushi.blog.51cto.com/9975742/1627128
2.打包与解包:
打包不像压缩一样会明显减小文件所占用的空间,打包的意义是方便文件管理和传输。使用 tar 命令进行打包与解包。将 file1file2 file3 三个文件打包为 file.tar 文件:[root@localhost~]# tar -cvf file.tar file1 file2 file3如果在打包时想排除一些文件和目录,需要使用 --exclude 参数:[root@localhost ~]# tar -cvf lucy.tar --exclude “.sh” --exclude“.i586” --exclude “init3.d” /etc注:此处 --exclude后直接跟要排除的文件和目录,不用使用等号形式,即 --exclude=”.sh” 。将 file.tar 文件解包,默认会解包到当前目录,如果想指定解包后文件的目录,使用-C 参数:[root@localhost~]# tar -xvf file.tar //解包到当前目录[root@localhost ~]#tar -xvf file.tar -C /tmp/ //目录必须存在,不会自动创建目录
查看打包文件中都包含哪些文件:
[root@localhost ~]# tar -tf test.tar
test1.txttest2.txt如果想查看打包文件中所包含文件的详细信息,需要多使用一个 -v 参数:[root@localhost ~]# tar –tvf test.tar-rw-r--r-- root/root 1982015-03-28 10:36 test1.txt-rw-r--r-- root/root 1660322015-03-28 09:08 test2.txt删除打包文件中的已有文件:[root@localhost~]# tar --delete test1.txt -f test.tar更新打包文件中的文件:[root@localhost~]# tar -uvf test.tar test1.txt执行上条命令后,无论此时 test.tar 文件中是否包含 test1.txt 文件,都会将其添加进 test.tar 文件中,如果已含有 test1.tar ,查看 test.tar:[root@localhost~]# tar -tf test.tartest1.txttest1.txttest2.txt不难看出,更新后打包文件中出现了两个 test1.tar ,但在解压时,新更新进去的文件会覆盖掉原有的同名文件。
3.打包与压缩结合使用:这是在实际应用中最常用的一种方式。可以与 tar 结合使用的压缩格式有 gz 、 bz2 、 xz ,差别不大,具体来看一下。2.1 tar 与 gz 结合:(1)打包 + 压缩:[root@localhost ~]# tar -zcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -zxvf test.tar.gz //可以使用 -C 参数2.2 tar 与 bz2 结合:(1)打包 + 压缩:[root@localhost ~]# tar -jcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -jxvf test.tar.gz //可以使用 -C 参数2.3 tar 与 xz 结合:(1)打包 + 压缩:[root@local
a8da
host ~]# tar -Jcvf test.tar.gz test1.txt test2.txt(2)解包 + 解压缩:[root@localhost ~]# tar -Jxvf test.tar.gz //可以使用 -C 参数
打包和压缩的内容可以结合之前的另一篇博客——《权当开篇——Linux学习之路(二)》——一块学习,下面给出博客链接:http://xitongjiagoushi.blog.51cto.com/9975742/1620232
本文出自 “细桶假狗屎” 博客,请务必保留此出处http://xitongjiagoushi.blog.51cto.com/9975742/1627128
相关文章推荐
- 习题总结(二)——禁ctrl+alt+delete,禁普通用户登录,禁ping
- 多学一点(四)——在 Linux 下配置多个 Tomcat
- 多学一点(三)——find 命令详解
- 多学一点(二)——在 Linux 下挂载 Windows 共享目录、使用 scp 命令在两台 Linux 间传输数据
- 学习笔记(七)——Linux 磁盘管理
- 习题总结(一)——硬链接,locate,chmod,家目录
- 多学一点(一)——在 Linux 下安装 JDK 和 Tomcat
- 学习笔记(六)——用户,用户组,切换登录用户
- Linux日志文件总管logrotate
- 学习笔记(五)——chown,chmod,chattr,lsattr,suid,sgid,stick_bit,Linux搜索文件/命令,软链接,硬链接
- 学习笔记(四)——目录命令、rm 等常用命令、查看文本命令、文件属性
- 学习笔记(三)——ls命令、Linux目录结构、cd命令、PATH、快捷键
- 学习笔记(二)——单用户模式、救援模式、运行级别、启动流程、登录文件
- 学习笔记(一)——安装与远程连接Linux操作系统
- 权当开篇——Linux学习之路(三)
- 权当开篇——Linux学习之路(一)
- Mac安装Gradle
- java.util.ConcurrentModificationException 解决办法
- JsonModel&AFNetWorking
- Web前端开发基础 第四课(颜色值)