bash 提取 zip 文件内容并动态重命名目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26772701/
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-09-18 11:44:08  来源:igfitidea点击:

Extract zip files contents and rename the directory dynamically

linuxbashshellunzipdirectory-structure

提问by Spidey

I have an application zip file created using Play Framework. It create the zip file with name A-1.0.zip. This zip file contains the directory with name A-1.0. (1.0 changes according to the version)

我有一个使用 Play Framework 创建的应用程序 zip 文件。它创建名为 A-1.0.zip 的 zip 文件。此 zip 文件包含名为 A-1.0 的目录。(1.0根据版本变化)

I wanted to extract the zip file and rename the folder from A-1.0 to A. So that my application init.d script finds the directory to start the application. This shuld be done dynamically using shell script.

我想解压缩 zip 文件并将文件夹从 A-1.0 重命名为 A。以便我的应用程序 init.d 脚本找到启动应用程序的目录。这应该使用 shell 脚本动态完成。

Is there a way where i can extract all the zip files into A folder instead of extracting into A-1.0 and renaming?? Please help!

有没有办法可以将所有 zip 文件解压缩到 A 文件夹中,而不是解压缩到 A-1.0 并重命名?请帮忙!

The following is what I tried....

以下是我尝试过的......

unzip A-1.0.zip -d ~/A

(I know that it is very dumb of me to do this !!)

(我知道我这样做很愚蠢!!)

This extracted the file into ~/A/A-1.0/[contents]

这将文件提取到 ~/A/A-1.0/[contents]

I need to extract all the [contents] into ~/Ainstead of ~/A/A-1.0/. I dunno how to do this using command line.....

我需要将所有 [内容] 提取到~/A而不是~/A/A-1.0/. 我不知道如何使用命令行来做到这一点.....

My init.d script searched for ~/A/bin/A -Dhttp.port=6565 -Dconfig.file=~/A/conf/application.confto start the Play! application.

我的 init.d 脚本搜索~/A/bin/A -Dhttp.port=6565 -Dconfig.file=~/A/conf/application.conf开始播放!应用。

To make this script working, I extract all into A-1.0/ then I rename with mv ~/A-1.0 ~/Amanually.

为了使这个脚本工作,我将所有内容提取到 A-1.0/ 然后我mv ~/A-1.0 ~/A手动重命名。

回答by Alexis LEGROS

I didn't find any specific unzip option to perform this automatically, but managed to achieve this goal by creating a temporary symbolic link in order to artificially redirect the extracted files this way

我没有找到任何特定的解压缩选项来自动执行此操作,但设法通过创建临时符号链接以这种方式人为地重定向提取的文件来实现此目标

ln -s A A-1.0
unzip A-1.0.zip
rm A-1.0

回答by Géza T?r?k

From the unzip man pageit boils down to:

解压缩手册页它归结为:

unzip A-1.0.zip 'A-1.0/*' -d /the/output/dir
      ^         ^
      |         |
      |         +- files to extract (note the quotes: unzip shall parse the wildcard instd of sh)
      +- The archive

回答by Thor_Bux

EDIT: This answer does not preserve subdirectories. It works fine if one doesn't have or need the subdirectory structure.

编辑:此答案不保留子目录。如果没有或不需要子目录结构,它可以正常工作。

I found that you can combine the answer from @géza-t?r?k with the -joption mentioned by @david-c-rankin (in the comment below the question). Which leads to unzip -j A-1.0.zip 'A-1.0/*' -d /the/output/dir. That would only process the files inside A-1.0/ and output them straight into the given output directory.

我发现您可以将@géza-t?r?k 的答案与-j@david-c-rankin 提到的选项结合起来(在问题下方的评论中)。这导致unzip -j A-1.0.zip 'A-1.0/*' -d /the/output/dir. 那只会处理 A-1.0/ 中的文件并将它们直接输出到给定的输出目录中。

Source: https://linux.die.net/man/1/unzip(look at -j)

来源:https: //linux.die.net/man/1/unzip(看-j)