如何从终端解压缩zip文件?

刚刚从互联网上下载了一个.zip文件。 我想使用终端解压缩文件。 这样做的正确方法是什么?

如果系统上尚未安装unzip命令,则运行:

 sudo apt-get install unzip 

安装解压缩实用程序后,如果要解压缩到特定目标文件夹,可以使用:

 unzip file.zip -d destination_folder 

您只需使用unzip

安装它: apt-get install unzip

并使用它: unzip file.zip

一个更有用的工具是7z ,它可以压缩和解压缩一系列压缩格式,特别是lzma ,通常是提供最高压缩率的协议。

此命令安装7z

 sudo apt-get install p7zip-full 

此命令列出了zip的内容:

 7z l zipfile.zip 

此命令提取zip的内容:

 7z x zipfile.zip 

您可以使用:

 unzip file.zip -d somedir 

提取到yourpath/somedir

如果要提取到绝对路径,请使用

 sudo unzip file.zip -d /somedir 

使用脚本工具:Perl和Python

这里的许多答案都提到了需要安装的工具,但是没有人提到Ubuntu的两种脚本语言Perl和Python已经提供了所有必要的模块,允许你解压缩zip存档,这意味着你不需要安装任何东西。其他。 只需使用下面提供的两个脚本中的任何一个来完成这项工作。 如果我们愿意的话,它们相当短,甚至可以缩小为单线命令。

python

 #!/usr/bin/env python3 import sys from zipfile import PyZipFile for zip_file in sys.argv[1:]: pzf = PyZipFile(zip_file) pzf.extractall() 

用法:

 ./pyunzip master.zip 

Perl的

 #!/usr/bin/env perl use Archive::Extract; foreach my $filepath (@ARGV){ my $archive = Archive::Extract->new( archive => $filepath ); $archive->extract; } 

用法:

 ./perlunzip master.zip 

另请参阅提取tar.gz存档的脚本 。

如果您的目的地与源zip文件相同,您可以简单地执行以下操作:

 unzip filename.zip 

我更喜欢bsdtar unzip / zip 。 对于提取,它们非常相似:

 bsdtar -x -f /one/two/three/four.zip -C /five unzip /one/two/three/four.zip -d /five 

但是对于压缩, bsdtar获胜。 说你有这个输入:

 /one/two/three/alfa/four.txt /one/two/three/bravo/four.txt 

并希望在zip文件中:

 alfa/four.txt bravo/four.txt 

使用bsdtar很容易:

 bsdtar -a -c -f four.zip -C /one/two/three alfa bravo 

zip没有像解压缩一样的-d选项,所以除非你先cd ,否则你无法实现上述目的。

以下是我觉得有用的选项的详细说明:

 Command: unzip -[option] zip-path. -l List archive files. -t Test archive files with cyclic redundancy check. -u update the existing files. -z archive comment. 

这是您要在当前目录中提取的命令

 unzip .zip 

如果要提取到特定目标文件夹,则可以使用

 unzip .zip -d  

如果有密码,你也可以使用-P

 unzip -P  .zip 

如果您的系统上尚未安装解压缩包,请运行:

 sudo apt-get install unzip 

解压缩安装

首先,如果没有安装,我们需要在我们的系统上安装解压缩。 unzip命令用于从ZIP存档中提取文件。

运行以下命令安装unzip

 sudo apt-get install unzip 

unzip Syntex

 $ unzip [-aCcfjLlnopqtuvy] [-d dir] zipfile 

现在请按照以下步骤操作:

解压缩文件

选项1 – 如果Zip文件与您的终端所在的目录/文件夹相同,我们想在当前工作目录中提取它。

使用以下命令来实现上述方案

 sudo unzip zip_file_name.zip 

如果zip文件受某些密码保护,请使用以下命令:

 sudo ubzip -P zip_file_name.zip 

请确保使用-P(大写P)而不是-p,因为它们是不同的选项。

选项2 – 如果zip文件不在同一目录中,我们想要在不同的目录中解压缩/解压缩该文件。

使用以下命令来实现上述方案

 sudo unzip path/filename.zip -d another_path_or_same_path 

如果我们不使用选项-d,则将解压缩文件以显示工作目录。

如果zip文件受密码保护,我们也可以使用-P

在Linux / Unix中使用tar命令

tar是Tape Archive的首字母缩写。 tar命令用于在Linux / Unix中处理归档。 系统管理员经常使用tar命令将一堆文件或目录翻录成高度压缩的存档,在Linux / Unix系统中称为tarballtarbzipgzip

焦油Syntex

 tar [OPTION...] [FILE]... 

要么

tar需要标志

 tar {-r|-t|-c|-x|-u} 

tar可选标志

 tar {one of the required Flags} [ -d ][-B] [ -F ] [ -E ] [ -i ] [-h ] [ -l ] [ -m ] [ -o ] [ -p ] [ -w] [ -s ] [ -U ] [ -v ] [-Number] [-b Blocks] [-f Archive] 

例子

通过压缩目录或单个文件来创建tar存档文件

下面的terminal命令将在当前工作目录中创建一个名为sample_dir.tar.tar文件,其中包含目录/home/codebind/sample_dirsample_dir

 ripon@ripon:~$ tar -cvf sample_dir.tar sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tar 

在此处输入图像描述

这些是那些标志(-cvf)实际意味着什么

-c, --create – create – 创建一个新的存档

-x, --extract, --get – extract -x, --extract, --get-x, --extract, --get – 从存档中提取文件

-f, --file ARCHIVE – file -f, --file ARCHIVE – 使用存档文件或设备存档

通过压缩目录或单个文件来创建tar.gztgz存档文件

下面的terminal命令将在当前工作目录中创建一个名为sample_dir.tar.gz.tar.gz文件,目录为/home/codebind/sample_dirsample_dir

请注意,我们在命令中添加了额外的标志-z。这是-z实际意味着的标志

-z, --gzip, --gunzip --ungzip – gzip -z, --gzip, --gunzip --ungzip – 使用gzip压缩存档

 ripon@ripon:~$ tar -cvzf sample_dir.tar.gz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tar.gz 

在此处输入图像描述

命令bellow将创建一个.tgz文件。 需要注意的是tar.gz和tgz都是相似的。

 ripon@ripon:~$ tar -cvzf sample_dir.tgz sample_dirsample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ ls sample_dir sample_dir.tgz 

一次压缩多个目录或文件

假设,例如,我们要将sample_dir目录, java_test目录和abc.py文件压缩为名为sample_dir.tar.gz的tar文件。

运行以下命令以实现上述目标。

 ripon@ripon:~$ tar -cvzf sample_dir.tar.gz sample_dir java_test abc.py sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output java_test/ java_test/HelloCV.java abc.py ripon@ripon:~$ ls sample_dir java_test abc.py sample_dir.tar.gz 

在此处输入图像描述

通过压缩目录或单个文件来创建.bzip2存档文件

 ripon@ripon:~$ tar -cjvf sample_dir.tar.bz2 sample_dir sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ 

请注意,我们在命令中添加了额外的标志-f这是标志-f实际意味着什么

-f, --file ARCHIVE – file -f, --file ARCHIVE – 使用存档文件或设备存档

在此处输入图像描述

提取.tar存档文件

我们可以使用tar命令提取或解压缩压缩文件。 下面的命令将sample_dir.tar的内容解压缩到当前目录。

 ripon@ripon:~$ tar -xvf sample_dir.tar sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ 

在此处输入图像描述

以下命令将在指定目录中提取或解压缩文件,即/home/codebind/dir_name

 ripon@ripon:~$ tar -xvf sample_dir.tar -C /home/codebind/dir_name sample_dir/ sample_dir/main.cpp sample_dir/sample.png sample_dir/output ripon@ripon:~$ 

我们在命令中添加了额外的标志-C这是标志-C实际含义

-C, --directory DIR – 目录DIR – 更改为目录DIR

在此处输入图像描述

首先,如果没有安装,你需要安装unzip

 sudo apt-get install unzip 

1 – 如果文件与终端位于同一目录中,并且您想要在同一位置提取它。

 sudo unzip file.zip 

如果文件受密码保护使用:

 sudo unzip -P file.zip 

确保它是-P不是-p它是不同的选择

2 – 如果文件不在同一目录中,并且您想将其提取到终端所在的不同目录中。 使用 :

 sudo unzip path/filename.zip -d anotherOrSamePath 

如果你没有使用-d文件将被提取到终端上的位置。

如果有密码,您也可以使用-P