linux文件解-压缩

常用:

解压tar.gz包  使用命令:tar -zxvf  file.tar.gz   -z 指有gzip的属性  -x 解开一个压缩文件的参数  -v解压过程中显示文件  -f放最后接filename

解压tar包  使用命令:tar -xvf file.tar

查看tar包里打包了哪些文件: tar -tf file.tar   或tar -tvf file.tar

解压zip文件  使用命令:unzip file.zip       AIX下解压ZIP文件: jar -xvf filename.zip 

gunzip file.zip

解压rar文件    使用命令:unrar e file.rar  把file.rar中的所有文件解压出来

tar包是未压缩过的,而zip或gz是打包后再进行压缩的文件

打包与压缩

打包目录   使用命令 tar -cvf  dir1.tar dir1                -c 是comprise 压缩/打包

打包并压缩   使用命令 tar -zcvf  dir1.tar.gz dir1      -z是打包并以gzip压缩  

排除某个不想添加到压缩文件的目录: tar -zcvf  dir3.tar.gz dir3 --exclude=dir3/dirx/dirr  --exclude=dir3/dirx/diry----错误,看最后实例

zip压缩      使用命令  zip   -r  myfile.zip dirname   -r表示递归压缩目录下所有文件

gzip压缩     只压缩文件,不保留源文件,不压缩目录

把project_a文件夹下的文件打包成project.war

1.打包jar - cvf project.war /project_a

-c   创建war包

-v   显示过程信息

-f   指 定 JAR文件名,通常这个参数是必须的

-M  不产生所有项的清单 (MANIFEST〕文件,此参数会忽略 -m参数

-0   这个是阿拉伯数字 ,只打包不压缩的意思

2.解压war包:

jar -xvf project.war

[root@rusky SHARE]# cd testdir3
[root@rusky testdir3]# ls
fuckdir fuck.txt
[root@rusky testdir3]# gzip *
gzip: fuckdir is a directory -- ignored
[root@rusky testdir3]# gzip -r *   有目录,加r递归压缩目录中文件
[root@rusky testdir3]# ls
fuckdir fuck.txt.gz
如果只压缩单个文件,非目录,则:gzip  123.txt  压缩后源文件123.txt变成123.txt.gz   

bzip2解压缩: 

bzip2是一个压缩能力更强的压缩程序,.bz2结尾的文件就是bzip2压缩的结果。 与bzip2相对的解压程序是bunzip2。tar中使用-j这个参数来调用gzip。下面来举例
说明一下:-j表示有bz2属性 
# tar -cjf all.tar.bz2 *.jpg 
这条命令是将所有.jpg的文件打成一个tar包,并且将其用bzip2压缩,生成一个bzip2压缩过的包,包名为all.tar.bz2 
# tar -xjf all.tar.bz2 
这条命令是将上面产生的包解开。 

============

AIX解压:.tar.gz格式方式

gunzip testfile.tar.gz     得到:testfile.tar

tar -xvf testfile.tar   得到testfile

=========================

tar压缩目录时排除我们不需要的某个目录或文件:

[root@rusky home]# tree test/      //查看test目录结构
test/
├── test1
│   ├── file1
│   ├── file1.1
│   └── file1.2
├── test2
│   ├── file2-1
│   ├── test2-1
│   └── test2-2
└── test3

5 directories, 4 files
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  
test/
test/test1/
test/test1/file1
test/test1/file1.1
test/test1/file1.2
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
test/test3/
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=test1     //排除test目录下的test1目录。"="等号后面跟着的是要压缩目录下的某个具体目录名,而不是路径
test/
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
test/test3/
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=test1 --exclude=test3   //排除多个test2和test3目录
test/
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=file*   //排除以file开头的所有文件,包括字目录里的
test/
test/test1/
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test3/
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=/home/test/test1/  --exclude=/home/test/test2/   //exclude后面不能是路径,否则不生效
test/
test/test1/
test/test1/file1
test/test1/file1.1
test/test1/file1.2
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
test/test3/
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=/home/test/test1  --exclude=/home/test/test2   //同上,exclude后面跟路径不生效
test/
test/test1/
test/test1/file1
test/test1/file1.1
test/test1/file1.2
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
test/test3/
[root@rusky home]# tar -zcvf rusky1.tar.gz test/  --exclude=./test/test1/  --exclude=./test/test2  //同上,路径不生效,默认压缩全部
test/
test/test1/
test/test1/file1
test/test1/file1.1
test/test1/file1.2
test/test2/
test/test2/test2-1/
test/test2/test2-2/
test/test2/file2-1
test/test3/
[root@rusky home]# 

  

原文地址:https://www.cnblogs.com/rusking/p/3624251.html