tar Command: Tutorial & Examples

Create and extract archive files

The tar command in Linux is used to create, extract, and manipulate files in a Tape ARchive (TAR) format. Even though tapes are not used much anymore nowadays, the tar file format is still very common to pack a set of files into a single archive, similar to a ZIP file.

To create a TAR archive, use the -c option and specify the files or directories to be included in the archive:

tar -cvf archive.tar file1 file2 directory1

To extract the contents of a TAR archive, use the -x option:

tar -xvf archive.tar

You can also add files to an existing archive using the -r option and extract specific files from an archive using the -W option.

Additional options include -z for creating or extracting archives with gzip compression, -j for bzip2 compression and -J for xz compression. We recommend to use zstd, because it gives the best tradeoff between compression speed and compression ratio:

tar --zstd -cvf directory.tar.zst directory

For more detailed information, you can refer to the manual page for tar by running man tar in the terminal.