- How to List Archive File Contents in TAR/TAR.GZ/TAR.BZ2
- List Archive File Contents (Quick Commands)
- List .tar File Content
- List .tar.gz File Content
- List .tar.bz2 File Content
- List .tar.xz File Content
- How to View the Content of a Tar File?
- Conclusion
- About the author
- Sam U
- Listing the content of a tar file or a directory only down to some level
- 6 Answers 6
How to List Archive File Contents in TAR/TAR.GZ/TAR.BZ2
While working with the archive files, sometimes you are required to list archive file contents instead of extract an archive file. Using this you can see the files available in an archive file. Read another tutorial with 18 Linux tar command examples
List Archive File Contents (Quick Commands)
The -t switch is used for list content of a tarball file without extract. Below is the quick commands used to list .tar, .tar.gz, .tar.bz2 and .tar.xz file contents.
tar -tvf archive.tar tar -ztvf archive.tar.gz tar -jtvf archive.tar.bz2 tar -Jtvf archive.tar.xz
List .tar File Content
Use -t switch with tar command to list content of a archive.tar file without actually extracting. You can see that output is pretty similar to the result of ls -l command.
drwxr-xr-x root/root 0 2018-01-12 11:11 backup/ drwxr-xr-x root/root 0 2018-01-12 11:09 backup/data/ -rw-r----- root/root 1058 2018-01-12 11:09 backup/data/config.ini -rw-r--r-- root/root 29 2018-01-12 11:11 backup/.htaccess -rw-r----- root/root 442 2018-01-12 11:08 backup/access.log -rw-r--r-- root/root 7 2018-01-12 11:09 backup/index.html lrwxrwxrwx root/root 0 2018-01-12 11:11 backup/config -> data/config.ini
List .tar.gz File Content
We use -z switch for handling .tar.gz files and use -t for the listing of archive file content. See below example to list an archive.tar.gz file contents without extracting the file.
drwxr-xr-x root/root 0 2018-01-12 11:11 html/ drwxr-xr-x root/root 0 2018-01-12 11:09 html/config/ -rw-r----- root/root 1058 2018-01-12 11:09 html/config/config.ini -rw-r--r-- root/root 29 2018-01-12 11:11 html/.htaccess -rw-r----- root/root 442488 2018-01-12 11:08 html/access.log -rw-r----- root/root 263636 2018-01-12 11:08 html/error.log -rw-r--r-- root/root 17 2018-01-12 11:09 html/index.html lrwxrwxrwx root/root 0 2018-01-12 11:11 html/config.ini -> config/config.ini
List .tar.bz2 File Content
We use -j switch for handling tar.bz2 files and use -t for the listing of archive file content. See below example to list an archive.tar.bz2 file contents without extracting the file.
drwxr-xr-x root/root 0 2018-01-12 11:11 www/ drwxr-xr-x root/root 0 2018-01-12 11:09 www/data/ -rw-r----- root/root 1994 2018-01-10 10:19 www/data/config.ini -rw-r--r-- root/root 29 2018-01-12 11:11 www/.htaccess -rw-r----- root/root 33442 2018-01-11 10:08 www/index.php lrwxrwxrwx root/root 0 2018-01-12 11:11 www/config -> data/config.ini
List .tar.xz File Content
We use -J (capital J) switch for handling tar.xz files and use -t for the listing of archive file content. See below example to list an archive.tar.xz file contents without extracting the file.
drwxr-xr-x root/root 0 2018-01-12 11:11 www/ drwxr-xr-x root/root 0 2018-01-12 11:09 www/data/ -rw-r----- root/root 1994 2018-01-10 10:19 www/data/config.ini -rw-r--r-- root/root 29 2018-01-12 11:11 www/.htaccess -rw-r----- root/root 33442 2018-01-11 10:08 www/index.php lrwxrwxrwx root/root 0 2018-01-12 11:11 www/config -> data/config.ini
How to View the Content of a Tar File?
Tar is a widely used utility to collect files and creating archives out of them. It was designed to create archives to store data on tapes, thus called “Tape ARchive.” The utility was first included in UNIX version 7 in 1979 and now available on multiple platforms.
Tar is a prominent Linux utility and comes with various functions such as creating archives, extracting them, extracting them to a specific directory, adding more files to the existing archive, etc. But can I view the content of the Tar file specifically while using the terminal? The answer is Yes! This multi-feature utility also allows viewing the content of the archived files, especially when working on the server and no graphical tool is available. Viewing the tar file content can also be quite handy when the file is quite large, and you only want to find a specific and extract it.
We have already discussed many features of the Tar utility. This guide focuses on a lesser-known feature of Tar utility and displays or listing the content of the tar file. So, let’s begin:
How to view the content of a tar file
You need to view the content of a tar file as it collects many files and ensures if a specific file is present. The syntax of the command to view the content of a tar file without extracting it is mentioned below:
- “-t”/ “–list”: Used to list the content of the tar file
- “-f”/ “–file”: Commanding the utility to use the file mentioned in the following argument
Moreover, you can use the “-v” flag or “–verbose” option to get detailed standard output. Let’s understand it by an example:
The long command would be:
In the above command, I am viewing the content of a “my_doc_file.tar” file.
How to view the content of tar.gz or tgz files
As it is known that a tar file does not compress the files, it simply collects. To compress it, we need another utility. A commonly used utility to compress tar files is “gzip.” The command to view the content of a “tar.gz” or “tgz” file is mentioned below:
The long command would be:
Where “-z” and “–gzip” options are used to handle the “tgz” or “tar.gz” files.
How to view the content of tar.bz, tar.bz2,tbz, or tbz2 files
The “tzip” is another utility for the compression of tar files. To view the content of files with extension “tar.bz”, “tar.bz2”, “tbz” or “tbz2”, follow the below-mentioned command:
And the long command would be:
The “-j” flag is used to handle “bzip” files.
How to view the content of tar.xz files
The “tar.xz” files are the tar files that are compressed using the “LZMA/LZMA2” algorithm. To view the content of files with “tar.xz” files, use:
The “-J” and “–xz” options are used to handle “tar.xz” files.
Conclusion
Tar is a feature-rich Linux utility that allows viewing the content of various tar archive files without extracting it. Regarding the content of a tar file in the terminal can be helpful in many situations. In this guide, we explore how to view the content of tar files and compressed tar files in the terminal using different flags and long commands. To get more about Tar utility, run “man tar” in the terminal.
About the author
Sam U
I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.
Listing the content of a tar file or a directory only down to some level
I wonder how to list the content of a tar file only down to some level? I understand tar tvf mytar.tar will list all files, but sometimes I would like to only see directories down to some level. Similarly, for the command ls , how do I control the level of subdirectories that will be displayed? By default, it will only show the direct subdirectories, but not go further.
@brennebeck: I doubt it; I was referring to the part about ls . Sorry for the confusion, my comment is not very clear.
6 Answers 6
I used the depth=1 solution and got two different results on archives that were made by specifying the current directory as the target. On one I got no out put at all. The other gave the output ./ . I don’t know why they were different.
I think this only works on GNU tar. On BSD tar (included in macOS) it only lists top level files and not directories. I think it has something to do with * being interpreted as 0 or more character and ends up excluding any entry with a / . To include files and directories, I was able to use the pattern «*/?*» , and for depth=2, «*/?*/?*» would work.
tar tvf scripts.tar | awk -F/ '' drwx------ glens/glens 0 2010-03-17 10:44 scripts/ -rwxr--r-- glens/www-data 1051 2009-07-27 10:42 scripts/my2cnf.pl -rwxr--r-- glens/www-data 359 2009-08-14 00:01 scripts/pastebin.sh -rwxr--r-- glens/www-data 566 2009-07-27 10:42 scripts/critic.pl -rwxr-xr-x glens/glens 981 2009-12-16 09:39 scripts/wiki_sys.pl -rwxr-xr-x glens/glens 3072 2009-07-28 10:25 scripts/blacklist_update.pl -rwxr--r-- glens/www-data 18418 2009-07-27 10:42 scripts/sysinfo.pl
Make sure to note, that the number is 3+ however many levels you want, because of the / in the username/group. If you just do
tar tf scripts.tar | awk -F/ '' scripts/ scripts/my2cnf.pl scripts/pastebin.sh scripts/critic.pl scripts/wiki_sys.pl scripts/blacklist_update.pl scripts/sysinfo.pl
You could probably pipe the output of ls -R to this awk script, and have the same effect.