Linux中文件压缩和归档类命令
文件压缩和归档类命令
1》gzip、gunzip使命令用来压缩、解压缩文件,文件压缩类命令还有bzip2,bunzip2等。
语法格式:
gzip/gunzip [参数选项] [文件]
注意的是:如果是压缩和解压是文件夹时必须要加入是“-r”参数,进行递归压缩和解压,否则会报错。
实例:
1.gzip压缩当前目录下的1.txt文件
gzip 1.txt
2.gzip压缩当前目录下的B文件夹
gzip -r B
[myvm168@localhost ~]$ ls
1.txt A Desktop Documents Downloads Music Pictures Public Templates test touch Videos
[myvm168@localhost ~]$ gzip 1.txt --对1.txt进行压缩
[myvm168@localhost ~]$ gzip A --报错的原因是文件夹压缩要必须加入参数‘-r’进行递归压缩
gzip: A is a directory -- ignored
[myvm168@localhost ~]$ gzip -r A --对A文件夹进行压缩
[myvm168@localhost ~]$ ls
1.txt.gz A Desktop Documents Downloads Music Pictures Public Templates test touch Videos
[myvm168@localhost ~]$ ll A
总用量 0
[myvm168@localhost ~]$
zip程序是用来压缩文件,而gunzip程序是用来还原gzip压缩的文件的,zcat程序用来显示gzip压缩的文件,同时压缩和解压文件时原始文件,会被删除
2》zip/unzip指令,zip一样用于压缩,unzip用于解压文件,这个在项目打包发布是很有用。
语法格式:
zip [参数选项] [文件名]
unzip [参数选项] [文件名]
参数问题:
-r: 用于递归压缩或解压文件夹
-d: 用于压缩或解压指定存放在那个目录下
例如:
1.将当前文件夹test下的所有文件或文件夹进行压缩成myTest.zip文件
zip -r myTest.zip test
2.将myTest.zip文件解压到A目录下
unzip -d A myTest.zip
[myvm168@localhost ~]$ zip -r myTest.zip test --进行对test文件夹进行压缩为myTest.zip
adding: test/ (stored 0%)
adding: test/etc/ (stored 0%)
adding: test/etc/profile (stored 0%)
adding: test/newfile.txt (stored 0%)
[myvm168@localhost ~]$ ls
1.txt.gz Desktop Downloads myTest.zip Public test Videos
A Documents Music Pictures Templates touch
[myvm168@localhost ~]$ unzip -d A myTest.zip --进行对myTest.zip压缩文件进行解压到A文件夹中
Archive: myTest.zip
creating: A/test/
creating: A/test/etc/
extracting: A/test/etc/profile
extracting: A/test/newfile.txt
[myvm168@localhost ~]$ cd A
[myvm168@localhost A]$ ls
test
[myvm168@localhost A]$
3》tar指令:tar指令是用来打包,最后打包后的文件是.tar.gz的文件
tar命令用来归档或解归档文件,在Linux系统中一般结合压缩命令一起使用,后缀为.tar.gz
语法格式:
tar [参数选项] 归档文件名 须归档的原文件或目录 //归档语法
tar [参数选项] 归档文件名 -C 目标目录 //解归档语法
例题:
1.压缩多个文件,将当前文件夹下的pig.txt和cat.txt压缩到pc.tar.gz
tar -czvf pc.tar.gz pig.txt cat.txt
2.将当前test文件夹,压缩成myTest.tar.gz
tar -czvf myTest.tar.gz test
3.将pc.tar.gz解压到当前目录
tar -xzvf pc.tar.gz
4.将myAA.tar.gz解压到/opt/tmp2目录下
tar -xzvf myAA.tar.gz -C /opt/tmp2
[myvm168@localhost ~]$ ls --查看当前目录
AA cat.txt Documents Music pig.txt Templates touch
AA.tar.gz Desktop Downloads Pictures Public test Videos
[myvm168@localhost ~]$ tar -czvf pc.tar.gz cat.txt pig.txt --进行对cat.txt和pig.txt压缩归档
cat.txt
pig.txt
[myvm168@localhost ~]$ ls --查看当前目录变化
AA cat.txt Documents Music Pictures Public test Videos
AA.tar.gz Desktop Downloads pc.tar.gz pig.txt Templates touch
[myvm168@localhost ~]$ tar -czvf myTest.tar.gz test --对test进行压缩归档
test/
test/etc/
test/etc/profile
test/newfile.txt
test/AA/
test/AA/1.txt
test/AA/2.txt
[myvm168@localhost ~]$ ls --查看当前目录的变化
AA cat.txt Documents Music pc.tar.gz pig.txt Templates touch
AA.tar.gz Desktop Downloads myTest.tar.gz Pictures Public test Videos
[myvm168@localhost ~]$ rm -rf cat.txt pig.txt --删除cat.txt和pig.txt是对比解压后的文件
[myvm168@localhost ~]$ ls --查看当前目录的变化
AA Desktop Downloads myTest.tar.gz Pictures Public test Videos
AA.tar.gz Documents Music pc.tar.gz Templates touch
[myvm168@localhost ~]$ tar -xzvf pc.tar.gz --对pc.tar.gz进行解压
cat.txt
pig.txt
[myvm168@localhost ~]$ ls --查看当前目录的变化
AA cat.txt Documents Music pc.tar.gz pig.txt Templates touch
AA.tar.gz Desktop Downloads myTest.tar.gz Pictures Public test Videos
[myvm168@localhost ~]$ mkdir -p /opt/tmp2
mkdir: 无法创建目录 “/opt/tmp2”: 权限不
[myvm168@localhost ~]$ mkdir -p opt/tmp2
[myvm168@localhost ~]$ tar -xzvf myTest.tar.gz -C opt/tmp2 --对myTest.tar.gz进行解压
test/
test/etc/
test/etc/profile
test/newfile.txt
test/AA/
test/AA/1.txt
test/AA/2.txt
[myvm168@localhost ~]$ ls --查看当前目录的变化
AA cat.txt Documents Music opt Pictures Public test Videos
AA.tar.gz Desktop Downloads myTest.tar.gz pc.tar.gz pig.txt Templates touch
[myvm168@localhost ~]$
tar命令常用参数及作用
参数 | 作用 |
---|---|
-c | 小写c,创建.tar格式的包文件 |
-C | 大写C,解包时指定目标目录 |
-f | 指定文档名 |
-r | 追加文件到“.tar”格式的文件结尾 |
-t | 列表查看包内的文件 |
-v | 输出详细信息 |
-x | 解开“.tar”格式的包文件 |
-z | 调用gzip程序进行压缩或解压缩 |
特别要注意的是,在参数的选择中c,r,x,t参数仅能存在一个,因为不可能能同时压缩与解压缩。同时“-f”参数选项要留意,在”f“参数之后要立即接着文档名,不要再加入参数。
例如:“tar -czfv tfile”是错误的写法。
“tar -czvf tfile”才是正确的写法。
练习题目:
1.对当前AA目录进行压缩归档,并查看当前目录观察变化
2.将归档文件解压到test目录
3.查看test目录
[myvm168@localhost ~]$ ls --查看当前目录
AA Desktop Documents Downloads Music Pictures Public Templates test touch Videos
[myvm168@localhost ~]$ tar -czvf AA.tar.gz AA --对AA目录进行压缩归档
AA/
AA/1.txt
AA/2.txt
[myvm168@localhost ~]$ ls --查看当前目录
AA AA.tar.gz Desktop Documents Downloads Music Pictures Public Templates test touch Videos
[myvm168@localhost ~]$ ls test --查看test目录
etc newfile.txt
[myvm168@localhost ~]$ tar -xzvf AA.tar.gz -C test --对存在的AA.tar.gz归档文件
AA/
AA/1.txt
AA/2.txt
[myvm168@localhost ~]$ ls test --查看test文件
AA etc newfile.txt
[myvm168@localhost ~]$