thichxai
thichxai

Reputation: 1137

How to get file name and total count in zip (Linux)

How do I get a result with file names at the beginning and the total number of files? When I run the command line below, it only shows the number of files but not file names.

I would like to get the zip file name and the number of documents in zip. Thanks

Desired output:

IAD1.zip 30000 files
IAD2.zip 24000 files
IAD3.zip 32000 files
.....

Command line:

zipinfo IAD${count}.zip |grep ^-|wc -l >>TotalCount.txt

With the command above, the result shows the number of documents in zip files, but not file names:

30000
24000
32000
.....

Upvotes: 11

Views: 18703

Answers (2)

Digvijay S
Digvijay S

Reputation: 2705

zipinfo -h <file name> | tr '\n' ':' | awk -F':' '{print $2 , $5 , "files"}'

explanation:

  • zipinfo -h -- list header line. The archive name, actual size (in bytes) and total number of files is printed.

  • tr '\n' ':' -- Replace new line with ":"

  • awk -F':' '{print $2 , $5 , "files"}' -- Read file as ":" delimited and print 2nd and 5th field

Demo:

:>zipinfo test.zip 
Archive:  test.zip
Zip file size: 2798 bytes, number of entries: 7
-rw-r--r--  3.0 unx       18 tx stor 20-Mar-10 13:00 file1.dat
-rw-r--r--  3.0 unx       32 tx defN 20-Mar-10 13:00 file2.dat
-rw-r--r--  3.0 unx       16 tx stor 20-Mar-10 12:26 file3.dat
-rw-r--r--  3.0 unx     1073 tx defN 20-Mar-12 05:24 join1.txt
-rw-r--r--  3.0 unx      114 tx defN 20-Mar-12 05:25 join2.txt
-rw-r--r--  3.0 unx      254 tx defN 20-Mar-11 09:39 sample.txt
-rw-r--r--  3.0 unx     1323 bx stor 20-Mar-14 09:14 test,zip.zip
7 files, 2830 bytes uncompressed, 1746 bytes compressed:  38.3%
:>zipinfo -h test.zip | tr '\n' ':' | awk -F':' '{print $2 , $5 , "files"}'
  test.zip  7 files

Upvotes: 18

P_M
P_M

Reputation: 51

To list all the filenames in the ZIP file:

unzip -l <path/to/your/zipfile.zip>

This command lists all files in the ZIP archive along with some additional information (like file sizes and timestamps).

Use grep to get specific file Like .txt or .html

unzip -l <path/to/your/zipfile.zip> | grep html | wc -l

It's multiple extension files and you're only interested in the filenames and want to count them, you can use grep to filter and count the results

unzip -l <path/to/your/zipfile.zip> | grep -v '^Archive:' | grep -v '^--------' | grep -v '^ ' | grep -v ' files$' | wc -l

unzip -l path/to/your/zipfile.zip lists the contents of the ZIP file.
grep -v '^Archive:' removes the line starting with "Archive:".
grep -v '^--------' removes the separator lines.
grep -v '^ ' removes lines starting with a space, which are usually part of the summary.
grep -v ' files$' removes the final line that shows the total count and size (since we're counting them separately).
wc -l counts the number of lines, which corresponds to the number of files.

Upvotes: 0

Related Questions