• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Tar Command in Linux (Create and Extract Archives)

Written by Admin, Updated On August 8, 2020
tar, terminal
Tar Command

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]
  • OPERATION – It is mandatory and can pass only one argument. The most frequently used operations are:
    • --create (-c) – Used to create a new tar archive.
    • --extract (-x) – Extract the entire archive or one or more files from an archive.
    • --list (-t) – Get a list of the files included in the archive
  • OPTIONS – The most frequently used operations are:
    • --verbose (-v) – Show the files being processed by the tar command.
    • --file=archive=name (-f archive-name) – Specifies the archive file name.
  • ARCHIVE_NAME – The name of the archive.
  • FILE_NAME – A space-separated list of filenames to be extracted from the archive. If not provided the entire archive is extracted.

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.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article How to Install Memcached on Ubuntu 20.04
Next Article   How to Install TensorFlow on Debian 10

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook