zip
用法:zip [选项] 压缩后文件名 需要压缩的文件或目录
常用选项:
-q 不显示压缩过程
-r 递归处理,将指定目录下的所有文件和子目录一并处理
-d 从压缩文件内删除指定的文件
-m 将文件压缩并加入压缩文件后,删除原始文件,即把文件移到压缩文件中
-P 为压缩文件设置密码(明文)
-e 为压缩文件设置密码(隐藏)
-D 压缩文件内不建立目录名称
-F 尝试修复已损坏的压缩文件
-o 以压缩文件内拥有最新更改时间的文件为准,将压缩文件的更改时间设成和该文件相同
-g 将文件压缩后附加在既有的压缩文件之后,而非另行建立新的压缩文件
-j 只保存文件名称及内容,而不存放任何目录名称
-u 更换较新的文件到压缩文件内
-z 替压缩文件加上注释
实例:
1、把一个文件file2和一个目录dir2压缩为test01.zip
[root@jacken ~]# zip -qr test01.zip file2 dir2
2、从压缩文件test01.zip中删除file2
[root@jacken ~]# zip -d test01.zip file2
3、向压缩文件test01.zip中添加file3(追加后file3会自动删除)
[root@jacken ~]# zip -m test01.zip file3
4、把一个文件file3压缩为file3.zip并设置密码为hi
[root@jacken ~]# zip -P hi file3.zip file3
[root@jacken ~]# zip -e file3.zip file3 //回车后输入隐藏密码
5、把目录dir1和file3压缩,名字为hi.zip并添加注释内容(内容为this is a test)
[root@jacken ~]# zip -zqr hi.zip dir1 file3
this is a test
. //以"."结束,回车
unzip
常用选项:
-l 显示压缩文件内所包含的文件
-v 显示压缩文件内所包含的文件(更详细)
-t 检查压缩文件是否正确
-z 仅显示压缩文件的备注文字
-d 指定文件解压缩后所要存储的目录
-x 指定不要处理.zip压缩文件中的哪些文件
-n 解压缩时不要覆盖原有的文件
-C 压缩文件中的文件名称区分大小写
-j 不处理压缩文件中原有的目录路径
-L 将压缩文件中的全部文件名改为小写
实例:
[root@jacken ~]# unzip -l test.zip
Archive: test.zip
this is a test file.
Length Date Time Name
--------- ---------- ----- ----
0 02-04-2015 03:13 hi/
7 02-04-2015 03:13 hi/456
0 02-04-2015 03:12 hi/hello/
10 02-04-2015 03:12 hi/hello/123
1702 02-04-2015 03:40 passwd
--------- -------
1719 5 files
[root@jacken ~]# unzip -v test.zip
Archive: test.zip
this is a test file.
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
0 Stored 0 0% 02-04-2015 03:13 00000000 hi/
7 Stored 7 0% 02-04-2015 03:13 f78ca403 hi/456
0 Stored 0 0% 02-04-2015 03:12 00000000 hi/hello/
10 Stored 10 0% 02-04-2015 03:12 5dbe6fff hi/hello/123
1702 Defl:N 685 60% 02-04-2015 03:40 343c303d passwd
-------- ------- --- -------
1719 702 59% 5 files
[root@jacken ~]# unzip -t test.zip
Archive: test.zip
this is a test file.
testing: hi/ OK
testing: hi/456 OK
testing: hi/hello/ OK
testing: hi/hello/123 OK
testing: passwd OK
No errors detected in compressed data of test.zip.
[root@jacken ~]# unzip -z test.zip
Archive: test.zip
this is a test file.
[root@jacken ~]#
[root@jacken ~]# unzip -q test.zip -d /tmp/
[root@jacken ~]# ls /tmp/
hi passwd
[root@jacken ~]# rm -rf /tmp/*
[root@jacken ~]# unzip -q test.zip -d /tmp/ -x passwd
[root@jacken ~]# ls /tmp/
hi
[root@jacken ~]#
[root@jacken ~]# echo "one_1" > one
[root@jacken ~]# echo "two_2" > two
[root@jacken ~]# ls
one two
[root@jacken ~]# cat one two
one_1
two_2
[root@jacken ~]# zip -q test.zip one two
[root@jacken ~]# ls
one test.zip two
[root@jacken ~]# echo "one_new" > one
[root@jacken ~]# echo "two_new" > two
[root@jacken ~]# unzip -q test.zip -x two
replace one? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
[root@jacken ~]# ls
one test.zip two
[root@jacken ~]# cat one two
one_1
two_new
[root@jacken ~]#
gzip/gunzip
注释:默认压缩后删除原文件
常用选项:
-r 递归处理,将指定目录下的所有文件及子目录一并处理
-t 测试压缩文件是否正确无误
-l 列出文件的相关信息
-c 保留原文件
-n:1-9,指定压缩比,默认是6
-d 解开压缩文件
-f 强行压缩文件,不理会文件名称或硬连接是否存在以及该文件是否为符号连接
-n 压缩文件时,不保存原来的文件名称及时间戳记
-N 压缩文件时,保存原来的文件名称及时间戳记。
实例:
[root@jacken ~]# ls -R
.:
123 inittab one passwd two
./123:
123 two
./123/two:
hi
[root@jacken ~]# gzip -r 123
[root@jacken ~]# ls -R
.:
123 inittab one passwd two
./123:
123.gz two
./123/two:
hi.gz
[root@jacken ~]# gzip one passwd
[root@jacken ~]# ls
123 inittab one.gz passwd.gz two
[root@jacken ~]# gzip -t one.gz
[root@jacken ~]# ls
123 inittab one.gz passwd.gz two
[root@jacken ~]# gzip -c inittab > inittab.gz
[root@jacken ~]# ls
123 inittab inittab.gz one.gz passwd.gz two
[root@jacken ~]# gzip -d one.gz passwd.gz
[root@jacken ~]# ls
123 inittab inittab.gz one passwd two
[root@jacken ~]#
[root@jacken ~]# ls
inittab passwd
[root@jacken ~]# gzip inittab passwd
[root@jacken ~]#
[root@jacken ~]# ls
inittab.gz passwd.gz
[root@jacken ~]#
[root@jacken ~]#
[root@jacken ~]#
[root@jacken ~]# ls
inittab.gz passwd.gz
[root@jacken ~]# gunzip -t inittab.gz passwd.gz
[root@jacken ~]# gunzip -l passwd.gz
compressed uncompressed ratio uncompressed_name
789 1925 60.7% passwd
[root@jacken ~]# gunzip passwd.gz
[root@jacken ~]# ls
inittab.gz passwd
[root@jacken ~]# gunzip -c inittab.gz > inittab
[root@jacken ~]# ls
inittab inittab.gz passwd
[root@jacken ~]#
bzip2/bzcat
常用选项:
-k 压缩或解压缩中,保留原文件
-t 测试压缩文件的完整性
-d 解压缩参数
-c 将压缩与解压缩的结果送到标准输出
-f 压缩或解压缩时,若输出文件与现有文件同名,强行覆盖
-z 强制执行压缩
-n 压缩比例
实例:
[root@jacken ~]# ls
file_1 file_2
[root@jacken ~]# cat file_1 file_2
file_one just one line
file_two just one line
[root@jacken ~]# bzip2 file_1
[root@jacken ~]# ls
file_1.bz2 file_2
[root@jacken ~]# bzcat file_1.bz2
file_one just one line
[root@jacken ~]# bzip2 -d file_1.bz2
[root@jacken ~]# ls
file_1 file_2
[root@jacken ~]# bzip2 -k file_1
[root@jacken ~]# ls
file_1 file_1.bz2 file_2
[root@jacken ~]# rm file_1
rm: remove regular file `file_1'? y
[root@jacken ~]# ls
file_1.bz2 file_2
[root@jacken ~]# bzip2 -k -d file_1.bz2
[root@jacken ~]# ls
file_1 file_1.bz2 file_2
[root@jacken ~]#
xz
常用选项:
-k 不删除原文件
-d 解压缩
-n 压缩比例(1-9,默认是6)
实例:
[root@jacken ~]# ls
file_1 file_2
[root@jacken ~]# xz file_1
[root@jacken ~]# ls
file_1.xz file_2
[root@jacken ~]# ls
file_1 file_2
[root@jacken ~]# xz file_1
[root@jacken ~]# xz -k file_2
[root@jacken ~]# ls
file_1.xz file_2 file_2.xz
[root@jacken ~]# rm -rf file_2
[root@jacken ~]# ls
file_1.xz file_2.xz
[root@jacken ~]# xz -d file_1.xz
[root@jacken ~]# xz -k -d file_2.xz
[root@jacken ~]# ls
file_1 file_2 file_2.xz
[root@jacken ~]#
tar
常用选项:
-c 建立压缩档案
-f 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名
-t 查看内容
-z 调用gzip属性
-j 调用bzip2属性
-x 解压
-r 向压缩归档文件末尾追加文件
-u 更新原压缩包中的文件
--exclude FILE:在压缩的过程中,不要将 FILE 打包!
实例:
tar -cf all.tar *.jpg 将所有.jpg的文件打成一个名为all.tar的包
tar -rf all.tar *.gif 将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。
tar -uf all.tar logo.gif 更新原来tar包all.tar中logo.gif文件,-u是表示更新文件的意思。
tar -tf all.tar 列出all.tar包中所有文件,-t是列出文件的意思
tar -xf all.tar 解出all.tar包中所有文件,-x是解开的意思压缩
tar -czf jpg.tar.gz *.jpg 将目录里所有jpg文件打包成jpg.tar后,并且将其用gzip压缩,生成一个gzip压缩过的包,命名为jpg.tar.gz
tar -cjf jpg.tar.bz2 *.jpg 将目录里所有jpg文件打包成jpg.tar后,并且将其用bzip2压缩,生成一个bzip2压缩过的包,命名为jpg.tar.bz2
tar �cZf jpg.tar.Z *.jpg 将目录里所有jpg文件打包成jpg.tar 且将其用compress压缩,生成一个umcompress压缩过的包,命名为jpg.tar.Z
rar jpg.rar *.jpg //rar格式的压缩,需要先下载rar for linux ,unrar e file.rar //解压rar