Skip to content
Advertisement

How to list the folders/files of a file.tar.gz file inside a file.tar

I need to list the folder/files inside a certs.tar.gz which is inside file.tar without extracting them.

[root@git test]# tar -tf file.tar
./
./product/
./product/.git/
./product/.git/refs/
./product/.git/refs/heads/
./Release/add_or_modify.sh
./certs.tar.gz
[root@git test]#

Advertisement

Answer

You may want to use and condition:

tar -xf abc.tar "abc.tar.gz" && tar -ztvf abc.tar.gz

Explanation:

For listing of files we use

If file is of type tar.gz:

tar -ztvf file.tar.gz

If file is of type tar:

tar -tvf file.tar

If file is of type tar.bz2:

tar -jtvf file.tar.bz2

You can also search for files in any of the above commands. e.g:

tar -tvf file.tar.bz2 '*.txt'

For extracting files we use

tar -xf file.tar

In these commands,

  • t: List the contents of an archive.
  • v: Verbosely list files processed (display detailed information).
  • z: Filter the archive through gzip so that we can open compressed (decompress) .gz tar file.
  • j: Filter archive through bzip2, use to decompress .bz2 files.
  • f filename: Use archive file called filename.
  • x: Extract all files from given tar, but when passed with a filename will extract only matching files
Advertisement