28

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.

1

1 Answer 1

34

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.

4
  • 2
    On tar 1.30+ this didn't work. I had to use --exclude='*/*' for top level directories. Commented Jun 13, 2023 at 10:32
  • @TejasPendse, If I understand correctly, this is what I describe in the second half of the answer: "If the list does not start with a dot or slash, you should use another pattern, for example tar --exclude='*/*' -tvf ttt.tar.gz". (I have tar (GNU tar) 1.34 now.)
    – sudodus
    Commented Jun 13, 2023 at 12:12
  • Works, but is slow for large tar files since it has to apply the exclusion filter on all files in the archive file. :(
    – reubenjohn
    Commented Jul 11, 2023 at 1:56
  • 1
    I found this more cleaner, tar -tf archive.tar | awk -F/ '{print $1}' | uniq Commented Aug 9, 2023 at 10:55

You must log in to answer this question.

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