Skip to content

tar usage example

Tar Command Syntax

The basic syntax of the tar command is shown below:

tar [option] [archive-name] [files or directories]

A brief explanation of each option is shown below:

  • c: Used to create a tar archive.
  • x: Used to extract from the tar archive.
  • t: Used to display a list of files inside the tar archive.
  • r: Used to add an additional file to the tar archive.
  • W: Used to verify a tar archive.
  • z: Used to create a tar archive using gzip.
  • j: Used to create a tar archive using bzip.
  • v: Used to display verbose information.
  • f: Used to specify the archive file name.

Creating a Tar Archive

To create a tar archive named compress-file.tar from a single file named file1.txt, run the following command:

tar -cf compress-file.tar file1.txt

To create a tar archive named compress-file.tar from multiple files, including file1.txt, file2.txt, and file3.txt, run the following command:

tar -cf compress-file.tar file1.txt file2.txt file3.txt

To create a tar archive of any directory, run the following command:

tar -cf compress-dir.tar dir1

This command will create a tar archive named compress-dir.tar from a directory called dir1.

Creating a Tar Bz2 Archive

You can compress a tar file using the bz2 algorithm by specifying tar.bz2 at the end of the archive name.

To create a tar.bz2 archive from the directory /etc, use the following command:

tar -cjf compress.tar.bz2 /etc

You can also specify the -v option to display the compression process in verbose mode.

Creating a Tar Gzip Archive

To create a tar archive in gzip format, use the -z option with the tar command.

The following command creates a tar.gz archive from the /usr directory:

tar -czf compress.tar.gz /use

Listing the Contents of a Tar Archive

You can use the -t option with the tar command to display a list of the contents available in a tar archive file.

The following example displays a list of the contents inside the compress-dir.tar file:

tar -tvf compress-dir.tar

Extracting a Tar Archive

To uncompress a tar archive, use the -x option with the tar command.

For example, to uncompress a file called compress-dir.tar, run the following command:

tar -xvf compress-dir.tar

To extract a single file named file1.txt from the compress-dir.tar file, run the following command:

tar -xvf compress-dir.tar file1.txt

Extracting a Tar Gzip Archive

To extract or uncompress a Gzip archive file using the tar command, use the -z option.

For example, to extract the compress.tar.gz archive file, run the following command:

tar -xzvf compress.tar.gz

Extracting a Tar Bz2 Archive

To extract files from a Bz2 archive, use the -j option with the tar command.

For example, to extract the contents of the compress.tar.bz2 archive file, run the following command:

tar -xjvf compress.tar.bz2

Extract a Tar Archive in a Different Directory

By default, when you extract a tar archive, its contents are extracted in the current directory. However, you can use the -C option with the tar command to extract the archive to a specified directory.

For example, to extract the contents of compress-dir.tar to the /opt directory, run the following command:

tar -xvf compress-dir.tar -C /opt

Adding and Removing Files from a Tar Archive

You can add or remove specific files and directories from an existing tar archive. To add a file, use the -r option, and to remove a file, use the –delete option.

For example, to add a new file named file6.txt to the archive named compress-dir.tar, run the following command:

tar -rvf compress-dir.tar file6.txt

To remove a file named file1.txt from the archive named compress-dir.tar, use the following command:

tar --delete -f compress-dir.tar file1.txt

Extract Multiple Files from the Tar Archive

To extract a group of files from a tar archive, you can use a wildcard.

For example, to extract all files that match the pattern “.html” from the archive “compress.tar.gz”, use the following command:

tar -xvzf compress.tar.gz --wildcards '*.html'

https://www.linode.com/docs/guides/archiving-and-compressing-files-with-gnu-tar-and-gnu-zip/

Feedback