unzip 命令往往用于解壓縮 zip 文件,但它能做的不僅僅是如此。 今天我們主要介紹一下該命令的作用及用法。
在某些 Linux 發(fā)行版上,可能沒有默認(rèn)安裝該命令,那需要先安裝,可以使用包管理器,比如 Debian 系統(tǒng)中:
sudo apt install unzip
下面我們來看一下它的用法。
Linux 中的 unzip 命令
unzip 命令的語法很簡單:
unzip [option] zip_file
如果不使用任何選項(xiàng),那么它會(huì)解壓整個(gè)給定的文件,看下面的例子:
unzip webdesign.zip
Archive: webdesign.zip
inflating: 339252-PAJF05-394.ai
inflating: 339252-PAJF07-322.eps
inflating: 339252-PALBTI-224.ai
inflating: 339252-PALBTL-394.eps
inflating: 339252-PALBTM-53.jpg
inflating: License free.txt
inflating: License premium.txt
有時(shí)候,這可能并不是我們想要的。 因?yàn)槿绻o定的壓縮文件中的文件太多,那么解壓后,當(dāng)前目錄中會(huì)充斥了大量的文件:
ls
339252-PAJF05-394.ai 339252-PALBTI-224.ai 339252-PALBTM-53.jpg 'License premium.txt'
339252-PAJF07-322.eps 339252-PALBTL-394.eps 'License free.txt' webdesign.zip
1,解壓到某個(gè)目錄
一般情況下,我們希望將文件解壓到某個(gè)目錄,然后目錄通常是與 zip 文件同名。 可以使用 -d 選項(xiàng),指定將文件提取(壓縮)到某個(gè)目標(biāo)目錄:
unzip -d target_directory zip_file
如果目標(biāo)目錄不存在,就會(huì)創(chuàng)建。 但是不會(huì)創(chuàng)建嵌套的目錄。
另外,也可以將目標(biāo)目錄(連同選項(xiàng) -d)放在壓縮文件后面,但并不是所有選項(xiàng)都可以放到命令后面。 如下所示:
unzip zip_file -d target_directory
2,在不解壓的情況下,查看壓縮文件的內(nèi)容
如果只想查看 zip 文件包中的內(nèi)容,但是并不想解壓它,可以使用 -l 選項(xiàng),該選項(xiàng)可以讓我們查看 zip 文件的內(nèi)容:
unzip -l zip_file
在輸出的文件列表中,還會(huì)現(xiàn)實(shí)文件的時(shí)間戳和大小(以字節(jié)為單位),如下所示:
$ unzip -l webdesign.zip
Archive: webdesign.zip
Length Date Time Name
--------- ---------- ----- ----
205358 2023-01-18 23:14 339252-PAJF05-394.ai
996906 2023-01-18 23:14 339252-PAJF07-322.eps
213968 2023-01-20 00:00 339252-PALBTI-224.ai
1005362 2023-01-20 00:00 339252-PALBTL-394.eps
305531 2023-01-20 00:00 339252-PALBTM-53.jpg
1462 2023-01-20 09:45 License free.txt
1116 2023-01-20 09:45 License premium.txt
--------- -------
2729703
如果需要,可以使用 -v 選項(xiàng)來獲得更多信息,如壓縮大小、壓縮比等。 輸出中的 CRC-32 是循環(huán)冗余檢查。
$ unzip -v webdesign.zip
Archive: webdesign.zip
Length Method Size Cmpr Date Time CRC-32 Name
-------- ------ ------- ---- ---------- ----- -------- ----
205358 Defl:N 142097 31% 2023-01-18 23:14 792f2380 339252-PAJF05-394.ai
996906 Defl:N 221228 78% 2023-01-18 23:14 440390d3 339252-PAJF07-322.eps
213968 Defl:N 147568 31% 2023-01-20 00:00 cdb64dfc 339252-PALBTI-224.ai
1005362 Defl:N 226727 77% 2023-01-20 00:00 aed3d97a 339252-PALBTL-394.eps
305531 Defl:N 183363 40% 2023-01-20 00:00 e4ced90f 339252-PALBTM-53.jpg
1462 Defl:N 728 50% 2023-01-20 09:45 0eb9e17b License free.txt
1116 Defl:N 558 50% 2023-01-20 09:45 165dd84e License premium.txt
-------- ------- --- -------
2729703 922269 66% 7 files
3,在不提示的情況下覆蓋已有文件
如果在當(dāng)前目錄中,已經(jīng)存在同名的文件,那么在解壓的時(shí)候,會(huì)有提示“是否要覆蓋當(dāng)前文件”,可以使用 -o 選項(xiàng),來跳過詢問,強(qiáng)制覆蓋:
unzip -o -d target_directory zip_file
4,不要覆蓋任何文件
如果不希望覆蓋現(xiàn)有文件,可以使用 -n 選項(xiàng):
unzip -n -d target_directory zip_file
5,更新且在必要的時(shí)候創(chuàng)建文件
更新文件,與覆蓋略有不同。 當(dāng)某些文件的時(shí)間戳比現(xiàn)有文件更新的時(shí)候,才會(huì)被覆蓋。 如果文件不存在,將會(huì)直接創(chuàng)建。 這種情況下,需要使用 -u 選項(xiàng):
unzip -u -d target_directory zip_file
6,更新現(xiàn)有文件,但是不創(chuàng)建新文件
這個(gè)例子與前一個(gè)略有不同。 在本例中,如果有某些文件的時(shí)間戳更新,會(huì)覆蓋這些文件,但是如果文件不存在,那么不會(huì)創(chuàng)建新文件。 這種情況,可使用 -f 選項(xiàng):
unzip -f -d target_directory zip_file
7,在安靜模式下解壓文件
在解壓文件的時(shí)候,會(huì)在屏幕上現(xiàn)實(shí)已經(jīng)解壓的文件。 但是當(dāng)文件有幾百個(gè)的時(shí)候... 屏幕會(huì)一直出現(xiàn)滾動(dòng)信息。
可以使用 -q 選項(xiàng)來選擇安靜模式,這種情況下,在解壓的過程中屏幕不會(huì)有信息輸出。
unzip -q -d target_directory zip_file
8,解壓的時(shí)候,排除某些文件
unzip 命令,可以實(shí)現(xiàn)在解壓的時(shí)候,排除某些文件,或者某些類型的文件:
unzip zip_file -x file_to_exclude
比如,我想在解壓的時(shí)候,排除 .eps 文件,如下:
$ unzip webdesign.zip -x *.eps
Archive: webdesign.zip
inflating: 339252-PAJF05-394.ai
inflating: 339252-PALBTI-224.ai
inflating: 339252-PALBTM-53.jpg
inflating: License free.txt
inflating: License premium.txt
以上例子是關(guān)于 unzip 命令的一些常用的,大家可以查看 man 手冊來獲取其更多的信息。
-
Linux
+關(guān)注
關(guān)注
87文章
11351瀏覽量
210505 -
Zip
+關(guān)注
關(guān)注
0文章
17瀏覽量
8662 -
文件
+關(guān)注
關(guān)注
1文章
571瀏覽量
24834 -
命令
+關(guān)注
關(guān)注
5文章
698瀏覽量
22126
發(fā)布評論請先 登錄
相關(guān)推薦
掌握find命令的這幾種用法,就沒有找不到的文件!
at命令用法
linux中comm命令用法
VirtualBox 的命令行用法
各種Shell命令的用法和作用
命令行工具Kubectl的別樣用法
Ping命令的7個(gè)基礎(chǔ)用法
模擬初始狀態(tài)相關(guān)的.ic和.save命令用法
一款運(yùn)行于windows上的linux命令神器-Cmder
![一款運(yùn)行于windows上的linux<b class='flag-5'>命令</b>神器-Cmder](https://file1.elecfans.com/web2/M00/82/78/wKgaomRUaqSAGDKHAAAwr9PbXMY967.png)
nmcli命令的一些常用選項(xiàng)和用法
![nmcli<b class='flag-5'>命令</b>的一些常用選項(xiàng)和<b class='flag-5'>用法</b>](https://file1.elecfans.com/web2/M00/A7/26/wKgaomUiH1eASGCwAAAMPlVzK4s312.png)
linux常用命令及用法
總結(jié)linux命令行的主要用法
Linux lsof命令的基本用法
![Linux lsof<b class='flag-5'>命令</b>的基本<b class='flag-5'>用法</b>](https://file1.elecfans.com/web1/M00/F3/7F/wKgaoWcYc2mAFw5cAAA4fI5XmGk856.png)
評論