• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

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

By admin

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 .

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL : Resize (extend) non-root EXT3/4 filesystem on non-LVM device (hard disk partition)
  2. CentOS / RHEL : How to delete LVM volume
  3. How to list or install only security updates with dnf in CentOS/RHEL 8
  4. How to Check the Size of the Yum Channels in Oracle Linux
  5. How to Provide Credentials From a File While Mounting CIFS Share in Linux
  6. Common Init.ora Parameters and Unix, Linux Kernel Parameters and Relationship Between Them
  7. How to Disable or set SELinux to Permissive mode
  8. CentOS / RHEL 6 : How to disable Transparent Huge pages (THP)
  9. CentOS / RHEL 7 : Tips on Troubleshooting NTP / chrony Issues
  10. Apache HTTP server – most commonly used containers (special configuration directives)

You May Also Like

Primary Sidebar

Recent Posts

  • Oracle Database – Configuring Secure Application Roles
  • Extend rule sets by using factors in Oracle Database Vault
  • What are Command Rules in oracle Database
  • Using Rule Sets in Oracle Database Vault
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary