Tar Command Complete Tutorial

TAR Command in Linux and Unix with Examples

When you are working in Linux and Unix systems. One of the most useful for compressing and archiving files and directories command is TAR.

Tar Command came from  Tape Archive. It also called as tarball.

In windows, you are doing zipping the files and directories using winzip or other softwares. In Unix and Linux system you can use TAR Command.

Tar Command in Linux

What is Archive files?

A archive file is a file which consists of multiple files, and some information about files which called as metadata. An archive file can have one file or multiple files in it. 

Why do you create Archive files?

Archive files have less size as compared to original files, they are compressed, Easily portable , we can send the files in emails, for big files it is very usefule. Also some confidental data can send with encryption. 

What is Tar Command?

TAR saves many files together into a single tape or disk archive, and can
restore individual files from the archive.Tar Command stands for Tape Archive. It is used to create the archive files and directories in Linux systems. Also decompression or untar of files can be done with Tar command using different Options.

Tar Command Syntax

tar [OPTION…] [FILE]…

Tar Command Important Options

  • -c :Create a new tar archive file.
  • -x: Extracting the archive file into a file system.
  • -f: This option will create archive of files of a given file name.
  • -r: with -r option you can add or update the file in the existing .ta file. 
  • -t: It will display the list of files under .tar file. 
  • -z: This option tell the tar command to zip the file using gzip
  • -j: filter the archive through bzip2.
  • -A: with this option you can append tar files to an archive
  • -d: find differences between archive and file system
  • -u: only append files newer than copy in archive
  • -k: This option is used for keep old files while extracting.
  • -m: don’t extract file modified time
  • -p: extract information about file permissions
  • -s: Used for same order of files, member arguments are listed in the same order as
    the files in the archive
  •  -W: for verify a file

Tar Command Examples:

Example 1: Create archive file using Tar Command

The option, here we need to use is -cvf, c for creating new archive file, f for giving file name and v for verbosely list files processed.

$ tar cvf thinkheight.tar thinkheights

OUTPUT: It will create the thinkheight.tar file  and archive the the directory thinkheights and subdirectories of it.

thinkheights/
thinkheights/test/

Example 2: Extract files using Tar Command

for extracting the files and directories from .tar file, we need to use option xvf

$ tar xvf thinkheight.tar 

OUTPUT: It will extract all the files, directories and subdirectories from thinkheight.tar file  

$ tar xvf thinkheight.tar
thinkheights/
thinkheights/test/

Example 3: Create gzip Archive file using Tar Command

gzip is file format which is used for compression and decompression, We can create gzip archive file using tar command, adding option z

$ tar cvzf thinkheights.tar.gz thinkheights

OUTPUT: It will archive the all the files, directories and subdirectories in gz format.  

$ tar cvzf thinkheights.tar.gz thinkheights
thinkheights/
thinkheights/test/

Example 4: Extract gzip Archive file using Tar Command

You need to just replace the c with x from above command to extract the gzip file.

$ tar xvzf thinkheights.tar.gz 

OUTPUT: It will extract all the files, directories and sub-directories into .tar.gz file.  

$ tar xvzf thinkheights.tar.gz
thinkheights/
thinkheights/test/

Example 5: Create bz2 Archive file using Tar Command

bz2  file is different format than gzip, which is also used for reduce the size of files. It creates archive files less than the size of gzip. Here we should use the option -j.

$ tar xvjf thinkheights.tar.bz2 thinkheights

OUTPUT: It archives all the files, directories and sub-directories into .tar.bz2 file. 

$ tar cvjf thinkheights.tar.bz2 thinkheights
thinkheights/
thinkheights/test/

Example 6: Extract bz2 Archive file using Tar Command

Just use xvjf instead of cvjf in above command. to extract files from bz2 format files.

$ tar xvjf thinkheights.tar.bz2 

OUTPUT: It archives all the files, directories and sub-directories from .tar.bz2 file. 

$ tar xvjf thinkheights.tar.bz2 
thinkheights/
thinkheights/test/

Note: with -j option, apart from bz2 format, you can create and extract other file formats like tbz, lzip, lzma, lzop and many others

Example 7: Untar single Tar file from .tar file

when you use x option, It will extract and untar all files. If you have to untar only 1 file, you have to specify the file name.  or you can use -C option and give the path name

$ tar xvf file_name.tar file_name1
or
$ tar xvf file_name.tar -C path of file

 

Example 8: Untar Multiple Tar file from .tar, .bz2, .gz file formats

 If you have to untar multiple files not to extract all files. you have to specify the file names. or you can use -C option and give the path name like below. It will work for all file formats .tar.gz, .tar and others

$ tar xvf file_name.tar “fileA” “fileB”
or
$ tar zxvf file_name1.tar.gz “fileA” “fileB”
or
$ tar jxvf file2.tar.bz2 “fileA” “fileB”

Example 9: List details archive tar file

For listing the details of tar file, We have mentioned in above section that -t should used. You should use below command to list the details

$ tar tvf thinkheight.tar

OUTPUT: It will list all details of archived files in .tar files. 

$ tar tvf thinkheight.tar
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/test/

Example 10: List details archive tar.gz file

For listing the details of tar.gz file, You should use option t.

$ tar tvf thinkheight.tar.gz

OUTPUT: It will list all details of archived files in .tar.gz files. 

$ tar tvf thinkheight.tar.gz
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/test/

Example 11: List details archive tar.bz2 file

For listing the details of tar.bz2 file, You should use option t.

$ tar tvf thinkheight.tar.bz2

OUTPUT: It will list all details of archived files in .tar.bz2 files. 

$ tar tvf thinkheight.tar.bz2
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/
drwxr-xr-x 91989/197609 0 2020-07-25 18:31 thinkheights/test/

Example 12: Update the .tar archive

You can add another file in .tar archived file using -r option.

$ tar rvf thinkheight.tar log3.txt

OUTPUT: It will add the file to existing archive file

$ tar rvf thinkheight.tar log3.txt
log3.txt

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.