Alex Rivera | Logout

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

Asked 2010-04-23T16:36:55.610
95

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

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

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.

Edit
Report

2 Answers

211

depth=1

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


depth=2

tar --exclude="*/*/*" -tf file.tar
answered 2011-11-14T01:33:36.273
6

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

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 2 tells 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.

answered 2010-04-23T17:27:55.493

Your Answer