List tar files in linux

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.

Читайте также:  Tape device in linux

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.

Источник

How to List the Content of a Tar File in Linux

list the content of a tar file1

Tar is a computer software utility for collecting many files into one archive file, often referred to as a tarball, for distribution or backup purposes. The name is derived from (t)ape (ar)chive, as it was originally developed to write data to sequential I/O devices with no file system of their own.
GNU tar is an archiving program designed to store multiple files in a single file (an archive), and to manipulate such archives. The archive can be either a regular file or a device (e.g. a tape drive, hence the name of the program, which stands for tape archiver), which can be located either on the local or on a remote machine.

Tar command options:

-t: List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.
-v: Verbosely list files processed.
-z: Filter the archive through gzip
-j, –bzip2: Filter the archive through bzip2(1).
-J, –xz: Filter the archive through xz(1).

List Content of Tar File without Extracting it

If you want only list or view the contents of a tarball file without extracting it, and you need to use tar command with -t option. For example, you want to List content of a Tar archive file called mytest.tar, and you can issue the following command to list archive file contents. Type:

devops@devops:~/tmp$ tar -tvf mytest.tar -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.gz File

If you want to list the contents of a tar.gz archive file, and you can use tar command with -zt options. Type:

devops@devops:~/tmp$ tar -ztvf mytest.tar.gz -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.bz2 File

If you want to list the contents of a tar.bz2 file in your Linux system, and you can run the following tar command with -jt option from the command line:

devops@devops:~/tmp$ tar -jtvf mytest.tar.bz2 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

List the Contents of a Tar.xz File

If you have a archive file with tar.xz extension format, and you want to list the contents of this tar.xz archive file only, and you can use tar command with -Jt option from command line on your Linux system, type:

devops@devops:~/tmp$ tar -Jtvf mytest.tar.xz -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test2 -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test3

Searching for Specific Files from a Tar file

If you only want to search for specific files ‘test1’ from a given tar file called mytest.tar, and you can issue the following command from command line:

devops@devops:~/tmp$ tar -tvf mytest.tar 'test1' -rw-rw-r-- devops/devops 0 2019-09-07 04:10 test1

Conclusion

You should know that how to List or display only the contents of tar, tar.gz, tar.xz or tar.bz2 file on your CentOS or RHEL or Ubuntu Linux operating system or Unix system.

Читайте также:  Удаленная поддержка пользователей linux

Источник

How can I view the contents of tar.gz file without extracting from the command-line?

I want to see the contents (list of files and folders) of an archive, for example a tar.gz file without extracting it. Are there any methods for doing that?

11 Answers 11

Run the below command in the terminal to see the contents of a tar.gz file without extracting it:

screenshot of listing compressed tarball files in the terminal

-t, —list
List the contents of an archive. Arguments are optional. When given, they specify the names of the members to list.

-f, —file=ARCHIVE Use archive file or device ARCHIVE.

does all these examples to look inside compressed file works on other types of format too. like zip , rar , tar etc. ?

@blockloop um, unless I’m a complete idiot, that’s just going to list the contents of the current directory — not the tarfile. The manpage doesn’t mention standard-input at all; and this reference seems to match my findings. (Tree 1.7.0.)

When you open de file with vim ( vim file.tar.gz ) it says «Select a file with cursor and press ENTER». You do just that, move the cursor over a file and press ENTER.

less can also open gz -compressed and uncompressed tar archives. It gives you a lovely ls -l style output too:

$ less ~/src/compiz_0.9.7.8-0ubuntu1.6.debian.tar.gz drwxrwxr-x 0/0 0 2012-09-21 11:41 debian/ drwxrwxr-x 0/0 0 2012-08-09 13:32 debian/source/ -rw-rw-r-- 0/0 12 2012-08-09 13:32 debian/source/format -rw-rw-r-- 0/0 25 2012-08-09 13:32 debian/libdecoration0-dev.docs -rw-rw-r-- 0/0 25 2012-08-09 13:32 debian/compiz-dev.docs -rw-rw-r-- 0/0 347 2012-08-09 13:32 debian/compiz-core.install -rw-rw-r-- 0/0 125 2012-08-09 13:32 debian/libdecoration0-dev.install . 

And because it’s less , you can scroll through it, search it, etc. However it fails miserably with other compression algorithms (in my experience).

You sure you don’t have an alias with special options for less that you’re not showing here? I just tried that to see, but it didn’t work. I don’t have any aliases setup for less .

My .bashrc contains [ -x /usr/bin/lesspipe ] && eval «$(SHELL=/bin/sh lesspipe)» which makes this work as described.

You could use the z command: zcat , zless , zgrep .

To view a files content use:

To check difference between files use:

These are just a few example, there are many more.

Well, that depends on the file. Most (de)compression programs have a flag that lists an archive’s contents.

Читайте также:  Linux выбрать интерпретатор python

    tar / tar.gz / tgz / tar.xz / tar.bz2 / tbz files

$ tar tf foo.tgz dir1/ dir1/subdir1/ dir1/subdir1/file dir1/subdir2/ dir1/subdir2/file dir2/ 
$ zip -sf foo.zip Archive contains: dir1/ dir2/ dir1/subdir1/ dir1/subdir1/file dir1/subdir2/ dir1/subdir2/file Total 6 entries (0 bytes) 
$ 7z l foo.7z 7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18 p7zip Version 9.20 (locale=en_US.utf8,Utf16=on,HugeFiles=on,4 CPUs) Listing archive: foo.7z -- Path = foo.7z Type = 7z Solid = - Blocks = 0 Physical Size = 168 Headers Size = 168 Date Time Attr Size Compressed Name ------------------- ----- ------------ ------------ ------------------------ 2015-03-30 19:00:07 . A 0 0 dir1/subdir1/file 2015-03-30 19:00:07 . A 0 0 dir1/subdir2/file 2015-03-30 19:07:32 D. 0 0 dir2 2015-03-30 19:00:07 D. 0 0 dir1/subdir2 2015-03-30 19:00:07 D. 0 0 dir1/subdir1 2015-03-30 19:00:06 D. 0 0 dir1 ------------------- ----- ------------ ------------ ------------------------ 0 0 2 files, 4 folders 
 $ rar v foo.rar RAR 4.20 Copyright (c) 1993-2012 Alexander Roshal 9 Jun 2012 Trial version Type RAR -? for help Archive foo.rar Pathname/Comment Size Packed Ratio Date Time Attr CRC Meth Ver ------------------------------------------------------------------------------- dir1/subdir1/file 0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9 dir1/subdir2/file 0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9 dir1/subdir1 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir1/subdir2 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir1 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir2 0 0 0% 30-03-15 19:07 drwxr-xr-x 00000000 m0 2.0 ------------------------------------------------------------------------------- 6 0 16 0% 

That’s most of the more popular archive formats. With all this in mind, you could write a little script that uses the appropriate command depending on the extension of the file you give to it:

#!/usr/bin/env bash for file in "$@" do printf "\n-----\nArchive '%s'\n-----\n" "$file" ## Get the file's extension ext=$ ## Special case for compressed tar files. They sometimes ## have extensions like tar.bz2 or tar.gz etc. [[ "$(basename "$file" ."$ext")" =~ \.tar$ ]] && ext="tgz" case $ext in 7z) type 7z >/dev/null 2>&1 && 7z l "$file" || echo "ERROR: no 7z program installed" ;; tar|tbz|tgz) type tar >/dev/null 2>&1 && tar tf "$file"|| echo "ERROR: no tar program installed" ;; rar) type rar >/dev/null 2>&1 && rar v "$file"|| echo "ERROR: no rar program installed" ;; zip) type zip >/dev/null 2>&1 && zip -sf "$file"|| echo "ERROR: no zip program installed" ;; *) echo "Unknown extension: '$ext', skipping. " ;; esac done 

Save that script in your PATH and make it executable. You can then list the contents of any archive:

$ list_archive.sh foo.rar foo.tar.bz foo.tar.gz foo.tbz foo.zip ----- Archive 'foo.rar' ----- RAR 4.20 Copyright (c) 1993-2012 Alexander Roshal 9 Jun 2012 Trial version Type RAR -? for help Archive foo.rar Pathname/Comment Size Packed Ratio Date Time Attr CRC Meth Ver ------------------------------------------------------------------------------- dir1/subdir1/file 0 8 0% 30-03-15 19:00 -rw-r--r-- 00000000 m3b 2.9 dir1/file 0 8 0% 30-03-15 19:29 -rw-r--r-- 00000000 m3b 2.9 dir1/subdir1 0 0 0% 30-03-15 19:00 drwxr-xr-x 00000000 m0 2.0 dir1 0 0 0% 30-03-15 19:29 drwxr-xr-x 00000000 m0 2.0 dir2 0 0 0% 30-03-15 19:07 drwxr-xr-x 00000000 m0 2.0 ------------------------------------------------------------------------------- 5 0 16 0% ----- Archive 'foo.tar.bz' ----- dir1/ dir1/subdir1/ dir1/subdir1/file dir1/file dir2/ ----- Archive 'foo.tar.gz' ----- dir1/ dir1/subdir1/ dir1/subdir1/file dir1/file dir2/ ----- Archive 'foo.tbz' ----- dir1/ dir1/subdir1/ dir1/subdir1/file dir1/file dir2/ ----- Archive 'foo.zip' ----- Archive contains: dir1/ dir1/subdir1/ dir1/subdir1/file dir1/file dir2/ Total 5 entries (0 bytes) 

And since someone mentioned that lesser editor, naturally, emacs can also do this:

Источник

Оцените статью
Adblock
detector