Archiving utility. Often combined with a compression method, such as gzip or bzip.

# To create an archive from files:
tar cf <target.tar> <file1> <file2> <file3>

# To create a gzipped archive:
tar czf <target.tar.gz> <file1> <file2> <file3>

# To create a gzipped archive from a directory using relative paths:
tar czf <target.tar.gz> -C <path/to/directory> .

# To extract a (compressed) archive into the current directory:
tar xf <source.tar[.gz|.bz2|.xz]>

# To extract a (compressed) archive into the target directory:
tar xf <source.tar[.gz|.bz2|.xz]> -C <directory>

# To create a compressed archive, using archive suffix to determine the compression program:
tar caf <target.tar.xz> <file1> <file2> <file3>

# To list the contents of a tar file:
tar tvf <source.tar>

# To extract files matching a pattern:
tar xf <source.tar> --wildcards <"*.html">

# To extract a specific file without preserving the folder structure:
tar xf <source.tar> <source.tar/path/to/extract> --strip-components=<depth_to_strip>

# ---

# Make a backup of the /etc dir
tar cvfz /etc-backup.tgz /etc

# Extracting tar into chosen directory
tar xvzf filename.tar.gz -C /desired/path

# tar the directory cps100 (and its files/subdirectories) into a tar
# file named foo.tgz.
tar cvzf foo.tgz cps100

# To tar all .cc and .h files into a tar file named foo.tgz use:
tar cvzf foo.tgz *.cc *.h

# Create a tar named file.tar in the directory you currently are in.
# Wildcards could also be used in this command, for example: tar -cvwf
# file.tar *.txt would archive all txt files in the current directory.
tar cvwf file.tar myfile.txt

# Extracting (untar) an archive using tar command
tar xvf archive_name.tar

# Creating a tar gzipped archive using option cvzf
tar cvzf archive_name.tar.gz dirname/

# View the tar archive file content without extracting using option tvf
tar tvf archive_name.tar

# Extract a single file / directory from tarball archive
tar xvf /dev/st0 filename
tar xvf /dev/st0 directory-name
tar xvf mytar.ball.tar filename
tar -zxvf mytar.ball.tar.gz directory-name

# Extract file to /tmp directory
tar -zxvf mytar.ball.tar.gz -C /tmp filename
tar -zxvf mytar.ball.tar.gz -C /tmp dir-name

# Tar Extract a Single File(s) From a Large Tarball

# Extract a file called `etc/default/sysstat` from `config.tar.gz` tarball
tar -ztvf config.tar.gz
tar -zxvf config.tar.gz etc/default/sysstat
tar -xvf {tarball.tar} {path/to/file}
# or
tar --extract --file={tarball.tar} {file}

# Extract a directory called `css` from `cbz.tar`
tar --extract --file=cbz.tar css

# Extract from cbz.tar all files beginning with `pic`, regardless of directory prefix
tar -xf cbz.tar --wildcards --no-anchored 'pic*'

# Extract all `php` files from `cbz.tar`
tar -xf cbz.tar --wildcards --no-anchored '*.php'

# UNIX decompress `tgz` / `tar.gz` file
gzip -dc filename.tgz | tar xf -
gzip -dc filename.tar.gz | tar xf -
# If file extension ends with `.Z`
zcat filename.tar.Z | tar xf -

# Copies directory_or_file_name on the local machine
# to /path/to/destination/directory_or_file_name on
# a remote machine.
tar -czf - directory_or_file_name | ssh username@hostname \
    "cd /path/to/destination; tar -xzf -"

# Copies the directory called directory_name from
# /path/to/source/directory_name on a remote server
# to the current directory on the local machine.
ssh username@hostname "cd /path/to/source; \
    tar -czf - directory_name" | tar -xzf -

# tar from file list
tar zcvf myFile.tar.gz file1.txt file2.txt file3.txt file4.txt