1.tar数组
tar命令格式 [-zjxcvfpP] filename tarbash
-z:表示同时用gzip压缩。插件
-j:表示同时用bzip2压缩。code
-J:表示同时用xz压缩。ip
-x:表示解包或者解压缩。ast
-t:表示查看tar包里的文件。class
-c:表示创建一个tar包或者压缩文件包。test
-v:表示可视化。可视化
-f:后面跟文件名(即-f filename,表示压缩后的文件名为filename,或者解压文件filename。多个参数组合的状况下,请把-f参数写到最后。)打包
-P:表示使用原文件的属性。(不经常使用)
-p:表示可使用绝对路径。(不经常使用)
--exclude filename:表示在打包或压缩时,不要将filename文件包括在内。(不经常使用)
[root@localhost ~]# cd test -bash: cd: test: 没有那个文件或目录 [root@localhost ~]# cd /tmp/8/test [root@localhost test]# xz -d 1.txt.xz xz: 1.txt.xz: 没有那个文件或目录 [root@localhost test]# ls 1.txt [root@localhost test]# xz -z 1.txt.xz xz: 1.txt.xz: 没有那个文件或目录 [root@localhost test]# xz -z 1.txt [root@localhost test]# ls 1.txt.xz [root@localhost test]# xz -d 1.txt.xz [root@localhost test]# mkdir test111 [root@localhost test]# touch test111/2.txt [root@localhost test]# echo "nihao" >!$ echo "nihao" >test111/2.txt [root@localhost test]# echo "nihao" >test111/2.txt [root@localhost test]# cp 1.txt test111/
[root@localhost test]# tree . . ├── 1.txt └── test111 ├── 1.txt └── 2.txt 1 directory, 3 files [root@localhost test]# tar -cvf test111.tar test111 test111/ test111/2.txt test111/1.txt [root@localhost test]# ls 1.txt test111 test111.tar
tar打包文件
[root@localhost test]# rm -f test111.tar [root@localhost test]# tar -cf test.tar test111 1.txt [root@localhost test]# ls 1.txt test111 test.tar
打包解包原文件不会删除
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt test.tar [root@localhost test]# tar -xvf test.tar test111/ test111/2.txt test111/1.txt 1.txt
--exclude选项:能够排除文件,也能够删除文件。
[root@localhost test]# tar -cvf test111.tar --exclude 1.txt test111 test111/ test111/2.txt
[root@localhost test]# mkdir test111/test222 [root@localhost test]# tar -cvf test111.tar --exclude test222 test111 test111/ test111/2.txt test111/1.txt
zip命令
zip命令压缩目录和文件,压缩目录,须要指定目录下的文件。
[root@localhost test]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 64%) [root@localhost test]# zip test111.zip test111/* adding: test111/1.txt (deflated 64%) adding: test111/2.txt (stored 0%) adding: test111/test222/ (stored 0%)
zip后面先跟目标文件名,即压缩后的自定义压缩包名,而后跟要压缩的文件或者目录。安装zip目录命令:yum install -y zip
zip -r:一并压缩二级目录下的文件。
[root@localhost test]# zip -r test111.zip test111/ updating: test111/1.txt (deflated 64%) updating: test111/2.txt (stored 0%) updating: test111/test222/ (stored 0%) adding: test111/ (stored 0%)
unzip:解压缩命令。安装unzip命令:yum install -y unzip。
[root@localhost test]# unzip 1.txt.zip Archive: 1.txt.zip replace 1.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y inflating: 1.txt [root@localhost test]# yum install -y unzip 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.163.com * updates: mirrors.163.com 软件包 unzip-6.0-19.el7.x86_64 已安装而且是最新版本 无须任何处理
打包并压缩
tar命令支持gzip压缩、bzip2压缩和xz压缩。
压缩成gzip格式文件
[root@localhost test]# tar -czvf test111.tar.gz test111 test111/ test111/2.txt test111/1.txt test111/test222/
使用-tf选项,能够查看包或者压缩包的文件列表。
[root@localhost test]# tar -tf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# tar -tf test.tar test111/ test111/2.txt test111/1.txt 1.txt
使用-zxvf选项,能够解压tar.gz格式的压缩包。
[root@localhost test]# rm -rf test111 [root@localhost test]# ls 1.txt 1.txt.zip test111.tar test111.tar.gz test111.zip test.tar [root@localhost test]# tar -zxvf test111.tar.gz test111/ test111/2.txt test111/1.txt test111/test222/ [root@localhost test]# ls 1.txt 1.txt.zip test111 test111.tar test111.tar.gz test111.zip test.tar
打包的同时使用bzip2压缩
使用-cjvf选项来压缩:
[root@localhost test]# tar -cjvf test111.tar.bz2 test111 test111/ test111/2.txt test111/1.txt test111/test222/
使用-tf选项来查看压缩包的文件列表:
[root@localhost test]# tar -tf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/
使用-jxvf t:
[root@localhost test]# tar -jxvf test111.tar.bz2 test111/ test111/2.txt test111/1.txt test111/test222/