linux学习--文件打包压缩

文件压缩

compress

comprss是一个比较老的压缩命令
1 压缩
compress file 生成 file.z
2 解压缩
uncompress file.z

gzip

1 gzip压缩
执行gzip file
会生成file.gz,而原来的file文件会被删除
2 gzip解压缩
gunzip file.gz 或者
gzip -d file.gz

bzip2

1 压缩
bzip2 file 生产file.bz2
2 解压缩
bunzip2 file.bz2
bzip2 -d A.bz2

注意:bzip2的压缩率最高,gzip其次,compress最小

文件打包

tar 的语法

tar [主选项+辅选项] 文件或者目录
主选项

1
2
3
c 创建新的档案文件。
x 从档案文件中释放文件。
t 列出档案文件的内容

辅选项

1
2
3
4
5
6
-z 使用gzip来压缩或者解压
-j 使用bzip2来压缩或者解压缩
-v 解压缩过程中显示文件
-f 使用档名(在f之后要立即接档名,不要再加其他参数)
-p 使用原文件原来属性(属性不会依据使用者而变)
--exclude FILE 在压缩的过程中,不要将FILE打包

用法示例

1 打包压缩
tar -cvf /tmp/root.tar /root
把root目录下的所有文件打包到/tmp/root.rar文件中,不压缩
tar -zcvf /tmp/root.tar.gz /root
把root目录下的文件打包并以gzip来压缩到/tmp/root.rar.gz文件中
tar -jcvf /tmp/root.tar.bz2
把root目录下的文件打包并以bzip2压缩到/tmp/root.rar.bz2文件中
2 查看压缩包中的文件
tar -tvf /tmp/root.tar
tar -ztvf /tmp/root.tar.gz
tar -jtvf /tmp/root.tar.bz2
3 解包解压缩
tar -xvf /tmp/root.tar
tar -zxvf /tmp/root.tar.gz
tar -jxvf /tmp/root/tar/bz2
如果不加路径,解压出来的文件默认当前所处在的目录
如先cd /usr/local/src再执行tar -xvf /tmp/root.tar
解开的目录会在/usr/loal/src/root
指定解压的路径要加 -C 参数
tar -xvf /tmp/root.rar -C /home/test
将root.tar文件中的文件全部解压到/home/test目录中
4 只解压包内的某个文件
tar -xvf /tmp/root.tar root/test.pdf
只解压root.tar中的test.pdf文件到当前目录
5 打包时,将某个文件或者目录排除掉
tar --exclude /home/feng -xvf home.tar /home /etc
将/home 和/etc下的文件打包,但不包括/home/feng目录

Contents
  1. 1. 文件压缩
    1. 1.1. compress
    2. 1.2. gzip
    3. 1.3. bzip2
  2. 2. 文件打包
    1. 2.1. tar 的语法
    2. 2.2. 用法示例
,