3

I would like to see the tree of a big compressed file (specifically only the second level of directories) so I used the following command: tar -tf tarfile | tree -L 2

But it outputs the tree of the directory I am in, not of the compressed file. The other commands work fine, for example if I do: tar -tf tarfile | less It lets me explore correctly the tarfile.

Am I doing something wrong or I can't use tree like other commands trough pipping? If not, is there any other way to only see the files till second level directories of a compressed file?

4
  • Do you have archivemount installed? Commented Dec 27, 2018 at 23:51
  • No, I just read about the command. But from what I understand, the command is used to mount the tarFile into a mount point. That would take a lot of time if the tar is too big right? Or maybe I have to use it some specific options only to show the files till some level? Commented Dec 28, 2018 at 0:39
  • It's probably a bit slower than tar tvf. How big are your tar files? Commented Dec 28, 2018 at 1:54
  • If you don't mind creating a temp file, try with file="/tmp/t1"; tar -tf tarfile > "${file}" && tree --fromfile "${file}" && rm "${file}". Noticed L and --fromfile are not working together in MacOS. Commented Aug 30, 2021 at 5:25

2 Answers 2

6

tree can accept a list of files from standard input via the --fromfile argument.

Reads a directory listing from a file rather than the file-system. Paths provided on the command line are files to read from rather than directories to search. The dot (.) directory indicates that tree should read paths from standard input.

So you may want to try something like:

tar -tf tarfile | tree --fromfile .

But sadly, the -L argument doesn't work when you read from standard input, at least on my machine.

3
  • @Peregrino69 the question was specifically about piping, and I mentioned standard input because the intented solution was tar -tf file.tar | tree --fromfile . I should probably clarify this. Commented Oct 2, 2021 at 17:58
  • This works, you're right. I misunderstood your purpose earlier. My apologies, I removed my comment. Commented Oct 2, 2021 at 18:20
  • 1
    Amazing! I had been searching for this capability for a long time, unaware that tree itself supports it.
    – xeruf
    Commented Nov 7, 2021 at 2:26
5

Since tree does not read stdin, but instead traverses the actual directory structure (whether the current directory or the specified directories), you would need to post-process the table of contents of the tar file. If the tar file was created with full/absolute path names, you'll need to adjust the ranges to find your desired directory depth.

One option is awk:

tar -tf tarfile | awk -F/ 'NF == 3'

Another is cut (sorting uniquely so that child directories beyond level 2 don't cause the parent to be reported again):

tar -tf tarfile | cut -d/ -f1-3 | sort -u

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .