2

Is there a flag that I can pass to the zipinfo command to list the contents of an archive non-recursively?

For example, if the target archive contains three directories, which in turn contain five empty directories each, I don't want the command to return fifteen elements, but only the three "top" or "first-level" directories.

How can this be achieved?

Z0OM
  • 3,149
GPWR
  • 152

1 Answers1

2

To the zip file format, things are not "recursive". In there, there's simply a list of files, whose names contain slashes (or more generally, path separators).

So, there's really 15 entries "flat" in your zip: the 15 "leaf" directories. The directories above are implied, since they are part of the path of these.

So, to find all top-level directories, you have to go through all entries, and extract the first path component, and then remove duplicates.

zipinfo -1 | sed -n 's;\([^/]*\)/.*;\1;p' | sort -u

Couple of things to note

  • I don't know how zipinfo behaves when there's file names with line breaks in there
  • if you're shifting around files between machines, where you need things like permissions, sparsity, ownership… zip is not the right tool, as it can't do these things. Also, with tools like mksquashfs, you can just make archives that compress usually better, and can be directly mounted using udisksctl loop-setup -f archive.squashfs (and if necessary udisksctl mount /dev/loopN after) or just mkdir mntdir; sudo mount -o loop archive.squashfs mountdir, and then you can just use your usual file management (from ls to your favorite file browser) to inspect the contents at near-zero cost.
  • Very nice. I'll play around with it and see whether I can manage to also have it return the top files, and not just the top directories. Don't know if that's possible, though(?) I'm no a sed export. Thank you for your educative answer. I appreciate your time. – GPWR Jun 10 '23 at 22:46
  • 1
    you can remove the / after the match group \(…\) – Marcus Müller Jun 10 '23 at 23:06
  • Oh, I see. Very cool, thanks so much. – GPWR Jun 11 '23 at 00:13
  • 1
    zipinfo renders newlines as ^J same as with literal ^J. Use bsdtar -tf file.zip for a non-ambiguous output where newline is rendered as \n and backslash as \\. bsdtar -tf file.zip | LC_ALL=C sed -n 's;/.*;;p' | LC_ALL=C sort -u – Stéphane Chazelas Jun 11 '23 at 06:08