Skip to content

tar.gz

tar.gz is a file format that is used to compress files and directories. It is a combination of tar and gzip.

Syntax

tar [options] [file]

Options

  • -c: Create a new archive.
  • -x: Extract files from an archive.
  • -t: List the contents of an archive.
  • -r: Append files to an archive.
  • -u: Update files in an archive.
  • -d: Compare files in an archive with files on the file system.
  • -A: Concatenate archives.
  • -f: Specify the file name of the archive.
  • -v: Verbose mode.
  • -z: Compress the archive with gzip.
  • -j: Compress the archive with bzip2.
  • -C: Change to a specific directory before performing any operations.
  • -p: Preserve the permissions of the files.
  • -P: Preserve the absolute path of the files.
  • -m: Do not overwrite existing files.
  • -W: Verify the archive after writing it.
  • -l: Follow symbolic links.
  • -h: Follow hard links.
  • -s: Handle sparse files efficiently.
  • -S: Handle sparse files inefficiently.
  • -k: Do not overwrite existing files.
  • -N: Only include files newer than the specified date.
  • -o: Only include files older than the specified date.
  • -T: Read the list of files from a file.
  • -X: Exclude files that match the specified pattern.
  • -I: Include files that match the specified pattern.
  • -R: Include files that match the specified pattern recursively.
  • -q: Suppress warnings.
  • -w: Ask for confirmation before overwriting files.
  • -y: Assume "yes" to all questions.
  • -n: Do not overwrite existing files.
  • -U: Overwrite files only if they are newer than the existing files.
  • -a: Preserve the permissions, ownership, and timestamps of the files.
  • -b: Set the block size of the archive.
  • -B: Set the block size of the archive in bytes.
  • -C: Change to a specific directory before performing any operations.
  • -D: Create a diff file.

Examples

Create a tar.gz archive

tar -czvf archive.tar.gz file1 file2 file3

Create a tar.gz archive with a specific directory

tar -czvf archive.tar.gz /path

Extract a tar.gz archive

tar -xvf archive.tar.gz

Extract a tar.gz archive to a specific directory

tar -xvf archive.tar.gz -C /path

List the contents of a tar.gz archive

tar -tvf archive.tar.gz

Compare files in a tar.gz archive with files on the file system

tar -dvf archive.tar.gz

Concatenate archives

tar -Af archive1.tar archive2.tar

Append files to an archive

tar -rvf archive.tar file1 file2 file3

Update files in an archive

tar -uvf archive.tar file1 file2 file3

Extract files from an archive

tar -xvf archive.tar file1 file2 file3