Skip to Content
This project is a work in progress. If you have any questions or suggestions, feel free to contact me.
LinuxArchives & CompressionTar Archives

Working with tar

  • tar stands for Tape Archive.
  • Used to archive multiple files/directories into a single file.
  • Commonly used for backup, compression, and packaging.
  • Output is usually a .tar, .tar.gz, .tgz, or .tar.bz2 file.

Basic Syntax

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

Common Options

OptionDescription
-cCreate new archive
-xExtract from archive
-tList contents of archive
-vVerbose (show file names)
-fFile name of archive
-zCompress with gzip (.tar.gz, .tgz)
-jCompress with bzip2 (.tar.bz2)
-JCompress with xz (.tar.xz)

Create an Archive

tar -cvf archive.tar file1 file2 dir/
  • Creates archive.tar with specified files/directories
  • -c: create, -v: verbose, -f: archive name

Create Compressed Archive

tar -czvf archive.tar.gz file1 dir/
  • Uses gzip compression (-z)
tar -cjvf archive.tar.bz2 file1 dir/
  • Uses bzip2 compression (-j)

Extract Archive

tar -xvf archive.tar
  • Extracts contents into current directory
tar -xzvf archive.tar.gz
  • Extracts .tar.gz archive
tar -xjvf archive.tar.bz2
  • Extracts .tar.bz2 archive

Extract to Specific Directory

tar -xvf archive.tar -C /path/to/dir

List Archive Contents

tar -tvf archive.tar

Add File to Existing Archive (not compressed)

tar -rvf archive.tar newfile.txt

Extract Single File

tar -xvf archive.tar file1.txt

Summary

TaskCommand Example
Create .tartar -cvf archive.tar files/
Create .tar.gztar -czvf archive.tar.gz files/
Extract .tartar -xvf archive.tar
Extract .tar.gztar -xzvf archive.tar.gz
List contentstar -tvf archive.tar
Extract to directorytar -xvf archive.tar -C /target/path
Last updated on