CentOS8-linux安装 tailf命令

这篇博客详细介绍了在CentOS8系统中如何安装tailf命令,包括tailf命令的功能简介、安装过程和解决gcc缺失的问题。通过编译源代码并安装gcc-c++解决依赖,从而成功安装tailf。

CentOS8-linux安装 tailf命令

一、起因

我安装的 CentOS8-linux 中,没有tailf命令,在网上搜了安装的方法,整理如下。可能不是最简单的,但亲测有效。

二、tailf 简介

(网上搜的,如果不感兴趣,可以直接跳过!)

  1. tailf命令几乎等同于tail -f,严格说来应该与tail --follow=name更相似些。

  2. 当文件改名之后它也能继续跟踪,特别适合于日志文件的跟踪(follow the growth of a log file)。

  3. 与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件(It is similar to tail -f but does not access the file when it is not growing. This has the side effect of not updating the access time for the file, so a filesystem flush does not occur periodically when no log activity is happening.)。

  4. tailf特别适合那些便携机上跟踪日志文件,因为它能省电,因为减少了磁盘访问嘛(tailf is extremely useful for monitoring log files on a laptop when logging is infrequent and the user desires that the hard disk spin down to conserve battery life.)。

  5. tailf命令不是个脚本,而是一个用C代码编译后的二进制执行文件,某些Linux安装之后没有这个命令,本文提供了怎么编译安装tailf命令的方法。

三、使用格式

格式:tailf logfile
动态跟踪日志文件logfile,最初的时候默认打印文件的最后10行内容。

四、安装

  1. 将下面代码存储到 tailf.c 文件中,上传到 CentOS8-linux 中(上传方式:方式一,可以直接拖到 /usr/local或者 /usr/local/tmp目录下,tmp 是我自己建的;方式二,使用 FileZilla 等基于 FTP协议的客户端工具)
/* tailf.c -- tail a log file and then follow it 
 * Created: Tue Jan  9 15:49:21 1996 by faith@acm.org 
 * Copyright 1996, 2003 Rickard E. Faith (faith@acm.org) 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: 
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
 * OTHER DEALINGS IN THE SOFTWARE. 
 *  
 * less -F and tail -f cause a disk access every five seconds.  This 
 * program avoids this problem by waiting for the file size to change. 
 * Hence, the file is not accessed, and the access time does not need to be 
 * flushed back to disk.  This is sort of a "stealth" tail. 
 */  
  
#include <stdio.h>  
#include <stdlib.h>  
#include <unistd.h>  
#include <malloc.h>  
#include <sys/stat.h>  
//#include "nls.h"  
#define _(s) s  
  
static size_t filesize(const char *filename)  
{  
    struct stat sb;  
  
    if (!stat(filename, &sb)) return sb.st_size;  
    return 0;  
}  
  
static void tailf(const char *filename, int lines)  
{  
    char **buffer;  
    int  head = 0;  
    int  tail = 0;  
    FILE *str;  
    int  i;  
  
    if (!(str = fopen(filename, "r"))) {  
    fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);  
    perror("");  
    exit(1);  
    }  
  
    buffer = malloc(lines * sizeof(*buffer));  
    for (i = 0; i < lines; i++) buffer[i] = malloc(BUFSIZ + 1);  
  
    while (fgets(buffer[tail], BUFSIZ, str)) {  
    if (++tail >= lines) {  
        tail = 0;  
        head = 1;  
    }  
    }  
  
    if (head) {  
    for (i = tail; i < lines; i++) fputs(buffer[i], stdout);  
    for (i = 0; i < tail; i++)     fputs(buffer[i], stdout);  
    } else {  
    for (i = head; i < tail; i++)  fputs(buffer[i], stdout);  
    }  
    fflush(stdout);  
  
    for (i = 0; i < lines; i++) free(buffer[i]);  
    free(buffer);  
}  
  
int main(int argc, char **argv)  
{  
    char       buffer[BUFSIZ];  
    size_t     osize, nsize;  
    FILE       *str;  
    const char *filename;  
    int        count;  
  
    //setlocale(LC_ALL, "");  
    //bindtextdomain(PACKAGE, LOCALEDIR);  
    //textdomain(PACKAGE);  
  
    if (argc != 2) {  
    fprintf(stderr, _("Usage: tailf logfile\n"));  
    exit(1);  
    }  
  
    filename = argv[1];  
  
    tailf(filename, 10);  
  
    for (osize = filesize(filename);;) {  
    nsize = filesize(filename);  
    if (nsize != osize) {  
        if (!(str = fopen(filename, "r"))) {  
        fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);  
        perror(argv[0]);  
        exit(1);  
        }  
        if (!fseek(str, osize, SEEK_SET))  
                while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)  
                    fwrite(buffer, 1, count, stdout);  
        fflush(stdout);  
        fclose(str);  
        osize = nsize;  
    }  
    usleep(250000);     /* 250mS */  
    }  
    return 0;  
}  
   

  1. 运行如下指令
    gcc -o /usr/bin/tailf tailf.c
    如图:
    安装 tailf 命令的指令
    注意:可能出现如下异常
    异常
    原因:这是因为没有安装 gcc 命令
    解决方案(必须联网):很简单,只需要一条指令安装 gcc 指令即可,如下:
    yum -y install gcc-c++
    需要下载大约 63Mb的资源,下载完后会自动安装好。
    再执行指令 gcc -o /usr/bin/tailf tailf.c 就能通过了,如下图:
    通过了
    成功后的示例图:
    效果
    搞定!
Re: 《文件过滤及内容编辑处理命令》 ================================================== 本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到极致,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述! 《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》---------------------------------    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值