user3576287
user3576287

Reputation: 1032

How to count the number of files inside a tar.gz file (without decompressing)?

I have a tar.gz file which I have made by pigz (parallel gzip). I wanted to count the number of files inside the compressed file without decompressing it.

I use this command:

tar -tzf file.tar.gz

but I got an error:

tar: This does not look like a tar archive
tar: Skipping to next header

Is it because I used pigz instead of gzip? If yes how can I count the them now?

Upvotes: 30

Views: 52831

Answers (3)

alecxs
alecxs

Reputation: 741

you can use the tar -vv verbose option twice for full verbose, then grep the first character from file permissions. the ^ means only match first character (begin of line). the grep -c option count the lines.

drwxrwx--x   directory
lrwxrwxrwx   symlink
-rw-rw----   file

count regular files only

gzip -cd file.tar.gz | tar -tvv | grep -c ^-

Upvotes: 8

user3576287
user3576287

Reputation: 1032

I found the solution!

I used unpigz for those files and it changed the file extensions to .tar . after that I could use tar -tzf without any problems.

Thanks!

Upvotes: 0

jdiver
jdiver

Reputation: 2393

Since it is a tar and gzip archive you should use z option to use gzip. Then simply you can count lines with wc.

tar -tzf file.tar.gz | wc -l

Upvotes: 43

Related Questions