tar Cheat Sheet: Quick Reference Guide

Written by: Linuxopsys   |   Last updated: June 26, 2023

Introduction

When it comes to creating backups, packaging software source code for distribution, and managing files in Linux, the tar command is no doubt one of the widely used archiving utilities. Tar command which is abbreviated as tape archive is used to group files into archives called tarballs and also compress files using popular compression algorithms such as gzip, bzip2, and xz.

This guide provides a comprehensive cheat sheet for the tar command, covering commonly used options and examples.

Common Options

The table below lists the most common options you can use with the tar command to perform different operations.

OptionsA brief description
-AAppend the archive to the end of another archive.
-cCreate a new archive. Directories are archived recursively, unless the --no-recursion option is given.
-dFind differences between the archive and the file system.
--deleteThis option has no short form, it deletes specified members from an archive.
-rSame as the -A options. It appends files to the end of an archive.
-tList the contents of an archive.
-uAppend files that are newer than the corresponding copy in the archive.
-xExtract files from an archive.
-? or --helpDisplay a short option summary and exit.
-kDon't replace existing files when extracting.
-OExtract files to standard output.
-fthe filename of the archive.
-vVerbosely list files processed.
-zCompress files with gzip.
-jCompress files with bzip2.
-JCompress files with xz.
-ZCompress files with compress.
-aUse the archive suffix/extension to determine the compression program.
-lUsed to check for symbolic links when creating archives.
-CChange to the specified directory before permitting any operations
-WUsed with -c option to verify any data errors.
--exclude=FILEExclude the FILE when adding to the tar archive or extracting from a tar archive.
-PPreserves leading slashes from file names when creating archives.
-p Preserve archived file permissions during extraction.
--wildcardsEnables the tar command to interpret wildcards.
-mInstructs tar to update the modification time of the extracted files to the current time.
-NOnly archive files newer than the specified date.
--remove-filesThis deletes the original files from the disk after archiving them.
--strip-componentsStrips the specified number of leading components from file names during extraction.

--to-commandPipe extracted files to a command for further processing.
--listed-incrementalUsed to handle new GNU-format incremental backups.
--ignore-zeros
Ignore zeroed blocks in the archive, which are normally treated as end-of-file (EOF) markers. Tar will stop reading after encountering 2 consecutive EOF (512-blocks filled with zeroes).
--no-recursion
Avoid descending automatically in directories.
--one-file-systemInstructs tar to stay in local file system when creating archive.

tar Command Operations

Now let's take a look at some common operations you can do with the tar command.

Create an archive

The -c option can be used to combine multiple files into an archive called a tarball. The -f option is used to specify the archive file name.

Here is an example of creating archives using the tar command:

$ tar -cf archive.tar file1 file2

Compress an archive

Compressing is different from creating an archive in the sense that archiving uses the same amount of disk space as all the individual files and directories combined, whilst compression reduces the size. The option for compressing files depends on the type of algorithm you want to use. To create and compress an archive using gzip use the -z option:

$ tar -czf archive.tar.gz file1 file2

Tar offers a good option, -a, that intelligently determines the compression algorithm from the specified archive suffix or extension. Compressing an archive using the xz compression algorithm, for example, you would use:

$ tar -caf archive.tar.xz file1 file2

Extract from an archive

The -x option can be used for extracting or uncompressing archives. Here is an example of extracting an archive in the current working directory:

$ tar -xf archive.tar

If you want to extract an archive to a specific directory use the -C option followed by the directory where you want the files to be extracted:

$ tar -xf archive.tar -C directory-path

Concatenate multiple archives

If you have two or more archives you can concatenate or combine them using the -A option:

$ tar -Af archive.tar archive2.tar

Append files to archive

Tar command gives you the ability to add more files to an already existing archive without having to extract and archive the files again. To append files to an archive use the -a option or -r option:

$ tar -rf archive.tar file_to_append
$ tar -af archive.tar file_to_append

List archive contents

The -t option can be used to list the contents of an archive. This option is very handy if you just want to have a peek at a large archive without extracting it:

$ tar -tf archive.tar

Download the tar cheat sheet - PDF

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment