从tar提取1个文件。广州与bash

时间:2021-08-15 22:52:50

Is it possible to programmatically pull a single file from a decently sized .tar.gz without extracting the entire tarball to disk? Essentially I need to get inside large tar.gz files over the network and extract 1 small text file. It seems somewhat over-the-top to pull and extract the tarball to disk, then pull the file out, then delete everything else. Also I'm going to be doing this recursively (e.g. package dependencies, each text file points to more tar.gz's), so the less network traffic and cpu cycles I can get away with, the better.

是否可以通过编程方式从大小合适的.tar中提取一个文件。没有将整个tarball提取到磁盘上的gz ?本质上,我需要进入大的焦油。gz文件通过网络提取1个小文本文件。将tarball提取到磁盘,然后将文件取出,然后删除其他所有内容,这看起来有点夸张。另外,我还将递归地执行这个操作(例如,包依赖项,每个文本文件指向更多的tar.gz),这样网络流量和cpu周期越少越好。

2 个解决方案

#1


21  

From the man page, to extract blah.txt from foo.tar.gz:

从man页面中,提取出blah。从foo.tar.gz txt:

tar -xzf foo.tar.gz blah.txt

(And this goes on superuser, of course, but hey, prompt answers are nice too.)

(当然,这也适用于超级用户,不过,快速的回答也不错。)

#2


4  

I echo Jefromi's answer, with the addition of including the path to the file if you have directories in the tar file (this may seem obvious to some, but it wasn't initially clear to me how to specify the directory structure).

我重复了Jefromi的回答,如果您在tar文件中有目录,那么可以添加到文件的路径(这对某些人来说可能很明显,但我最初并不清楚如何指定目录结构)。

For example, if you did the tar at the src/ directory, and blah.txt was under release1/shared/, you would go back to the src/ directory (if you want it untarred at the same place)

例如,如果您在src/目录中执行tar,等等。txt在release1/shared/下,您可以回到src/目录(如果您希望它在相同的位置上无标记)

tar -xzf tar.gz release1/shared/blah.txt

If you don't remember the directory structure of your tar file (I'm a little disorganized and sometimes forget where I did the tar), you can always

如果您不记得您的tar文件的目录结构(我有点混乱,有时会忘记我在哪里做了tar),那么您总是可以这样做的

tar -tzf tar.gz

to see the contents, canceling out (Ctrl+C) once you get an idea of your directory structure.

要查看内容,请在了解目录结构后取消(Ctrl+C)。

#1


21  

From the man page, to extract blah.txt from foo.tar.gz:

从man页面中,提取出blah。从foo.tar.gz txt:

tar -xzf foo.tar.gz blah.txt

(And this goes on superuser, of course, but hey, prompt answers are nice too.)

(当然,这也适用于超级用户,不过,快速的回答也不错。)

#2


4  

I echo Jefromi's answer, with the addition of including the path to the file if you have directories in the tar file (this may seem obvious to some, but it wasn't initially clear to me how to specify the directory structure).

我重复了Jefromi的回答,如果您在tar文件中有目录,那么可以添加到文件的路径(这对某些人来说可能很明显,但我最初并不清楚如何指定目录结构)。

For example, if you did the tar at the src/ directory, and blah.txt was under release1/shared/, you would go back to the src/ directory (if you want it untarred at the same place)

例如,如果您在src/目录中执行tar,等等。txt在release1/shared/下,您可以回到src/目录(如果您希望它在相同的位置上无标记)

tar -xzf tar.gz release1/shared/blah.txt

If you don't remember the directory structure of your tar file (I'm a little disorganized and sometimes forget where I did the tar), you can always

如果您不记得您的tar文件的目录结构(我有点混乱,有时会忘记我在哪里做了tar),那么您总是可以这样做的

tar -tzf tar.gz

to see the contents, canceling out (Ctrl+C) once you get an idea of your directory structure.

要查看内容,请在了解目录结构后取消(Ctrl+C)。