How to install tar on RHEL/CentOS 6/7/8 using yum

last updated in Categories , , ,

I am trying to extract a file using ‘tar xvf file.tar.gz’ syntax, but I am getting an error:
bash: tar: command not found
How do I fix this problem? How can I install tar on a CentOS/RHEL version 8, 7 or 6 desktop using yum command?

GNU tar is a free and open-source software package for archiving files. The GNU/tar might not be installed on your system. This page shows how to install tar using the yum command on CentOS or Red Hat Linux.

ADVERTISEMENTS


How to install tar on RHEL/CentOS

The procedure to install tar on Red Hat Enterprise Linux:

  1. Open the terminal application
  2. Search for tar app, run: sudo yum search tar
  3. Install tar on CentOS/RHEL, run: sudo yum install tar
  4. Upgrade tar on CentOS/RHEL 6/7/8 Linux, run: sudo yum update tar

Let us see all command and example in details.

How to use yum to install tar command

Open the Terminal application or login using ssh client and type the following yum command to install tar on CentOS/RHEL 6/7/8:
sudo yum search tar
Sample outputs:

Last metadata expiration check: 0:11:49 ago on Wed Jun 24 13:29:35 2020.
============================================== Name Exactly Matched: tar ==============================================
tar.x86_64 : A GNU file archiving program
============================================= Name & Summary Matched: tar =============================================
libtar.i686 : Tar file manipulation API
libtar.x86_64 : Tar file manipulation API
kf5-libgravatar.x86_64 : Gravatar support library
php56-php-tarantool.x86_64 : PHP driver for Tarantool/Box
php70-php-tarantool.x86_64 : PHP driver for Tarantool/Box
php71-php-tarantool.x86_64 : PHP driver for Tarantool/Box
php72-php-tarantool.x86_64 : PHP driver for Tarantool/Box
python3-pylibravatar.noarch : Python module for Libravatar
target-restore.noarch : Systemd service for targetcli/rtslib
....
..
....
gnome-getting-started-docs-gl.noarch : Galician translations for gnome-getting-started-docs videos
gnome-getting-started-docs-hu.noarch : Hungarian translations for gnome-getting-started-docs videos
gnome-getting-started-docs-pt_BR.noarch : Brazilian Portuguese translations for gnome-getting-started-docs videos
================================================== Name Matched: tar ==================================================
star.x86_64 : An archiving tool with ACL support
bsdtar.x86_64 : Manipulate tape archives
kstart.x86_64 : Daemon version of kinit for Kerberos v5
xmlstarlet.x86_64 : Command Line XML Toolkit
texlive-ifmtarg.noarch : If-then-else command for processing potentially empty arguments
================================================ Summary Matched: tar =================================================
subversion-tools.x86_64 : Supplementary tools for Subversion
python3-rtslib.noarch : API for Linux kernel LIO SCSI target
centos-indexhtml.noarch : Browser default start page for CentOS
....
..
drpm.i686 : A library for making, reading and applying deltarpm packages
drpm.x86_64 : A library for making, reading and applying deltarpm packages
ntsysv.x86_64 : A tool to set the stop/start of system services in a runlevel
aespipe.x86_64 : AES-based encryption tool for tar/cpio and loop-aes imagemore
e3.x86_64 : Text editor with key bindings similar to WordStar, Emacs, pico, nedit, or vi
icedtea-web.noarch : Additional Java components for OpenJDK - Java browser plug-in and Web Start implementation

Get info about the tar package

Type the following yum command:
sudo yum list tar
Sample outputs:

Last metadata expiration check: 0:12:50 ago on Wed Jun 24 13:29:35 2020.
Available Packages
tar.i686                                                     2:1.30-4.el8                                                   BaseOS
tar.x86_64                                                   2:1.30-4.el8                                                   BaseOS

Execute the following command:
yum info tar.x86_64
Sample outputs:

Last metadata expiration check: 0:33:46 ago on Mon Dec 16 16:39:38 2019.
Available Packages
Name         : tar
Epoch        : 2
Version      : 1.30
Release      : 4.el8
Arch         : x86_64
Size         : 838 k
Source       : tar-1.30-4.el8.src.rpm
Repo         : BaseOS
Summary      : A GNU file archiving program
URL          : http://www.gnu.org/software/tar/
License      : GPLv3+
Description  : The GNU tar program saves many files together in one archive and can
             : restore individual files (or all of the files) from that archive. Tar
             : can also be used to add supplemental files to an archive and to update
             : or list files in the archive. Tar includes multivolume support,
             : automatic archive compression/decompression, the ability to perform
             : remote archives, and the ability to perform incremental and full
             : backups.
             : 
             : If you want to use tar for remote backups, you also need to install
             : the rmt package on the remote box.

Install tar on CentOS or RHEL

Finally install gnu/tar on your CentOS or RHEL box:
sudo yum update
sudo yum install tar

Install tar on RHEL or CentOS Linux
We got tar installed on CentOS or RHEL. Let us verify GNU/tar version. Type:
tar --version
Sample outputs:

tar (GNU tar) 1.30
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
Written by John Gilmore and Jay Fenlason.

How to use tar Command in Linux

Now that tar on RHEL installed on RHEL. Let us see some tar command examples. The syntax is:
tar [options] file

Extract a tar.gz archive

Say you have a wordpress.tar.gz archive and you need to extract it, run:
tar -xvzf wordpress.tar.gz

wordpress/
wordpress/xmlrpc.php
wordpress/wp-blog-header.php
wordpress/readme.html
wordpress/wp-signup.php
wordpress/index.php
wordpress/wp-cron.php
wordpress/wp-config-sample.php
wordpress/wp-login.php
wordpress/wp-settings.php
wordpress/license.txt
wordpress/wp-content/
wordpress/wp-content/themes/
wordpress/wp-content/themes/twentynineteen/
wordpress/wp-content/themes/twentynineteen/footer.php
wordpress/wp-content/themes/twentynineteen/template-parts/
wordpress/wp-content/themes/twentynineteen/template-parts/content/
....
...
....
wordpress/wp-admin/widgets.php
wordpress/wp-admin/setup-config.php
wordpress/wp-admin/install.php
wordpress/wp-admin/admin-header.php
wordpress/wp-admin/post-new.php
wordpress/wp-admin/themes.php
wordpress/wp-admin/options-reading.php
wordpress/wp-trackback.php
wordpress/wp-comments-post.php

Where,

  • x – Extract files.
  • v – Verbose outputs.
  • z – Work on compressed files
  • f – Extract file named wordpress.tar.gz

How to extract files to a specific directory

To extract all files into a specified directory such as /tmp/ pass the -C options to the tar command”
tar -xvzf wordpress.tar.gz -C /tmp/

Only extract a single file from tarball

Say you want to get README.txt, run:
tar -xvzf wordpress.tar.gz README.txt
One can extract multiple files using wildcards. For example, all ‘*.txt’ or ‘*.php’ files, run:
tar -xvzf wordpress.tar.gz *.php
tar -xvzf wordpress.tar.gz *.txt

Sample outputs:

wordpress/license.txt
wordpress/wp-content/themes/twentynineteen/readme.txt
wordpress/wp-content/themes/twentysixteen/readme.txt
wordpress/wp-content/themes/twentysixteen/genericons/COPYING.txt
wordpress/wp-content/themes/twentysixteen/genericons/LICENSE.txt
wordpress/wp-content/themes/twentytwenty/readme.txt
wordpress/wp-content/themes/twentyseventeen/readme.txt
wordpress/wp-content/plugins/akismet/LICENSE.txt
wordpress/wp-content/plugins/akismet/readme.txt
wordpress/wp-includes/images/crystal/license.txt
wordpress/wp-includes/ID3/license.commercial.txt
wordpress/wp-includes/ID3/license.txt
wordpress/wp-includes/ID3/readme.txt
wordpress/wp-includes/js/tinymce/license.txt
wordpress/wp-includes/js/swfupload/license.txt
wordpress/wp-includes/js/plupload/license.txt

How do I list and search contents of the tar archive

Pass the -ztvf as follows:
tar -ztvf wordpress.tar.gz
Sample outputs:

drwxr-xr-x nobody/nogroup    0 2019-12-12 21:33 wordpress/
-rw-r--r-- nobody/nogroup 3150 2019-07-01 08:01 wordpress/xmlrpc.php
-rw-r--r-- nobody/nogroup  369 2017-11-30 23:11 wordpress/wp-blog-header.php
-rw-r--r-- nobody/nogroup 7368 2019-09-02 21:44 wordpress/readme.html
-rw-r--r-- nobody/nogroup 31112 2019-09-03 00:41 wordpress/wp-signup.php
-rw-r--r-- nobody/nogroup   420 2017-11-30 23:11 wordpress/index.php
-rw-r--r-- nobody/nogroup  3955 2019-10-10 22:52 wordpress/wp-cron.php
-rw-r--r-- nobody/nogroup  2898 2019-01-08 04:30 wordpress/wp-config-sample.php
-rw-r--r-- nobody/nogroup 47597 2019-12-09 13:30 wordpress/wp-login.php
-rw-r--r-- nobody/nogroup 19120 2019-10-15 15:37 wordpress/wp-settings.php
-rw-r--r-- nobody/nogroup 19935 2019-01-01 20:37 wordpress/license.txt
drwxr-xr-x nobody/nogroup     0 2019-12-12 21:33 wordpress/wp-content/
drwxr-xr-x nobody/nogroup     0 2019-12-12 21:33 wordpress/wp-content/themes/

Use the grep command for searching a file:
tar -ztvf wordpress.tar.gz | grep INSTALL.txt

How to create a tarball archive

The syntax is:
tar -cvf backups.tar /var/www/html/
Add the compression via gzip:
tar -zcvf backups.tar.gz /var/www/html/
To apply bzip2 compression and create a tarball, run
tar -jcvf backups.tar.bz2 /var/www/html/
One can extract such files using the following syntax:
tar -jxvf backups.tar.bz2 ## gzip compression ##
tar -jxvf backups.tar.bz2 ## bzip2 compression ##

Conclusion

You learned how to install the tar command on CentOS or RHEL version 6/7/8 using the yum command. See GNU/tar help pages here for detailed syntax usage.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

1 comment

    Still, have a question? Get help on our forum!