目录
在linux下常用的压缩格式有:.tar、.tar.bz2、.tar.gz 不同的压缩格式使用的打包工具及压缩命令也不同。以下简要介绍常用的压缩命令。
一、tar打包工具
tar打包工具经常搭配使用的参数: -f:指定要提取的tar文件的文件名 -c:创建新的tar文件 -x:提取文件 -j:使用bzip2压缩格式 -z:使用gzip压缩格式 -v:打印出命令执行过程
以上是tar工具在使用时经常搭配的参数,需要注意的是,tar工具本身只是提供打包和解包的功能。
例如: tar -cvf hello.tar hello.txt //将文件hello.txt打包为hello.tar tar -xvf hello.tar //解包
tar工具在提供打包和解包的过程中还可以使用gzip/bzip2进行压缩。可根据压缩格式分为以下两种:
1、对.tar.bz2格式文件进行压缩和解压缩 tar -cvjf hello.tar.bz2 hello.txt //将文件hello.txt打包并压缩为hello.tar.bz2 tar -xvjf hello.tar.bz2 //解压缩
2、对.tar.gz格式文件进行压缩和解压 tar -cvzf hello.tar.gz hello.txt //将文件hello.txt打包并压缩为hello.tar.gz tar -xvzf hello.tar.gz //解压缩
二、gzip压缩工具
gzip工具负责压缩和解压缩.gz格式的压缩包。 1、gzip对文件的操作: gzip hello.txt //压缩文件hello.txt gzip -d hello.txt.gz //解压缩 2、gzip对文件夹的操作: gzip -r hello //对文件夹进行压缩(hello为文件夹) gzip -rd hello.gz //对文件夹进行解压缩
注意:gzip虽然可以对文件夹进行压缩,但是并不能提供打包的服务,只是对文件夹中的所有文件进行了单独的压缩。
三、bzip2压缩工具
和gzip工具类似,只是bzip2工具负责压缩和解压缩.bz2格式的压缩包。 常用命令如下: bzip2 -z hello.txt //压缩 bzip2 -d hello.txt.bz2 //解压缩
四、其他格式的压缩和解压缩
1、.rar格式 需要使用以下命令先安装rar工具: sudo apt-get install rar 常用的命令为: rar a hello.txt.rar hello.txt //压缩 rar x hello.txt.rar //解压缩
2、.zip格式 zip格式压缩使用的zip命令如下: zip -r hello.txt.zip hello.txt zip格式解压缩使用unzip命令如下: unzip hello.txt.zip