解压缩并移动下载的文件 - Linux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14381460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:42:55  来源:igfitidea点击:

Unzip and move a downloaded file - Linux

linuxubuntucommand-line

提问by Houdini

Surprisingly I could not find a straight-forward answer to this question on here yet. I am still learning Linux. Say I have downloaded a zip file to my Downloads folder. Now, I want to move it into a protected folder, like /opts or /var. Is there a good command to both sudo moveAND unzipthe file to where I need it to go?

令人惊讶的是,我还没有在这里找到这个问题的直接答案。我还在学习Linux。假设我已将 zip 文件下载到我的下载文件夹。现在,我想将其移动到受保护的文件夹中,例如 /opts 或 /var。是否有一个很好的命令可以将文件sudo moveunzip文件放到我需要去的地方?

采纳答案by Shawn Chin

If you wish to perform two separate operations (move and extract) then you have no option but to use two commands.

如果您希望执行两个单独的操作(移动和提取),那么您别无选择,只能使用两个命令。

However, if your end goal is to extract the zip file to a specific directory, you can leave the zip file where it is and specify an extraction directory using the -doption:

但是,如果您的最终目标是将 zip 文件解压缩到特定目录,则可以将 zip 文件保留在原处,并使用以下-d选项指定解压缩目录:

sudo unzip thefile.zip -d /opt/target_dir

From the manpage:

联机帮助页

[-d exdir]

An optional directory to which to extract files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), immediately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be suppressed. In particular, ''-d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but ''-d~'' is treated as a literal subdirectory ''~'' of the current directory.

[-d exdir]

将文件提取到的可选目录。默认情况下,所有文件和子目录都在当前目录中重新创建;-d 选项允许在任意目录中提取(始终假设一个人有权写入该目录)。这个选项不需要出现在命令行的末尾;它也被接受在 zipfile 规范之前(使用普通选项),紧跟在 zipfile 规范之后,或者在文件和 -x 选项之间。选项和目录可以连接起来,它们之间没有任何空格,但请注意,这可能会导致正常的 shell 行为被抑制。特别是,''-d ~''(波浪号)被 Unix C shell 扩展为用户主目录的名称,但 ''-d~'' 被视为一个字面子目录 ''~''

回答by Ivaylo Strandjev

sudo mv <file_name> /opts && unzip /opts/<file_name>

sudo mv <file_name> /opts && unzip /opts/<file_name>

Also you may specify the unzip destination to unzip so you can do this in a single command. This however will be a bit different from the command above as the zip will be kept in its current location, only the unzipped files will be extracted to the pointed destination.

您也可以指定解压缩目标,以便您可以在单个命令中执行此操作。然而,这与上面的命令有点不同,因为 zip 将保留在其当前位置,只有解压缩的文件将被提取到指定的目的地。

回答by Satish

unzip -d [target directory] [filename].zip