This comprehensive Linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

The Linux ‘tar’ stands for tape archive, is used to create Archive and extract the Archive files. tar command in Linux is one of the important command which provides archiving functionality in Linux. The tar command used to rip a collection of files and directories into highly compressed archive file commonly called tarball or tar, gzip and bzip in  Linux.

The GNU tar command included with Linux distributions has integrated compression. It can create a .tar archive and then compress it with gzip or bzip2 compression in a single command. That’s why the resulting file is a .tar.gz file or .tar.bz2 file.

General Syntax

tar [options] [archive-file] [file or directory to be archived]

Options:

root@codesposts:~# tar --help
Usage: tar [OPTION...] [FILE]...
GNU 'tar' saves many files together into a single tape or disk archive, and can
restore individual files from the archive.

Examples:
  tar -cf archive.tar foo bar  # Create archive.tar from files foo and bar.
  tar -tvf archive.tar         # List all files in archive.tar verbosely.
  tar -xf archive.tar          # Extract all files from archive.tar.

 Main operation mode:

  -A, --catenate, --concatenate   append tar files to an archive
  -c, --create               create a new archive
  -d, --diff, --compare      find differences between archive and file system
      --delete               delete from the archive (not on mag tapes!)
  -r, --append               append files to the end of an archive
  -t, --list                 list the contents of an archive
      --test-label           test the archive volume label and exit
  -u, --update               only append files newer than copy in archive
  -x, --extract, --get       extract files from an archive

 Operation modifiers:

      --check-device         check device numbers when creating incremental
                             archives (default)
  -g, --listed-incremental=FILE   handle new GNU-format incremental backup
  -G, --incremental          handle old GNU-format incremental backup
      --ignore-failed-read   do not exit with nonzero on unreadable files
      --level=NUMBER         dump level for created listed-incremental archive
  -n, --seek                 archive is seekable
      --no-check-device      do not check device numbers when creating
                             incremental archives
      --no-seek              archive is not seekable
      --occurrence[=NUMBER]  process only the NUMBERth occurrence of each file
                             in the archive; this option is valid only in
                             conjunction with one of the subcommands --delete,
                             --diff, --extract or --list and when a list of
                             files is given either on the command line or via
                             the -T option; NUMBER defaults to 1
      --sparse-version=MAJOR[.MINOR]
                             set version of the sparse format to use (implies
                             --sparse)
  -S, --sparse               handle sparse files efficiently

 Overwrite control:

  -k, --keep-old-files       don't replace existing files when extracting,
                             treat them as errors
      --keep-directory-symlink   preserve existing symlinks to directories when

Creating Archives

One of the basic functionality of the tar command is to create archives from files or directories.

Creating .tar Archives

To create .tar archive for a particular file or directory using tar command, you can follow the command below:

root@codesposts:~$ tar -cvf name-of-archive.tar /path/to/file-or-directory

Creating .tar.gz Archives

To create .tar.gz archives for a particular file or directory using tar command, you can follow the command below:

root@codesposts:~$ tar cvzf name-of-archive.tar.gz /path/to/file-or-directory

OR

root@codesposts:~$ tar cvzf name-of-archive.tgz /path/to/file-or-directory

Creating .tar.bz2 Archives

To create .tar.bz2 archives for a particular file or directory using tar command, you can follow the command below:

root@codesposts:~$ tar cvzf name-of-archive.tar.bz2 /path/to/file-or-directory

OR

root@codesposts:~$ tar cvzf name-of-archive.tar.tbz /path/to/file-or-directory

OR

root@codesposts:~$ tar cvzf name-of-archive.tar.tb2 /path/to/file-or-directory

Extracting Archives

If you want to uncompress/extract the archived files, you can use -x option with the tar command.

Extracting .tar Archives

Extracting In The Current Directory

To extract the .tar archive in the current working directory, you can use the command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar

Extracting In The Specified Directory

To extract the .tar archive in a specified directory, you can use the -C option with the tar command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar /path/to/directory

Extracting .tar.gz Archives

You can extract the .tar.gz archives, you can use the tar command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar.gz

Extracting .tar.bz2 Archives

You can extract the .tar.bz2 archives, you can use the tar command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar.bz2

Extracting A Single File From Archive

If you want to extract a single file from an archive, you can use the command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar name-of-file

OR

root@codesposts:~$ tar -xvf name-of-archive.tar.gz name-of-file

OR

root@codesposts:~$ tar -xvf name-of-archive.tar.bz2 name-of-file

Extracting Multiple Files From Archive

If you want to extract a single file from an archive, you can use the command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar "File1" "File2" "File3"

OR

root@codesposts:~$ tar -xvf name-of-archive.tar.gz "File1" "File2" "File3"

OR

root@codesposts:~$ tar -xvf name-of-archive.tar.bz2 "File1" "File2" "File3"

Extracting Files Using Wildcards

For .tar Archives

To Extract Files using wildcards from .tar Archives, you can run the command like this:

root@codesposts:~$ tar -xvf name-of-archive.tar --wildcards '*.txt'

For .tar.gz Archives

To Extract Files using wildcards from .tar.gz Archives, you can run the command like this:

root@codesposts:~$ tar -zxvf name-of-archive.tar.gz --wildcards '*.txt'

For .tar.bz2 Archives

To Extract Files using wildcards from .tar.bz2 Archives, you can run the command like this:

root@codesposts:~$ tar -jxvf name-of-archive.tar.bz2 --wildcards '*.txt'

Adding Files Or Directories To Archives

To add files or directories to existing .tar archived file, we can use the option -r (append). 

root@codesposts:~$ tar -rvf name-of-archive.tar filename.ext

But, tar command does not allow to add/append data in .tar.gz or .tar.bz2 archives. If you try to do that, you will get the following error:

root@codesposts:~$ tar -rvf name-of-archive.tar.gz filename.ext

tar: This does not look like a tar archive
tar: Skipping to next header
filename.ext
tar: Error exit delayed from previous errors

Displaying Contents Of Archives

If you want to display the contents of the archived files, you can use -t option with the tar command.

Displaying Contents Of .tar Archives

You can display the contents of the .tar archives, you can use the tar command like this:

root@codesposts:~$ tar -tvf name-of-archive.tar

Displaying Contents Of .tar.gz Archives

You can display the contents of the .tar.gz archives, you can use the tar command like this:

root@codesposts:~$ tar -tvf name-of-archive.tar.gz

Displaying Contents Of .tar.bz2 Archives

You can display the contents of the .tar.bz2 archives, you can use the tar command like this:

root@codesposts:~$ tar -tvf name-of-archive.tar.bz2

Verifying Archives

To verify a .tar archive, we can use the -W option with tar command like below:

root@codesposts:~$ tar -tvfW name-of-archive.tar

But, tar command does not allow to verfiy .tar.gz or .tar.bz2 archives.

Checking Size Of Archives

If you want to check the size of an archive, you can use the command like below:

root@codesposts:~$ tar -czf - name-of-archive.tar | wc -c

OR

root@codesposts:~$ tar -czf - name-of-archive.tar.gz | wc -c

OR

root@codesposts:~$ tar -czf - name-of-archive.tar.bz2 | wc -c