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
說明: 其他兩種壓縮格式同理。