Linux打包和壓縮——管理打包和壓縮的命令


Linux打包和壓縮——管理打包和壓縮的命令

摘要:本文主要學習了Linux的打包命令和壓縮命令。

tar命令

tar命令可以用來進行打包和解打包,壓縮和解壓縮。

基本語法

打包和壓縮的語法:

1 tar [選項] 源文件或目錄

解打包和解壓縮的語法:

1 tar [選項] 壓縮包

選項說明

打包和壓縮的選項:

1 -c:將多個文件或目錄進行打包。
2 -v:顯示打包文件的過程。
3 -f 文件名:指定打包的文件名。
4 -z:壓縮和解壓縮.tar.gz格式。
5 -j:壓縮和解壓縮.tar.bz2格式。

解打包和解壓縮的選項:

1 -x:對tar包做解打包操作。
2 -v:顯示解打包的過程。
3 -f 文件名:指定要解壓的文件名。
4 -z:壓縮和解壓縮.tar.gz格式。
5 -j:壓縮和解壓縮.tar.bz2格式。
6 -t:只查看包中有哪些文件或目錄,不做解打包操作。
7 -C 目錄名:指定解打包位置。

使用舉例

打包為 tar 格式的文件:

1 [root@localhost home]# tar -cvf hello.tar hello hello-hard hello-soft 
2 hello
3 hello-hard
4 hello-soft
5 [root@localhost home]# ls
6 hello  hello-hard  hello-soft  hello.tar  test  test-soft
7 [root@localhost home]# 

壓縮為 tar.gz 格式的文件:

1 [root@localhost home]# tar -zcvf test.tar.gz test test-soft
2 test/
3 test-soft
4 [root@localhost home]# ls
5 hello  hello-hard  hello-soft  hello.tar  test  test-soft  test.tar.gz
6 [root@localhost home]# 

解打包 tar 格式的文件:

1 [root@localhost home]# tar -xvf hello.tar 
2 hello
3 hello-hard
4 hello-soft
5 [root@localhost home]# ls
6 hello  hello-hard  hello-soft  hello.tar  test.tar.gz
7 [root@localhost home]# 

解壓縮 tar.gz 格式的文件:

1 [root@localhost home]# tar -zxvf test.tar.gz 
2 test/
3 test-soft
4 [root@localhost home]# ls
5 hello  hello-hard  hello-soft  hello.tar  test  test-soft  test.tar.gz
6 [root@localhost home]# 

查看 tar 格式文件的內容:

1 [root@localhost home]# tar -tvf hello.tar
2 -rw-r--r-- root/root     10240 2019-07-11 01:28 hello
3 hrw-r--r-- root/root         0 2019-07-11 01:28 hello-hard 連接到 hello
4 lrwxrwxrwx root/root         0 2019-07-11 03:56 hello-soft -> hello
5 [root@localhost home]# 

查看 tar.gz 格式文件的內容:

1 [root@localhost home]# tar -ztvf test.tar.gz 
2 drwxr-xr-x root/root         0 2019-07-11 01:14 test/
3 drwxr-xr-x root/root         0 2019-07-11 01:14 test-soft/
4 [root@localhost home]# 

zip命令

zip命令類似於Windows系統中的winzip壓縮程序。

基本語法

1 zip [選項] 壓縮包名 源文件或源目錄列表

選項說明

1 -r:遞歸壓縮目錄,及將指定目錄下的所有文件以及子目錄全部壓縮。
2 -m:將文件壓縮之后,刪除原始文件,相當於把文件移到壓縮文件中。
3 -v:顯示詳細的壓縮過程信息。
4 -q:在壓縮的時候不顯示命令的執行過程。
5 -壓縮級別:壓縮級別是從1~9的數字,-1代表壓縮速度更快,-9代表壓縮效果更好。
6 -u:更新壓縮文件,即往壓縮文件中添加新文件。

使用舉例

 1 [root@localhost home]# ls
 2 hello  hello-hard  hello-soft  test  test-soft
 3 [root@localhost home]# zip hello.zip hello hello-hard 
 4   adding: hello (deflated 99%)
 5   adding: hello-hard (deflated 99%)
 6 [root@localhost home]# ls
 7 hello  hello-hard  hello-soft  hello.zip  test  test-soft
 8 [root@localhost home]# zip test.zip test test-soft
 9   adding: test/ (stored 0%)
10   adding: test-soft/ (stored 0%)
11 [root@localhost home]# ls
12 hello  hello-hard  hello-soft  hello.zip  test  test-soft  test.zip
13 [root@localhost home]#

unzip命令

unzip命令可以查看和解壓縮zip文件。

基本語法

1 unzip [選項] 壓縮包名

選項說明

1 -d 目錄名:將壓縮文件解壓到指定目錄下。
2 -n:解壓時並不覆蓋已經存在的文件。
3 -o:解壓時覆蓋已經存在的文件,並且無需用戶確認。
4 -v:查看壓縮文件的詳細信息,包括壓縮文件中包含的文件大小、文件名以及壓縮比等,但並不做解壓操作。
5 -t:測試壓縮文件有無損壞,但並不解壓。
6 -x 文件列表:解壓文件,但不包含文件列表中指定的文件。

使用舉例

 1 [root@localhost home]# ls
 2 hello.zip  test.zip
 3 [root@localhost home]# unzip -v hello.zip 
 4 Archive:  hello.zip
 5  Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
 6 --------  ------  ------- ---- ---------- ----- --------  ----
 7    10240  Defl:N       99  99% 07-11-2019 01:28 dda39bf9  hello
 8    10240  Defl:N       99  99% 07-11-2019 01:28 dda39bf9  hello-hard
 9 --------          -------  ---                            -------
10    20480              198  99%                            2 files
11 [root@localhost home]# unzip hello.zip 
12 Archive:  hello.zip
13   inflating: hello                   
14   inflating: hello-hard              
15 [root@localhost home]# ls
16 hello  hello-hard  hello.zip  test.zip
17 [root@localhost home]# unzip -d zip test.zip 
18 Archive:  test.zip
19    creating: zip/test/
20    creating: zip/test-soft/
21 [root@localhost home]# ls
22 hello  hello-hard  hello.zip  test.zip  zip
23 [root@localhost home]# ls zip
24 test  test-soft
25 [root@localhost home]#


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM