输出 tar 存档中的根目录

发布于 2024-11-02 04:16:50 字数 227 浏览 2 评论 0 原文

我正在尝试使用 shell 脚本自动化您在编译 nginx 之类的东西时所经历的过程。 (我不想使用 apt-get )

目前我有这个:

wget http://nginx.org/download/nginx-1.0.0.tar.gz
tar xf nginx-1.0.0.tar.gz

但接下来我需要找出它提取的目录名称是什么,以便我可以启动配置脚本。

I'm trying to automate the process you go through when compiling something like nginx using a shell script. (I don't want to use apt-get)

Currently I have this:

wget http://nginx.org/download/nginx-1.0.0.tar.gz
tar xf nginx-1.0.0.tar.gz

But next I need to find out what the directory name is from where it extracted too so I can start the configure script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

旧人 2024-11-09 04:16:50

使用它来查找存档的顶级目录(-ies)。

tar tzf nginx-1.0.0.tar.gz | sed -e 's@/.*@@' | uniq

此处调用 sed 来获取由 tar 打印的路径的第一个组成部分,因此它

path/to/file --> path

会通过执行 s 命令。我使用 @ 符号作为分隔符,而不是更常见的 / 符号,以避免在正则表达式中转义 / 。因此,该命令的意思是:用空字符串替换与 /.* 模式(即斜杠后跟任意数量的任意字符)匹配的字符串部分。或者,换句话说,删除第一个斜杠之后(包括)的字符串部分。

(必须对其进行修改才能使用绝对文件名;但是,这些在 tar 文件中非常罕见。但请确保这种理论上的可能性不会在您的代码中创建漏洞!)

Use this to find out the top-level directory(-ies) of an archive.

tar tzf nginx-1.0.0.tar.gz | sed -e 's@/.*@@' | uniq

sed is invoked here to get the first component of a path printed by tar, so it transforms

path/to/file --> path

It does this by executing s command. I use @ sign as a delimiter instead of more common / sign to avoid escaping / in the regexp. So, this command means: replace part of string that matches /.* pattern (i.e. slash followed by any number of arbitrary characters) with the empty string. Or, in other words, remove the part of the string after (and including) the first slash.

(It has to be modified to work with absolute file names; however, those are pretty rare in tar files. But make sure that this theoretical possibility does not create a vulnerability in your code!)

离笑几人歌 2024-11-09 04:16:50

按照另一个答案中所述使用 sed 是一个很好的方法,但最好在 sed 之前使用 head -1 而不是在之后使用 uniq ;这具有更好的性能 - 您只需通过 sed 泵送第一行,这也避免了 uniq 将 sed 的整个输出加载到内存中的要求。此外,如果 tar 包含多个顶级目录,这将返回第一个顶级目录,而 uniq 将提供所有顶级目录。

tar tzf nginx-1.0.0.tar.gz | head -1 | sed -e 's/\/.*//'

我个人认为将 sed 模式匹配中的内部 / 转义为 \/ 比引入另一个分隔符(例如 @)更具可读性,但这只是一个偏好问题

Using sed as described in the other answer is a good approach, but it's better to use head -1 before sed instead of uniq after; this has much better performance - you are pumping only the first line through sed and this also avoids the requirement of uniq to load the entire output of sed into memory. Furthermore, if the tar contains multiple top level directories, this will return the first top level directory, whereas uniq will give you all top level directories.

tar tzf nginx-1.0.0.tar.gz | head -1 | sed -e 's/\/.*//'

I personally find it more readable to escape the internal / in the pattern match of sed as \/ rather than introducing another delimiter such as @, but that's just a matter of preference

你好,陌生人 2024-11-09 04:16:50

这个如何获取所有顶级目录(包括 . ):

tar tf nginx-1.0.0.tar.gz | xargs dirname | sort | uniq

要获取第一个顶级目录,我将使用 @thomas-steinbach 发布的解决方案:

tar tf nginx-1.0.0.tar.gz | head -1

How about this one for getting all top level directories (including . ):

tar tf nginx-1.0.0.tar.gz | xargs dirname | sort | uniq

To get the first top level directory I would use the solution posted by @thomas-steinbach:

tar tf nginx-1.0.0.tar.gz | head -1
云裳 2024-11-09 04:16:50

此命令仅使用 tar

tar --exclude=\*/\*/\* --no-wildcards-match-slash -tzf nginx-1.0.0.tar.gz

有趣的排除模式(未转义)为 */*/*,表示“排除所有两层深度的文件和目录”:第一个* 为基目录,第二个为第一级文件和目录,第三个为第二级文件和目录。

仅当通配符仅匹配文件/目录名称(不带斜杠)时,这才有效。这就是 --no-wildcards-match-slash 存在的原因。

This command only uses tar:

tar --exclude=\*/\*/\* --no-wildcards-match-slash -tzf nginx-1.0.0.tar.gz

The funny exclude pattern, unescaped, is */*/* and means "exclude all files and directories that are two levels deep": the first * is for the base directory, the second is for the files and directories at first level, the third is for the files and directories at second level.

This could only work if the wildcards match just the file/directory names, without the slashes. That's why --no-wildcards-match-slash is there.

記柔刀 2024-11-09 04:16:50

上面的许多答案都是正确的,但我遇到了一种情况,由于管道到head,实际的焦油停止

Borne shell 中的以下命令:

tar -v -zxf plotutils-3.1.tar.gz | head -1 | cut -d "/" -f 1

将生成顶级目录名称:plotutils-3.1
然而,生成的目录要么是空的,要么包含一项。
我用的是ubuntu。要获得 tar 的实际结果,您必须

tar -zxf plotutils-3.1.tar.gz  

再次执行另一个命令。我不确定我在这里做错了什么;但这一点应该注意。我在尝试编写 shell 脚本来自动运行 autotool 的配置脚本时发现了这一点。希望这可以帮助其他人。

Many of the above answers are correct, but I encounter a situation where the actual tar stopped as a result of piping to head.

The following command in Borne shell:

tar -v -zxf plotutils-3.1.tar.gz | head -1 | cut -d "/" -f 1

Will produce the top directory name: plotutils-3.1
However, the resulting directory will be either empty or containing one item.
I am using ubuntu. To get the actual result of the tar you have to do another command

tar -zxf plotutils-3.1.tar.gz  

again. I am not sure I am doing something wrong here; but this should be noted. I found this out when trying to write a shell script to automatically run the autotool's configure script. Hope this may help others.

半枫 2024-11-09 04:16:50

目录名称应为 nginx-1.0.0 或任何不带 .tar.gz 的 tarball 名称。在 wget 和 tar 之后尝试这个:

cd nginx*
./configure
# etc

如果您愿意,您也可以使用变量。

name='nginx-1.0.0' # or $1, or whatever works for you
wget "http://nginx.org/download/$name.tar.gz"
tar -xf "$name.tar.gz"
./$name/configure

但老实说,最好的解决方案是在解压后 cd 到正确的目录,无论您使用 glob 还是变量作为目录名。

The directory name should be nginx-1.0.0 or whatever the tarball's name is without the .tar.gz. Try this after wget and tar:

cd nginx*
./configure
# etc

You could also use variables, if you like.

name='nginx-1.0.0' # or $1, or whatever works for you
wget "http://nginx.org/download/$name.tar.gz"
tar -xf "$name.tar.gz"
./$name/configure

Honestly, though, the best solution would be to cd into the proper directory after extracting, whether you use a glob or a variable for the directory name.

缱绻入梦 2024-11-09 04:16:50

这里给出的答案对于绝对路径来说效果不佳,因为它们将它们修剪到路径的第一个目录。如果您使用绝对路径创建了 tar 存档,则以下代码段将返回其原始根目录:

tar tf archive.tar | head -1

对于压缩 .tar.gz 存档,请添加选项 c。希望这会对其他人有所帮助。

The answers given here doesn't work well for absolute paths as they prune them to the first directory of the path. If you have created a tar archive with absolute paths, the following snippet would return its originally root:

tar tf archive.tar | head -1

For compresseed .tar.gz archives add the option c. Hope this would help some others.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文