Tar –list only top level files and folders

greposxtar

using tar --list --file=x will list all files and folders.
I am just looking to list the top level files and folders

does anyone know how to do that?

alternatively, does anyone know how to list only the top level files, but all folders including subfolders? Maybe with grep somehow?

update: looking for something that works on most nix flavors including MacOS.

Best Answer

The following method works for me to show only the top level files and directory names,

tar --exclude='./*/*' -tvf ttt.tar.gz

The following method shows one more level,

tar --exclude='./*/*/*' -tvf ttt.tar.gz

Directories are listed with a trailing slash.

If the list does not start with a dot or slash, you should use another pattern, for example

tar --exclude='*/*' -tvf ttt.tar.gz

for only the top level directory and

tar --exclude='*/*/*' -tvf ttt.tar.gz

for one more level.

Related Question