Back to the command reference

tar

ArchivesFundamentals In simulator In real Linux

Create and extract archives

Name: tar = tape archive

You can practise it right away in the simulator, and use it in real Linux mode too.

Description

tar bundles files and directories into a single archive (.tar), keeping structure, permissions and times. It doesn't compress by itself but can call gzip (-z), bzip2 (-j) or xz (-J) to make .tar.gz, .tar.bz2 or .tar.xz files.

Remember: c = create, x = extract, t = list, with the archive name right after f.

Syntax

tar [OPTION]... [FILE]...

Common options

-c / -xCreate / extract
-tList the contents
-f ARCHIVEArchive file name (almost always needed)
-z / -j / -JCompress with gzip / bzip2 / xz
-a / --auto-compressPick the compression from the file extension (.tar.gz, .tar.xz, …)
-vVerbose
-C DIRExtract into a directory
--exclude PATTERNLeave out files matching PATTERN, e.g. --exclude="*.log"
-T / --files-from FILERead the names to archive from FILE
-r / --appendAdd files to an existing (uncompressed) archive
-p / --preserve-permissionsKeep the permissions when extracting
-k / --keep-old-filesDo not overwrite existing files when extracting
--strip-components NDrop the first N directories from names when extracting
-O / --to-stdoutExtract files to standard output (no files written)

Examples

  • $ tar -czf backup.tar.gz projects/

    Archive and compress a directory

    Explain
  • $ tar -xzf backup.tar.gz -C /tmp

    Extract into /tmp

    Explain
  • $ tar -tzf backup.tar.gz

    List the contents without extracting

    Explain

Related