Linux Administrator

Most Useful Tar Command Examples

Tar stands for tape archive in Linux this command often used to create .tar.gz or .tgz archive files. It is also called “tarballs” .There are lots of options are available with this command, but you just need to remember a few options to create archives with tar. Using tar command you can also extract the archive.

GNU tar command included with Linux distributions has integrated compression. It can make a .tar archive and after that compress it with gzip or bzip2 compression in a single command. That is the reason the subsequent file is a .tar.gz file or .tar.bz2 file.

Create A tar File

Follow the below command to create on tar file called “files.tar”.

# tar -cvf files.tar file1.txt file2.txt file3.txt
OR
# tar -cvf files.tar *.txt

Where,
c : Create a .tar archive file
v – In verbose mode to show the .tar file process
f – File name type of the archive file

Content Listing Of tar File

Using -t options we can see the all contents of the tar file such as user info, size, permission, date and time etc.

# tar -tvf files.tar

Extract/Untar tar File

Follow the below command to extract the tar file:

# tar -xvf files.tar

In the above command tar file will extract in the current directory. If you want to extract tar file on different directory follow the below command.

# tar -xvf files.tar -C /different/directory/

Where,
x : Used for extract or untar files
C : Used to extract or untar files in another directory

Create tar.gz File

Using z option with tar command you can create a compressed gzip archive file.

# tar -zcvf files.tar.gz file1.txt file2.txt file3.txt

Where,
z : Used to compress tar with .gz extension
c : Create a .tar archive file
v : In verbose mode to show the .tar file process
f : File name type of the archive file

Create tar.bz2 File

While gzip compression is most frequently used to create .tar.gz or .tgz files, tar also supports bzip2 compression. This allows you to create bzip2-compressed files, often named .tar.bz2, .tar.bz, or .tbz files. To do so, just replace the -z for gzip in the commands here with a -j for bzip2.

# tar -jcvf files.tar.bz2 file1.txt file2.txt file3.txt

Where,
j : Used to compressed tar with .bz2 extension
c : Create a .tar archive file
v – In verbose mode to show the .tar file process
f – File name type of the archive file

Uncompress tar.gz File

Follow the below command to uncompress the tar.gz file:

# tar -zxvf files.tar.gz

Where,
x : Used for extract or untar files
v – In verbose mode to show the .tar file process
f – File name type of the archive file

Uncompress tar.bz2 file

Follow the below command to uncompress the tar.bz2 file:

# tar -jxvf files.tar.bz2

Content Listing Of tar.gz File

Type the below command to list the contents of tar.gz file using -t option:

# tar -tvf files.tar.gz

Content Listing Of tar.bz2 File

Type the below command to list the contents of tar.gz file using -t option:

# tar -tvf files.tar.bz2

Extract Single File From tarball or tar File

You can also extract one single file from tarball, you need to just specify the file name which you want to extract from the tarball.

# tar -zxvf files.tar.gz file2.txt
OR
# tar -jxvf files.tar.bz2 file2.txt

Extract Multiple File With Same Extension Using Wildcards

Sometime we need bunch of files only from compressed file so we used here –wildcards flag and then gives the astric with file extension.

# tar -jxvf files.tar.bz2 --wildcards ".txt"

Add New File/Directory Into tar File

Type the below command to add new file/directory to existing tar file using –r (append) option.

# tar -rvf files.tar newfile.txt

Some tar Command Options:

  • c – create a archive file.
  • x – extract a archive file.
  • v – show the progress of archive file.
  • f – filename of archive file.
  • t – viewing content of archive file.
  • j – filter archive through bzip2.
  • z – filter archive through gzip.
  • r – append or update files or directories to existing archive file.
  • W – Verify a archive file.
  • wildcards – Specify patters in UNIX tar command.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment