pengpenglin
人,必须选择一种生活方式并有勇气坚持下去
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 262, comments - 221, trackbacks - 0
【Linux学习笔记】压缩和打包命令
【1.打包与压缩】
首先我们有必要区分一下“打包”和“压缩”这两个概念。在Windows平台用久了,很容易把这两个概念混在一起。Linux下的打包仅仅是把多个文件“捆绑”成一个文件(实际上可以理解为用一个文件夹来保存这些文件),但不降低这些文件总体的磁盘占用空间。而压缩则是通过诸如空白bit压缩等方式来减少这些文件所占用的磁盘空间。
【2.gzip和zcat】
gzip和zcat命令分别用来压缩文件和读取压缩文件中的内容。其语法格式如下:
[root@www
~
]# gzip [
-
cdtv#] 文件名
[root@www
~
]# zcat 文件名.gz
-c:将压缩的资料输出到屏幕上,可以将输出流重定向到其它地方
-d:解压缩的参数
-v:显示压缩比
-#:压缩等级,-1最快,-9最慢,预设-6
注意:使用gzip时默认会把原文件压缩成.gz文件,而原文件会被删除。如果要保持原文件的存在,则要使用参数-c和输出流重定向符>。
[root
@www
tmp]
#
gzip -cv man.config > man.config.gz
这里我们使用了输出流重定向,将压缩后的内容重定向到当前目录下的man.config.gz,而保留源文件不变。如果需要解压文件我们也可以选择用gzip而不需要用gunzip。
[root
@www
tmp]
#
gzip -d man.config.gz
我们也可以不解压而直接查看压缩文档的内容,方法就是使用zcat命令
[root
@www
tmp]
#
zcat man.config.gz
【3.tar命令】
tar命令用于对若干文件进行打包,具体用法参考如下(参见《鸟哥的Linux私房菜》)
[root
@www
~
]
#
tar [-j|-z] [
cv
] [-f 建立的檔名] filename
<==打包與壓縮
[root
@www
~
]
#
tar [-j|-z] [
tv
] [-f 建立的檔名] <==察看檔名
[root
@www
~
]
#
tar [-j|-z] [
xv
] [-f 建立的檔名] [-C 目錄] <==解壓縮
選項與參數:
-
c:
建立打包檔案
,可搭配
-
v 來察看過程中被打包的檔名(filename)
-
t:
察看打包檔案的內容含有哪些檔名
,重點在察看『檔名』就是了;
-x:
解打包或解壓縮的功能
,可以搭配
-
C (大寫) 在特定目錄解開
特別留意的是,
-
c
,
-
t
,
-x
不可同時出現在一串指令列中。
-
j:透過 bzip2 的支援進行壓縮
/
解壓縮:此時檔名最好為
*.
tar
.
bz2
-
z:透過 gzip 的支援進行壓縮
/
解壓縮:此時檔名最好為
*.
tar
.
gz
-
v:
在壓縮
/
解壓縮的過程中,將正在處理的檔名顯示出來
!
-
f filename:
-
f 後面要立刻接要被處理的檔名!建議
-
f 單獨寫一個選項囉!
-
C 目錄:這個選項用在解壓縮,若要在特定目錄解壓縮,可以使用這個選項。
注意:打包后的文件名需要我们自己加后缀名区分以利于区别,tar命令并不会帮我们加后缀名
①使用tar命令压缩文件并查看信息
[
root@localhost /
]
# tar -jcpv -f test.tar.bz2 test
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/
[
root@localhost /
]
# tar -zcpv -f test.tar.gz test
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/
[
root@localhost /
]
# ll test.tar.*
-rw-r--r--
1
root root
183
2月
19
10
:
04
test.tar.bz2
-rw-r--r--
1
root root
170
2月
19
10
:
05
test.tar.gz
[
root@localhost /
]
# tar -jtv -f test.tar.bz2
tar: Record size
=
8
blocks
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test
/
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test
/test1/
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test
/test1/test2/
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test
/test1/test2/test3/
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test
/test1/test2/test3/test4/
[
root@localhost /
]
# tar -zt -f test.tar.gz
test/
test/test1/
test/test1/test2/
test/test1/test2/test3/
test/test1/test2/test3/test4/
[
root@localhost /
]
#
这里我们需要特别注意的是当我们使用tar [-j|-z] cpv命令压缩文件时,文件绝对路径将变成现对于当前目录的路径(也就是根路径标示符/将被移除)
②搜寻压缩包内的指定文件并解压到指定目录
[
root@localhost /
]
# tar
-jtv
-f test.tar.bz2
| grep 'test4'
tar: Record size
=
16
blocks
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test/test1/test2/test3/test4/
[
root@localhost /
]
# tar
-jxv
-f test.tar.bz2
test/test1/test2/test3/test4
test/test1/test2/test3/test4/
在上面的例子中,我们使用tar -jtv 命令首先查看压缩包的内容,然后用管道命令|把命令的执行结果作为grep命令的搜索参数,查找文件中包含'test4'的文件。然后我们就可以在tar命令的末尾加上特定的文件名,表明我们只需要解压指定的文件。
我们也可以使用另一种方式来搜索文件
[
root@localhost /
]
# grep 'test4'
<(tar -jtvf test.tar.bz2)
tar: Record size
=
16
blocks
drwxr-xr-x root/root
0
2009
-
02
-
19
09
:
52
:
45
test/test1/test2/test3/test4/
③在解压缩的同时查找文件
同样的我们不单可以在查看的过程中查找,我们同样可以在解压的过程中查找(比较适合在解压后的文件一大堆,需要快速找到文件的情况)
[
root@localhost /
]
# tar
-jxv
-f test.tar.bz2
| grep 'test4'
test/test1/test2/test3/test4/
[
root@localhost /
]
# ls
bin etc lib misc proc selinux
test
test.tar.gz usr
boot home lost+found mnt root srv testdir tmp var
dev initrd media opt sbin sys test.tar.bz2 u01
④在解压时不包含某些文件或目录
[
root@localhost /
]
# ls
bin etc lib misc proc selinux testdir usr
boot home lost+found mnt root srv tmp var
dev initrd media opt sbin sys u01
[
root@localhost /
]
# tar -jcvf testdir.tar.bz2 testdir
--exclude t1.txt \
> --exclude testdir2
testdir/
testdir/t2.txt
testdir/t3.txt
[
root@localhost /
]
#
这里要特别注意的是:exclude前面是--,另外exclude每次只能带一个参数,如果需要剔除多个选项,要用多个--exclude组合
⑤备份在某个时间点之后更新的文件
[
root@localhost /
]
# ls
bin etc lib misc proc selinux testdir u01
boot home lost+found mnt root srv testdir.tar.bz2 usr
dev initrd media opt sbin sys tmp var
[
root@localhost /
]
# tar -jcpvf testdir_newer_than_090101.tar.bz2 \
>
--newer-mtime
=
'
2009
-
01
-
01
'
testdir
tar: Treating date `
2009
-
01
-
01
' as
2009
-
01
-
01
00
:
00
:
00
+
0
nanoseconds
testdir/
testdir/t1.txt
testdir/t2.txt
testdir/t3.txt
testdir/testdir2/
[
root@localhost /
]
# tar -jtvf testdir_newer_than_090101.tar.bz2
tar: Record size
=
8
blocks
drwxr-x--x paul/root
0
2009
-
02
-
19
14
:
51
:
49
testdir/
-rw-rw-r-- paul/paul
21
2009
-
02
-
19
14
:
45
:
51
testdir/t1.txt
-rw-r--r-- root/root
0
2009
-
02
-
19
11
:
34
:
21
testdir/t2.txt
-rw-r--r-- root/root
0
2009
-
02
-
19
11
:
34
:
24
testdir/t3.txt
drwxr-xr-x root/root
0
2009
-
02
-
19
11
:
34
:
31
testdir/testdir2/
[
root@localhost /
]
#
--newer-mtime这个参数让我们在备份时只备份那些mtime时间比指定时间还要新的文件。如果我们只是使用了--newer,那么将比较文件的mtime和ctime。
-------------------------------------------------------------
生活就像打牌,不是要抓一手好牌,而是要尽力打好一手烂牌。
posted on 2009-02-02 23:39
Paul Lin
阅读(642)
评论(0)
编辑
收藏
所属分类:
Unix / Linux
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理
相关文章:
【Linux学习笔记】压缩和打包命令
【Linux学习笔记】特殊权限
【Linux学习笔记】文件的寻找
【Linux学习笔记】文件和目录的默认权限与隐藏权限
【Linux学习笔记】文件操作命令
【Linux学习笔记】目录操作命令
【Linux学习笔记】分区与目录
【Linux学习笔记】文件类型与扩展名
【Linux学习笔记】文件权限学习要点
Unix基本概念及入门技术【四】:Solaris下cp命令的若干注意点
<
2009年2月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
2
3
4
5
6
7
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(21)
给我留言
查看公开留言
查看私人留言
随笔分类
J2EE 框架(9)
J2EE基础(4)
J2SE(43)
Java 工具(5)
Oracle Concept(4)
Oracle SQL/PLSQL(9)
Oracle 开发(13)
Oracle 管理(4)
Oracle 调优
Oracle 错误诊断
RoR(19)
UML(3)
Unix / Linux(13)
Web基础(19)
其它技术(7)
感悟(3)
杂项(7)
架构与性能(8)
模式与重构(19)
灌水(8)
电影与音乐(16)
走过的路(1)
软件过程与软件方法(3)
阳光户外(2)
项目管理(36)
随笔档案
2012年2月 (3)
2011年11月 (4)
2011年10月 (1)
2011年9月 (2)
2011年8月 (2)
2011年7月 (5)
2011年6月 (3)
2011年5月 (1)
2011年4月 (1)
2011年3月 (3)
2011年1月 (1)
2010年12月 (1)
2010年11月 (5)
2010年10月 (3)
2010年9月 (1)
2010年7月 (1)
2010年6月 (1)
2010年5月 (4)
2010年4月 (9)
2010年3月 (19)
2010年2月 (8)
2010年1月 (3)
2009年12月 (34)
2009年11月 (1)
2009年10月 (2)
2009年7月 (4)
2009年6月 (5)
2009年5月 (3)
2009年4月 (2)
2009年3月 (1)
2009年2月 (5)
2009年1月 (5)
2008年12月 (13)
2008年11月 (4)
2008年10月 (1)
2008年9月 (6)
2008年8月 (5)
2008年7月 (3)
2008年6月 (31)
2008年5月 (10)
2008年4月 (9)
2008年3月 (7)
2008年2月 (4)
2008年1月 (19)
BlogJava热点博客
BeanSoft
Jack.Wang
如坐春风
诗特林
郑晖
银河使者
阿密果
隔叶黄莺
好友博客
无羽苍鹰
搜索
最新评论
1. re: 【Java基础专题】编码与乱码(01)---编码基础[未登录]
666666666666666666666这几天正在做个类似工程编码出现错误
--李
2. re: 【Java基础专题】IO与文件读写---使用Apache commons IO简化文件读写
不错
--阿斯兰
3. re: 【Java基础专题】编码与乱码(03)----String的toCharArray()方法
多谢分享
--thx
4. re: 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换
评论内容较长,点击标题查看
--karl
5. re: 【Java基础专题】编码与乱码(01)---编码基础[未登录]
谢谢,帮了我大忙!
--小龙
阅读排行榜
1. Oracle中如何插入特殊字符:& 和 ' (多种解决方案)(48098)
2. getOutputStream() has already been called for this response的解决方法(43290)
3. 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换(36803)
4. 360安全卫士误删audiosrv.dll文件造成系统无声的解决方案(21192)
5. Java中读取字节流并按指定编码转换成字符串的方法(17286)
评论排行榜
1. 360安全卫士误删audiosrv.dll文件造成系统无声的解决方案(33)
2. 【原】Oracle开发专题之:时间运算(11)
3. 【Java基础专题】编码与乱码(05)---GBK与UTF-8之间的转换(11)
4. getOutputStream() has already been called for this response的解决方法(10)
5. 【原】Oracle开发专题之:分析函数(OVER)(8)