Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Tar Command in Linux (Create and Extract Archives) - DesignLinux

Jul 26 2020
designlinux 0 Comments
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]
  • 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.

Related

Tags: tar, terminal

How to Install Apache Cassandra on CentOS 8

Prev Post

How to Set Up SSH Keys on Ubuntu 20.04

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 256
  • 614,628

DesignLinux.com © All rights reserved

Go to mobile version