Skip to content

tar command

Create and extract .tar, .tar.gz, .tar.bz2 and .tar.xz archives.

On this page

Syntax

tar [options] [archive] [files…]

Provided by the tar package. Run man tar for the full manual.

Common options

OptionWhat it does
-cCreate an archive
-xExtract an archive
-tList the contents
-f FILEArchive file name (put it last in the group)
-zgzip (.tar.gz, .tgz)
-jbzip2 (.tar.bz2)
-Jxz (.tar.xz)
-vList files as they are processed
-C DIRExtract into or archive from DIR
--exclude=PATTERNSkip matching files

Examples

  1. Extract a .tar.gz

    tar -xzf archive.tar.gz
  2. Extract into a directory

    tar -xzf archive.tar.gz -C /opt/app
  3. Extract a .tar.xz

    tar -xJf linux.tar.xz
  4. Extract a .tar.bz2

    tar -xjf backup.tar.bz2
  5. Create a .tar.gz

    tar -czf backup.tar.gz /var/www/html
  6. Create with exclusions

    tar -czf site.tar.gz --exclude="*.log" --exclude=node_modules project/
  7. List the contents

    tar -tzf archive.tar.gz
  8. Extract one file

    tar -xzf archive.tar.gz path/to/file.txt
  9. Create a .tar.xz

    tar -cJf data.tar.xz data/
  10. Back up with a date in the name

    tar -czf "backup-$(date +%F).tar.gz" /etc

Tips

  • Modern GNU tar detects compression when extracting, so tar -xf file.tar.gz also works.
  • Remember it as e**x**tract **z**ipped **f**ile: -xzf; **c**reate **z**ipped **f**ile: -czf.

tar guides

← All Linux commands · Suggest a fix

Back to top