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?

  • Do you have archivemount installed? – Mark Plotnick Dec 27 '18 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? – Aurelie Navir Dec 28 '18 at 0:39
  • It's probably a bit slower than tar tvf. How big are your tar files? – Mark Plotnick Dec 28 '18 at 1:54

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.