zip
支持压缩目录
1.在/tmp/目录下创建目录(study_zip)及文件
root@yolks1 study_zip]# !tree
tree 1
1
└── 2
└── 3
└── test_zip.txt
2 directories, 1 file
2.yum 安装 zip命令
yum install -y zip
3.压缩/tmp/下的文件
[root@yolks1 tmp]# zip test_new_file.txt.zip test_new_file.txt
adding: test_new_file.txt (deflated 64%)
[root@yolks1 tmp]# ls -lh test_new_file.*
-rw-r--r-- 1 root root 27K 6月 21 23:33 test_new_file.txt
-rw-r--r-- 1 root root 9.7K 6月 25 22:38 test_new_file.txt.zip
4.压缩目录 -r选项
[root@yolks1 tmp]# zip -r study_zip.zip study_zip/
adding: study_zip/ (stored 0%)
adding: study_zip/1/ (stored 0%)
adding: study_zip/1/2/ (stored 0%)
adding: study_zip/1/2/3/ (stored 0%)
adding: study_zip/1/2/3/test_zip.txt (stored 0%)
adding: study_zip/123.txt (stored 0%)
5.解压缩
有可能提示命令不存在,则安装unzip
[root@yolks1 tmp]# unzip study_zip.zip
-bash: unzip: 未找到命令
[root@yolks1 tmp]# yum install -y unzip
因为zip压缩会保留源文件,所以解压会提示改名或者覆盖等选项,选择适合自己的选项
[root@yolks1 tmp]# unzip study_zip.zip
Archive: study_zip.zip
replace study_zip/1/2/3/test_zip.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: n
replace study_zip/123.txt? [y]es, [n]o, [A]ll, [N]one, [r]ename: y
extracting: study_zip/123.txt
这里需要注意的是,当你不指定解压目录时,unzip默认解压到当前目录下,在压缩的时候zip不会消掉原文件,所以在解压的时候原文件还在,这是系统就会提示你是否覆盖,y/是的覆盖;n/不覆盖;A/全部覆盖;N/取消这次操作;r/重命名文件。
解压到指定目录去 -d 选项
[root@yolks1 tmp]# unzip test_new_file.txt.zip -d /tmp/unzip/
Archive: test_new_file.txt.zip
inflating: /tmp/unzip/test_new_file.txt
zip压缩包无法直接预览但是可以查看文件列表
[root@yolks1 tmp]# unzip -l study_zip.zip
Archive: study_zip.zip
Length Date Time Name
--------- ---------- ----- ----
0 06-25-2018 22:32 study_zip/
0 06-25-2018 22:31 study_zip/1/
0 06-25-2018 22:32 study_zip/1/2/
0 06-25-2018 22:32 study_zip/1/2/3/
0 06-25-2018 22:32 study_zip/1/2/3/test_zip.txt
0 06-25-2018 22:32 study_zip/123.txt
--------- -------
0 6 files
tar打包
tar本身就是一个打包工具,可以把目录打包成一个文件,它把所有文件整合成一个大文件,方便移动和复制
压缩也是一种打包,压缩的原理是将文件中相同的信息用一个字符代替,致使文件体积变小达到压缩的目的,压缩对于文本类或数据类文件有较明显的作用。
打包就是将一些文件放在一起变成一个包,便于保存和传输,图片和视频数据因为不象文本一样,因此多个图片在压缩的时候没有明显效果,因此只能做打包,进行保存。
- tar
- -z:表示同时用gzip压缩
- j:表示同时用bzip2压缩
- J:表示同时用xz压缩
- x:表示解包或者解压缩
- t:表示查看tar包里的文件
- c:表示建立一个tar包或者压缩文件包
- v:表示可视化
- f:后面跟文件名,在多个参数的使用情况下请将-f放在最后一位,应为f后面必须跟上文件名,比如-xcvf 。
- --exclude 文件名:表示可以指定在打包一个目录时,里面的其中一些文件可以不被打包,指定的哪些文件,哪些文件将不被打包。
操作目录如下:
[root@yolks1 chapter6]# tree bzip2
bzip2
├── test_bzip2_two.txt
├── test_bzip2_two.txt.bz2
├── test_bzip2.txt
└── xz
└── test_xz.txt
1 directory, 4 files
1.tar打包目录 tar -c[表示建立一个tar包或者压缩文件包]v[表示可视化]f[后面跟文件名,在多个参数的使用情况下请将-f放在最后一位,应为f后面必须跟上文件名,比如-xcvf] tar包文件名 要打包的目录/
[root@yolks1 chapter6]# tar -cvf bzip2.tar bzip2/
bzip2/
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
bzip2/test_bzip2_two.txt
bzip2/xz/
bzip2/xz/test_xz.txt
[root@yolks1 chapter6]# ls -lh bzip2.tar
-rw-r--r-- 1 root root 7.2M 6月 25 23:04 bzip2.tar
2.tar解包 tar -x[表示解包或者解压缩]vf
[root@yolks1 chapter6]# ls -lh bzip2
总用量 4.9M
-rw-r--r-- 1 root root 2.4M 6月 22 21:44 test_bzip2_two.txt
-rw-r--r-- 1 root root 241K 6月 22 21:42 test_bzip2_two.txt.bz2
-rw-r--r-- 1 root root 2.4M 6月 22 21:31 test_bzip2.txt
drwxr-xr-x 2 root root 25 6月 22 21:57 xz
3.tar 同时打包目录及文件
[root@yolks1 chapter6]# tar -cvf bzip2.tar test_gzip.txt bzip2/
test_gzip.txt
bzip2/
bzip2/xz/
bzip2/xz/test_xz.txt
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
bzip2/test_bzip2_two.txt
4.查看包里的文件列表
[root@yolks1 chapter6]# tar -tf bzip2.tar
test_gzip.txt
bzip2/
bzip2/xz/
bzip2/xz/test_xz.txt
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
bzip2/test_bzip2_two.txt
5.过滤不需要打包的文件或目录的操作;
# 查看bzip目录结构
[root@yolks1 chapter6]# tree bzip2
bzip2
├── test_bzip2_two.txt
├── test_bzip2_two.txt.bz2
├── test_bzip2.txt
└── xz
└── test_xz.txt
1 directory, 4 files
# 不打包test_bzip2_two.txt文件
[root@yolks1 chapter6]# tar -cvf bzip2.tar --exclude test_bzip2_two.txt bzip2/
# //命令后跟自定义的tar打包的文件名,后在--exclude参数下跟需要过滤掉的文件或目录,最后跟原目录。
bzip2/
bzip2/xz/
bzip2/xz/test_xz.txt
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
打包并压缩
tar可以打包的同时以gzip、bzip2、xz这三种方式压缩文件,以下用gzip举例其余两个参照
tar打包时并以zip压缩
[root@yolks1 chapter6]# tar -czvf bzip2.tar.gz bzip2/
bzip2/
bzip2/xz/
bzip2/xz/test_xz.txt
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
bzip2/test_bzip2_two.txt
[root@yolks1 chapter6]# ls
bzip2 bzip2.tar.gz test_gzip_two.txt test_gzip.txt.gz
bzip2.tar --exclude test_gzip.txt
[root@yolks1 chapter6]# ls -lh bzip2.tar.gz
-rw-r--r-- 1 root root 2.1M 6月 25 23:21 bzip2.tar.gz
tar包解包并以zip解压缩
[root@yolks1 chapter6]# tar -zxvf bzip2.tar.gz
bzip2/
bzip2/xz/
bzip2/xz/test_xz.txt
bzip2/test_bzip2.txt
bzip2/test_bzip2_two.txt.bz2
bzip2/test_bzip2_two.txt
查看文件列表
tar -tf bzip2.tar.gz