Site icon DesignLinux

Tar Command in Linux (Create and Extract Archives)

Extract tar.bz2 file

The Tar word derived from its name “Tape ARchive”, it was designed to store files on magnetic tape. The tar command is used to make tar archives by encapsulating the files or directories. It can be use to extract tar files, add new files and to list the files included in the archive. In this tutorial we will show you how to use tar command to extract, create and list the archives with examples.

Tar Command Syntax

By default most Linux systems comes with GNU tar pre-installed. There are two versions of tar, BSD tar and GNU tar. The basic syntax for tar is as below:

tar [OPERATION_AND_OPTIONS] [ARCHIVE_NAME] [FILE_NAME]

Creating Tar Archive

Tar supports a vast range of compression programs such as gzip, bzip2, lzip, lzma, lzop, xz and compress. To create a tar archive use the -c option followed by -f and the name of the archive.

For example, to create an archive named test.tar for the files named file1, file2, file3, you would run the following command:

tar -cf test.tar file1 file2 file3

Below is the same command using the long-form options:

tar --create --file=test.tar file1 file2 file3

It is also possible to make archives from the contents of one or more directories or files. By default, directories are archived recursively unless --no-recursion option is specified.

The following example will create an archive named sites.tar of the /var/www directory:

tar -cf sites.tar /var/www

To view the log of files which are being processed, use the -v option.

Creating Tar Gz Archive

Gzip is the most popular for compressing tar files. The name of archive should end with either tar.gz or tgz while compressing tar archives.

The -z option informs the tar to compress the archive using the gzip algorithm. For instance, to create a tar.gz archive from given files you would use the following command:

tar -czf test.tar.gz file1 file2

Creating Tar Bz2 Archive

Another popular algorithm for compressing tar files is bzip2. When compressing tar archives with bzip2 the archive name should end with either tar.bz2 or tbz.

When -j option is specified tar will use bzip2 algorithm to compress the archive. The following command will create a tar.bz2 archive from the given files:

tar -cjf archive.tar.bz2 file1 file2

Listing Tar Archives

When used with the --list (-t) option, the tar command will list the content of a tar archive without extracting it. The command bellow, will list the content of the test.tar file:

To get the list of the tar command, you should use the --list (-t) option, the tar command will list the content of a tar archive without extracting it. Following example command will list the content of the test.tar file:

tar -tf test.tar

It will show the list the names of all files in the archive:

file1
file2
file3

You also can use the --verbose (-v) option to get more details such as file owner , file size, timestamp, etc.

tar -tvf test.tar
-rw-r--r-- tecnstuff/users 0 2020-02-08 01:19 file1
-rw-r--r-- tecnstuff/users 0 2020-02-08 01:19 file2
-rw-r--r-- tecnstuff/users 0 2020-02-08 01:19 file3

Extracting Tar Archive

Commonly, the archived files in Linux are archived and compressed using a tar or tar.gz format. It is important to know how to extract these files from the command line.

Use the --extract (-x) option followed by the archive name to extract a tar archive:

tar -xf test.tar

Use -v option to print the names of the files being extracted.

tar -xvf test.tar

Extracting Tar Archive to Different Directory

By default, tar will extract the archive contents in the current working directory . Use the --directory (-C) to extract archive files in a specific directory:

For instance, to extract the archive contents to the /home/tecnstuff/data directory, you can use:

tar -xf test.tar -C /home/tecnstuff/data

Extracting Tar Gz and Tar Bz2 Archives

It is not required to specify a decompression option while extracting tar.gz or tar.bz2 files. The command will be same as when extracting tar archive:

tar -xf test.tar.gz
tar -xf test.tar.bz2

Extract Specific Files from a Tar Archive

When you have required to extract only specific files from archive, you don’t need to extract whole archive. You can extract a specific file from a tar archive by appending a space-separated list of file names after the archive name:

tar -xf test.tar file1 file2

Extracting one or more directories from an archive is the same as extracting files:

tar -xf test.tar dir1 dir2

Adding Files to Existing Tar Archive

To add files or directories to an existing tar archive, use the --append (-r) operation.

For example, to add a file named newfile to test.tar, you would run:

tar -rvf test.tar newfile

Removing Files from a Tar Archive

Use the --delete operation to remove files from an archive.

The following example shows how to remove the file file1 from test.tar:

tar --delete -f test.tar file1

Conclusion

Mostly, common use of the tar command is to create and extract the tar archive. To know more about the tar command, visit Gnu tar documentation page.

Exit mobile version