tar command - how to archive files in Linux

Updated 29 October 2024

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.

Common Options

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

OptionA 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
--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.
-pPreserve 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-zerosIgnore zeroed blocks in the archive, which are generally treated as end-of-file (EOF) markers. Tar will stop reading after encountering 2 consecutive EOF (512-blocks filled with zeroes).
--no-recursionAvoid descending automatically in directories.
--one-file-systemInstructs tar to stay in the local file system when creating an archive.
-WVerify the archive after writing it to ensure that it has been created correctly.

tar Command Operations <h2>

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

Create

The -c option can be used to combine multiple files into an archive called a tarball. Here is an example of creating archives using the tar command:

$ tar -cf archive.tar file1 file2

Compress

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

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

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

$ tar -Af archive.tar archive2.tar

Append

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

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

Spotted a mistake or want something added? Send me a note.