bash 仅列出某个级别的 tar 文件或目录的内容

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

Listing the content of a tar file or a directory only down to some level

bashtarlsdepth

提问by Tim

I wonder how to list the content of a tar file only down to some level?

我想知道如何仅列出某个级别的 tar 文件的内容?

I understand tar tvf mytar.tarwill list all files, but sometimes I would like to only see directories down to some level.

我知道tar tvf mytar.tar会列出所有文件,但有时我只想看到某个级别的目录。

Similarly, for the command ls, how do I control the level of subdirectories that will be displayed? By default, it will only show the direct subdirectories, but not go further.

同样,对于 command ls,如何控制将显示的子目录的级别?默认情况下,它只会显示直接的子目录,但不会进一步显示。

采纳答案by Glen Solsberry

tar tvf scripts.tar | awk -F/ '{if (NF<4) print }'


drwx------ glens/glens       0 2010-03-17 10:44 scripts/
-rwxr--r-- glens/www-data 1051 2009-07-27 10:42 scripts/my2cnf.pl
-rwxr--r-- glens/www-data  359 2009-08-14 00:01 scripts/pastebin.sh
-rwxr--r-- glens/www-data  566 2009-07-27 10:42 scripts/critic.pl
-rwxr-xr-x glens/glens     981 2009-12-16 09:39 scripts/wiki_sys.pl
-rwxr-xr-x glens/glens    3072 2009-07-28 10:25 scripts/blacklist_update.pl
-rwxr--r-- glens/www-data 18418 2009-07-27 10:42 scripts/sysinfo.pl

Make sure to note, that the number is 3+ however many levels you want, because of the / in the username/group. If you just do

请务必注意,由于用户名/组中的 /,该数字是 3+,无论您想要多少级别。如果你只是这样做

tar tf scripts.tar | awk -F/ '{if (NF<3) print }'

scripts/
scripts/my2cnf.pl
scripts/pastebin.sh
scripts/critic.pl
scripts/wiki_sys.pl
scripts/blacklist_update.pl
scripts/sysinfo.pl

it's only two more.

只有两个了。

You could probably pipe the output of ls -Rto this awkscript, and have the same effect.

您可能可以将 的输出通过管道ls -R传输到此awk脚本,并具有相同的效果。

回答by sacapeao

depth=1

深度=1

tar --exclude="*/*" -tf file.tar


depth=2


深度=2

tar --exclude="*/*/*" -tf file.tar

回答by Sisyphus

Another option is archivemount. You mount it, and cd into it. Then you can do anything with it just as with other filesystem.

另一种选择是archivemount。你挂载它,然后 cd 进入它。然后你可以像使用其他文件系统一样使用它做任何事情。

$ archivemount /path/to/files.tgz /path/to/mnt/folder

It seems faster than the tar method.

它似乎比 tar 方法更快。

回答by leonbloy

It would be nice if we could tell the findcommand to look inside a tar file, but I doubt that is possible.

如果我们可以告诉find命令查看 tar 文件,那就太好了,但我怀疑这是可能的。

I quick and ugly (and not foolproof) way would be to limit the number of directory separators, for example:

我快速而丑陋(而不是万无一失)的方法是限制目录分隔符的数量,例如:

 $ tar tvf myfile.tar | grep -E '^[^/]*(/[^/]*){1,2}$'

The 2tells to display not more than 2 slashes (in my case one is already generated by the user/group separator), and hence, to display files at depth at most one. You might want to try with different numbers in place of the 2.

2告诉显示不超过2斜线(在我的情况下,一个已经由用户/组分离器产生的),因此,至多一个为显示深度的文件。您可能想尝试用不同的数字代替 2。

回答by ire_and_curses

I agree with leonbloy's answer- there's no way to do this straightforwardly within the tarball itself.

我同意leonbloy 的回答- 在 tarball 本身中无法直接做到这一点。

Regarding the second part of your question, lsdoes not have a max depth option. You can recurse everythingwith ls -R, but that's often not very useful.

关于您问题的第二部分,ls没有最大深度选项。你可以递归一切ls -R,但是这往往不是非常有用。

However you can do this with both findand tree. For example to list files and directories one level deep, you can do

但是,您可以同时使用find和来做到这一点tree。例如,要列出一层深的文件和目录,您可以执行以下操作

find -maxdepth 2

or

或者

tree -L 2

treealso has a -doption, which recursively lists directories, but not files, which I find much more useful than -L, in general.

tree还有一个-d选项,它递归地列出目录,但不列出文件,我发现它比-L一般情况下有用得多。