I'm looking for a terminal command to count the number of top-level items in a ZIP archive. I know that zip -l archive.zip
will show the file count, but this shows all files, not just top-level items. If archive.zip
will unzip to the following (* indicates top-level)
* Dir1
File1
File2
* Dir2
File3
File4
File5
File6
* Dir3
* File7
* File8
then I would like a program to output 5
.
unzip -l archive.zip|grep /|grep -v "/.*/"|wc -l
. The first filter shows only the lines with files, the second eliminates subdirectories and the third returns the line count. If the archive has absolute paths, you'll need to allow two slashes and eliminate three or more. – AFH Mar 14 '17 at 16:25unzip -l archive.zip | grep -v / | wc -l
, right? Sorry that my original post wasn't clear – BallpointBen Mar 14 '17 at 16:50|wc -l
you'll see the files considered, and it's the top-level ones. There is a bug(!): if the archive path contains a single/
this will be counted, so you need an extra filter to exclude it, eg replacegrep /
bygrep "^ .*/"
. – AFH Mar 14 '17 at 17:09