Tar command and Syntax Explained with Examples

This tutorial explains how to use tar command in Linux step by step with practical examples. Learn how to create tar file, extract single or multiple files from tar archive, list files from tar archive, add new file in tar archive, remove single or multiple files from tar archive and compress files with tar in detail.

What is tar

tar in linux stand for tap archive. tar was originally developed to write data to sequential I/O devices for backups on magnetic tape. tar is the file format and the name of program used to handle such files. Linux system administrator use tar to collect many files into one larger file for distribution or archiving, while preserving file system information like user and group permissions, dates, and directory structures etc.

tar file

tar file is a archive created from tar command. tar file is a single file that contains any number of individual files plus information to allow them to be restored to their original form. tar file also known as Tarballs. Tarballs are a common way to distribute Linux packages. Tarballs are normally distributed in a compressed format, with a .tar.gz or .tgz file extension, consolidated as a package in a single file.

Syntax of tar command

Syntax of tar command is

tar option(s) archive_name file_name(s)
  • tar :- command
  • options :- switch and parameters used with tar command
  • archive_name :- name of archived file created from command
  • file_name :- single or multiple files used to create tar archive file

options used with tar command

tar command requires at least one option to work. tar command has numerous options to use. Most of them are not frequently used. You can check all available options by reading manual page of tar. Run following command for the manual of tar

$man tar

Compulsory options

At least one option form following options required

Option Description
-Aappend tar files to an archive
-ccreate a new archive
-dfind differences between archive and file system
--deletedelete from the archive
-rappend files to the end of an archive
-tlist the contents of an archive
-uonly append files that are newer than copy in archive
-xextract files from an archive

Additional options

Use as per your requirement

Option Description
-bblock size of Nx512 bytes (default N=20)
-Cchange to directory DIR
-fuse archive file or device
-hdo not dump symlinks; dump the files they point to
-iignore blocks of zeros in archive
-jfilter the archive through bzip2
-kkeep existing files; don't overwrite them from archive
-lstay in local file system when creating an archive
-mdon't extract file modified time
--ppreserve-permissions extract all protection information
-vverbosely list files processed
-Xexclude files listed in FILE
-Zfilter the archive through compress
-zfilter the archive through gzip

How to create tar file

To create tar file run following command

$tar -cvf test.tar custom_folder

tar cvf

in above command

  • tar:- command
  • c:- option to creates a new .tar archive file
  • v:- option to display a list of the files that are included in the archive
  • f:- option to specifytype of the archive file.You should always use -f option as the final option in sequence otherwise, the system will become confused as to the desired name for the new file and will use the next option in the sequence as the name.
  • test.tar:- archived file name
  • custom_folder:- folder which is going to archived

How to compress a tar file

tar command does not have compress or decompress features on its own. tar command is combined with external compression utility.

Option Compression utility

When you use any of above switch to combine compress utility with tar, compression utility compress the new archive file as soon as it has been created.

tar with j and z option

How to extract tar file

Following steps should be taken before extracting a tar file.

  1. Check is tar file compressed or not? You can easily determine it by looking at the filename extension.
  2. If the tar file has been compressed, it must first be decompressed using the appropriate decompression program.
  3. Move tar file in an empty directory. Create new if you do not have one. It would prevent the reconstituted files from cluttering up the current directory and overwriting any files or directories with same names that are in it.
  4. It is also advisable to check sufficient space available before unpacking tar file.
  • Use -x option to extract
  • Use -v option to display the list of files during the unpack process
  • Use -f option to specify file name

Same options can be used to have the compression programs automatically decompress tar files prior to extraction. For example to uncompress bz2 file use -j switch or to uncompress gzip file use -z switch.

In following example we would decompress and extract the tar file compressed in above example

make a folder and move all three tar files

tar move archived file in folder

Extract tar file

tar xvf

Extract tar file compressed with bz2

tar xvjf

Extract tar file compress with gzip

tar xvzf

Linux tar command examples

So far in this article we have covered basic of tar with example. Now we will take some advance example of tar command. These examples of tar command helpful for beginner in linux. So let check how Linux system administrator use tar command with example

How to list files of tar tile

Some time you may want to check which files tar file contains without extracting tar file. Use - t option to list the content of tar file without extracting it. -t option works well with compressed tar files as well.

tar tvf

How to remove files after creating tar file

By default, tar creates an archive of copies of the original files and/or directories, and the originals are retained. To remove files / directories after creating tar file use --remove-files option.

tar remove

How to extract single file form tar file

You can extract specific file form tar archive. File name is required for it. You can check file name from -t option as we did in above example. You need to specify full path of file.

To extract single file form tar archive

$tar -xvf  [Archived tar file name] [name of file ]

tar single file

If tar file compressed with bz2 use -j option while extracting

$tar -xvjf  [Archived tar file name] [name of file ]

If tar file compressed with gzip use -z option while extracting

$tar -xvzf  [Archived tar file name] [name of file ]

How to extract multiple files from tar archive

In above example we extracted single file but you can extract multiple files from tar archive. You can specify multiple files separately

To extract single file form tar archive

$tar -xvf  [Archived tar file name] ["name of file" ] ["name of file" ]

tar multiple file

If tar file compressed with bz2 use -j option while extracting

$tar -xvjf  [Archived tar file name] ["name of file" ] ["name of file" ] ["name of file" ]

If tar file compressed with gzip use -z option while extracting

$tar -xvzf  [Archived tar file name] ["name of file" ] ["name of file" ] ["name of file" ]

How to extract group files from tar file

You can use wildcard to extract group of files from tar file. Same as above if tar file is compressed you need to use same options [-j for bz2, -z for gzip ] as well.

tar wildcard

How to add new files in existing tar file

-r option is used to append the existing tar archive.

You can only append tar file if it is not compressed. You cannot append tar file if you have used -j , -z or -Z options during the creation process.

tar append

How to exclude files from tar

While creating tar archive some time you need to exclude certain files or directories. With --exclude option you can specify the file or directory

tar execlude

You can use multiple exclude options for tar like

$ tar --exclude='file1' --exclude='directory/file2' --exclude='patter*'

ComputerNetworkingNotes RHCE 7 Study Guide Tar command and Syntax Explained with Examples