How to count the number of files in a tar archive?

So you have a tar archive file and you want to know the number of files in it without expanding it. You know the text counter program ‘wc’? You can count the number of lines from the input by:

wc -l file.txt

You know how to list the content (files and directories) of a tar archive?

tar --list example.tar

Now you just combine these two to count the number of lines of the files list of the tar archive:

tar --list example.tar | wc -l

So that you have the number of files in the tar file example.tar. The pipe means relaying the output of the previous command to the next command as input.