如何使用yum在RHEL/CentOS 6/7/8上安装tar

时间:2020-01-09 10:41:01  来源:igfitidea点击:

执行tar xvf file.tar.gz时,报错bash: tar: command not found,如何解决这个问题?

如何使用yum命令在CentOS/RHEL版本8、7或6桌面上安装tar?

GNU tar是一个免费的开源软件包,用于存档文件。

GNU/tar可能未安装在您的系统上。

本教程将学习"如何在CentOS或Red Hat Linux上使用yum命令安装tar"。

如何在RHEL/CentOS上安装tar

在Red Hat Enterprise Linux上安装tar的过程:

  • 打开终端应用程序
  • 搜索tar应用程序,运行:sudo yum search tar
  • 在CentOS/RHEL上安装tar,运行:sudo yum install tar
  • 在CentOS/RHEL 6/7/8 Linux上升级tar,运行:sudo yum update tar

让我们详细查看所有命令和示例。

如何使用yum安装tar命令

打开终端应用程序或使用ssh客户端登录,然后执行以下yum命令以在CentOS/RHEL 6/7/8上安装tar:

sudo yum search tar

获取有关tar包的信息

执行以下yum命令:

sudo yum list tar

输出示例:

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

执行以下命令:

yum info tar.x86_64

输出示例:

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.

在CentOS或RHEL上安装tar

最后在您的CentOS或RHEL机器上安装gnu/tar:

sudo yum update
sudo yum install tar

我们在CentOS或RHEL上安装了tar。
让我们验证GNU/tar版本。
类型:

tar --version

输出示例:

tar (GNU tar) 1.30
Copyright (C) 2016 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.

如何在Linux中使用tar命令

现在,RHEL上的tar安装在RHEL上。
让我们看一些tar命令示例。
语法为:

tar [options] file

提取tar.gz存档

假设您有一个wordpress.tar.gz档案,需要将其解压缩,然后运行:

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

其中:

  • x提取文件。
  • v详细输出。
  • z处理压缩文件
  • f提取名为wordpress.tar.gz的文件

如何将文件提取到特定目录

要将所有文件提取到指定目录(例如/tmp /)中,请将-C选项传递给tar命令

tar -xvzf wordpress.tar.gz -C /tmp/

仅从tar包提取单个文件

假设您要获取README.txt,请运行:

tar -xvzf wordpress.tar.gz README.txt

一个可以使用通配符提取多个文件。
例如,所有* .txt或* .php文件,运行:

tar -xvzf wordpress.tar.gz *.php
tar -xvzf wordpress.tar.gz *.txt

如何列出和搜索tar归档文件的内容

通过-ztvf,如下所示:

tar -ztvf wordpress.tar.gz

输出示例:

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 2016-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 2016-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/

使用grep命令搜索文件:

tar -ztvf wordpress.tar.gz | grep INSTALL.txt

如何创建tar包存档

语法为:

tar -cvf backups.tar /var/www/html/

通过gzip添加压缩:

tar -zcvf backups.tar.gz /var/www/html/

要应用bzip2压缩并创建tar包,请运行

tar -jcvf backups.tar.bz2 /var/www/html/

可以使用以下语法提取此类文件:

tar -jxvf backups.tar.bz2 ## gzip compression ##
tar -jxvf backups.tar.bz2 ## bzip2 compression ##