zip命令能够用来解压缩文件,或者对文件进行打包操做。zip是个使用普遍的压缩程序,文件经它压缩后会另外产生具备“.zip”扩展名的压缩文件。linux
注意: zip既能够压缩目录文件也能够压缩普通文件。数组
语法: zip [options] [filename.zip] [filename]工具
说明: zip后面先跟目标文件名,也就是自定义的压缩包名,而后跟源文件名。spa
options:code
-r:压缩目录文件时使用,表示级联压缩,连通目录内文件一同压缩ip
[root@3 d6z]# yum install -y zip
[root@3 d6z]# zip 1.txt.zip 1.txt adding: 1.txt (deflated 74%) [root@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai [root@3 d6z]# du -ah 1.3M ./1.txt 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 5.2M .
说明: 使用zip压缩文件时,源文件不会被删除。io
[root@3 d6z]# zip adai.zip adai adding: adai/ (stored 0%) 不加-r选项则只会压缩该文件夹,不压缩其内容 ############################### [root@3 d6z]# zip -r adai.zip adai updating: adai/ (stored 0%) adding: adai/1.txt (deflated 74%) adding: adai/2.txt (deflated 74%) [root@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai adai.zip [root@3 d6z]# du -ah 1.3M ./1.txt 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 656K ./adai.zip 5.9M .
eg1:可视化
[root@3 d6z]# 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@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai adai.zip
说明: 因zip压缩文件时不删除源文件,因此在相同目录进行解压缩时会提示:‘replace 1.txt? ’。扩展
eg2:打包
[root@3 d6z]# unzip 1.txt.zip -d /tmp/3.txt Archive: 1.txt.zip inflating: /tmp/3.txt/1.txt
说明: 解压时能够指定目录,可是不能指定其解压后的文件名。
[root@3 d6z]# unzip -l adai.zip Archive: adai.zip Length Date Time Name --------- ---------- ----- ---- 0 06-22-2017 23:29 adai/ 1277475 06-22-2017 23:29 adai/1.txt 1277475 06-22-2017 23:29 adai/2.txt --------- ------- 2554950 3 files
说明: zip压缩文件只能使用‘unzip -l’查看其文件目录,没法查看文件内容。
tar命令是Unix/Linux系统中备份文件的可靠方法,几乎能够工做于任何环境中,它的使用权限是全部用户。
语法: tar [options] [filename]
options:
-c:创建一个tar包或者压缩文件包
-f:指定目标文件名,若是多个参数组合使用时,把-f放在最后面
-z:同时用gzip压缩
-j:同时用bzip2压缩
-J:同时用xz压缩
-t:查看包里面的文件 -v:可视化
--exclude:后面跟文件名,表示打包除了该文件以外的内容
eg1: 打包目录文件
[root@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai adai.zip [root@3 d6z]# tar -cvf adai.tar adai/ adai/ adai/1.txt adai/2.txt [root@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai adai.tar adai.zip [root@3 d6z]# du -ah 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 656K ./adai.zip 1.3M ./1.txt 2.5M ./adai.tar 8.3M .
说明: 打包不会删除源文件,当某.tar文件已经存在时,再次打包会直接覆盖该文件,无任何提示。
eg2: 打包普通文件
[root@3 d6z]# tar -cvf 2.tar 2.txt 2.txt [root@3 d6z]# ls 1.txt 1.txt.zip 2.tar 2.txt adai adai.tar adai.zip [root@3 d6z]# du -ah 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 656K ./adai.zip 1.3M ./1.txt 2.5M ./adai.tar 1.3M ./2.tar 9.5M .
eg3: 同时打包目录文件和普通文件
[root@3 d6z]# tar -cvf adailinux.tar adai 1.txt 2.txt adai/ adai/1.txt adai/2.txt 1.txt 2.txt [root@3 d6z]# ls 1.txt 1.txt.zip 2.tar 2.txt adai adailinux.tar adai.tar adai.zip [root@3 d6z]# du -ah 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 656K ./adai.zip 1.3M ./1.txt 2.5M ./adai.tar 1.3M ./2.tar 4.9M ./adailinux.tar 15M .
[root@3 d6z]# tar -tf 3.tar 3/ 3/1.txt 3/2.txt
[root@3 d6z]# ls 3 1.txt 2.txt 3.txt 3linux.tar 3.tar [root@3 d6z]# tar -cvf 3.tar 3 --exclude "*.txt" 打包除了“.txt”之外的文件 3/ 3/3linux.tar 3/3.tar [root@3 d6z]# tar -cvf 3.tar 3 --exclude 1.txt --exclude 3.tar 打包除了1.txt和3.tar之外的文件 3/ 3/2.txt 3/3linux.tar 3/3.txt [root@3 d6z]# tar -tf 32.tar 3/ 3/2.txt 3/3linux.tar 3/3.txt
[root@3 d6z]# tar -xvf adai.tar adai/ adai/1.txt adai/2.txt [root@3 d6z]# ls 1.txt 1.txt.zip 2.txt adai adai.tar adai.zip [root@3 d6z]# du -ah 1.3M ./2.txt 1.3M ./adai/1.txt 1.3M ./adai/2.txt 2.5M ./adai 328K ./1.txt.zip 656K ./adai.zip 1.3M ./1.txt 2.5M ./adai.tar 8.3M .
说明: 解包时,若是该文件已经存在则会直接覆盖,无任何提示。
语法: tar [options] [filename]
options:
-z:同时用gzip压缩
-j:同时用bzip2压缩
-J:同时用xz压缩
eg1: 打包并用gzip压缩
[root@3 d6z]# du -h adai 9.8M adai [root@3 d6z]# tar -czvf adai.tar.gz adai adai/ adai/1.txt adai/2.txt adai/adailinux.tar adai/adai.tar adai/3.txt [root@3 d6z]# du -h adai.tar.gz 2.6M adai.tar.gz
eg2: 打包并用bzip2压缩
[root@3 d6z]# tar -cjvf adai.tar.bz2 adai adai/ adai/1.txt adai/2.txt adai/adailinux.tar adai/adai.tar adai/3.txt [root@3 d6z]# du -h adai.tar.bz2 972K adai.tar.bz2
eg3: 打包并用xz压缩
[root@3 d6z]# tar -cJvf adai.tar.xz adai adai/ adai/1.txt adai/2.txt adai/adailinux.tar adai/adai.tar adai/3.txt [root@3 d6z]# du -h adai.tar.xz 64K adai.tar.xz
示例:解包并解压xz格式的包
[root@3 d6z]# tar -xJvf adai.tar.xz adai/ adai/1.txt adai/2.txt adai/adailinux.tar adai/adai.tar adai/3.txt
说明: 其他两种压缩格式同理。