How to tar, untar files and view contents of tar file under Linux

The tar command is useful for bundling up multiple files and/or directories. In a sense, it’s similar to the zip command. However, zip files are compressed by definition; tar files can be compressed, but don’t have to be.

Creating a tar file

In the examples to follow, the following file structure is used: a top level directory called /var/tmp/test containing the files file1, file2 and file3.

# ls -lR /var/tmp/test
/var/tmp/test:
total 12
-rw-r--r-- 1 root root 2277 Jan  4 09:57 file1
-rw-r--r-- 1 root root 1611 Jan  4 09:58 file2
-rw-r--r-- 1 root root 1456 Jan  4 09:58 file3

1. Assuming you are in the /var/tmp/test directory already, to create a tar file of all the files in the test directory, issue the below command.

# tar cvf test.tar .
./
tar: ./test.tar: file is the archive; not dumped
./file1
./file3
./file2

Here,
c – create (an archive)
v – verbose (just because)
f – filename (the name of our tar file)
. – current directory

Note: Also worth mentioning is that by default tar is recursive. Meaning it will back up all files and subdirectories recursively unless you otherwise specify with the n flag (non-recursive)

Displaying the Contents of a tar file

The current directory will now contain a file called test.tar. To display the contents of the tar file, we could issue this command:

# tar tvf test.tar
drwxr-xr-x root/root         0 2018-01-04 10:01 ./
-rw-r--r-- root/root      2277 2018-01-04 09:57 ./file1
-rw-r--r-- root/root      1456 2018-01-04 09:58 ./file3
-rw-r--r-- root/root      1611 2018-01-04 09:58 ./file2

Here,
t – table of contents (list)
v – verbose (display all info)
f – filename (test.tar)

Extracting the complete tar file

To extract the entire contents of the tar file to the current directory, we can type:

# tar xvf test.tar 
./
./file1
./file3
./file2

Here,
x – extract
v – verbose
f – filename (test.tar)

Extracting specific files from the tar file

You can also extract a specific file from the tar file instead of extracting the entire tar file. To extract only the file1 file from the archive, type the following command:

tar xvf test.tar file1

Using compression with tar

If you would also like to add compression to your tar files, you can combine the gzip utility with tar on the command line by adding the z switch to the command. Usually when this is done, we change the suffix of our tar filename from .tar to either .tgz or .tar.gz. This will let whoever sees the file know that it is a gzipped tar file.

# tar zcvf test.tgz .
./
./file1
./file3
./file2
tar: .: file changed as we read it
# ls -lrt
total 16
-rw-r--r-- 1 root root 2277 Jan  4 09:57 file1
-rw-r--r-- 1 root root 1611 Jan  4 09:58 file2
-rw-r--r-- 1 root root 1456 Jan  4 09:58 file3
-rw-r--r-- 1 root root 1639 Jan  4 11:27 test.tgz

Here,
z – gzip compression
c – create
v – verbose
f – filename (test.tgz)
. – current directory

The tar command knows about another compression algorithm called bzip2. To use bzip2 instead of gzip, replace the ‘z‘ in the above command with a ‘j‘ For example:

# tar jcvf test.bz2 .

For extracting the compressed files, include the options ‘z’ and ‘j’. For example,

# tar xzvf test.tgz
# tar xjvf test.bz2

Preserving Permissions with tar

If you would like to preserve the permissions of the files you backup, use the p option with the tar command. This will save the uid, gid as well as the specific permission attributes of the files (read, write, execute etc.)

# tar pcvf test.tar .

While extracting the above tar file, you would have to use the p option again if you want to extract the files with the preserved file permissions. For example :

# tar pxvf test.tar .
Related Post