Skip to main content

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

# To [c]reate an archive and write it to a [f]ile:
tar cf target.tar file1 file2 file3

# To [c]reate a g[z]ipped archive and write it to a [f]ile:
tar czf target.tar.gz file1 file2 file3

# To [c]reate a g[z]ipped archive from a directory using relative paths:
tar czf target.tar.gz --directory=path/to/directory .

# To E[x]tract a (compressed) archive [f]ile into the current directory [v]erbosely:
tar xvf source.tar[.gz|.bz2|.xz]

# To E[x]tract a (compressed) archive [f]ile into the target directory:
tar xf source.tar[.gz|.bz2|.xz] --directory=directory

# To [c]reate a compressed archive and write it to a [f]ile, using [a]rchive suffix to determine the compression program:
tar caf target.tar.xz file1 file2 file3

# To Lis[t] the contents of a tar [f]ile [v]erbosely:
tar tvf source.tar

# To E[x]tract files matching a pattern from an archive [f]ile:
tar xf source.tar --wildcards "*.html"

# ---

# 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