zoukankan      html  css  js  c++  java
  • 部分显示文件内容命令之head,tail,

    head

    显示文件内容前面多少字节-c或者多少行-n的内容

    [02:47:35 root@C8-3-55 ~]#head --help
    用法:head [选项]... [文件]...
    Print the first 10 lines of each FILE to standard output.
    With more than one FILE, precede each with a header giving the file name.
    
    如果没有指定文件,或者文件为"-",则从标准输入读取。
    
    必选参数对长短选项同时适用。
      -c, --bytes=[-]NUM       print the first NUM bytes of each file;
                                 with the leading '-', print all but the last
                                 NUM bytes of each file
      -n, --lines=[-]NUM       print the first NUM lines instead of the first 10;
                                 with the leading '-', print all but the last
                                 NUM lines of each file
      -q, --quiet, --silent 不显示包含给定文件名的文件头
      -v, --verbose         总是显示包含给定文件名的文件头
      -z, --zero-terminated    以 NUL 字符而非换行符作为行尾分隔符
          --help            显示此帮助信息并退出
          --version         显示版本信息并退出
    
    [02:50:02 root@C8-3-55 ~]#head -n 3 /etc/passwd
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    

    如果参数为负,则去尾
    例:-3表示从头开始显示到倒数第n-3行

    [02:59:27 root@C8-3-55 ~]#head -n -3 ./f1.txt
    1
    2
    3
    4
    5
    6
    7
    

    tail

    显示文件内容尾部多少字节或多少行的内容

    [02:51:39 root@C8-3-55 ~]#tail --help
    用法:tail [选项]... [文件]...
    Print the last 10 lines of each FILE to standard output.
    With more than one FILE, precede each with a header giving the file name.
    
    如果没有指定文件,或者文件为"-",则从标准输入读取。
    
    必选参数对长短选项同时适用。
      -c, --bytes=[+]NUM       output the last NUM bytes; or use -c +NUM to
                                 output starting with byte NUM of each file
      -f, --follow[={name|descriptor}]
                               output appended data as the file grows;
                                 an absent option argument means 'descriptor'
      -F                       same as --follow=name --retry
      -n, --lines=[+]NUM       output the last NUM lines, instead of the last 10;
                                 or use -n +NUM to output starting with line NUM
          --max-unchanged-stats=N
                               with --follow=name, reopen a FILE which has not
                                 changed size after N (default 5) iterations
                                 to see if it has been unlinked or renamed
                                 (this is the usual case of rotated log files);
                                 with inotify, this option is rarely useful
          --pid=PID            with -f, terminate after process ID, PID dies
      -q, --quiet, --silent    never output headers giving file names
          --retry              keep trying to open a file if it is inaccessible
      -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                                 (default 1.0) between iterations;
                                 with inotify and --pid=P, check process P at
                                 least once every N seconds
      -v, --verbose            always output headers giving file names
      -z, --zero-terminated    以 NUL 字符而非换行符作为行尾分隔符
          --help            显示此帮助信息并退出
          --version         显示版本信息并退出
    
    [02:52:14 root@C8-3-55 ~]#tail -n 3 /etc/passwd
    user98:x:8988:8988::/home/user98:/bin/bash
    user99:x:8989:8989::/home/user99:/bin/bash
    user100:x:8990:8990::/home/user100:/bin/bash
    

    如果参数为添加了+号的数,则刨头
    例:+3表示从尾开始显示到正数数第n-3行,或者从头第3行开始显示

    [02:59:44 root@C8-3-55 ~]#tail -n -3 ./f1.txt
    8
    9
    10
    [03:03:54 root@C8-3-55 ~]#tail -n +3 ./f1.txt
    3
    4
    5
    6
    7
    8
    9
    10
    

    tail的 -f选项

    shell编程中tail比较常用的-f选项,可以跟踪文件的变化。
    如果文件有追加内容,文件的尾部不断有新内容出现。
    在生产中经常用来查看日志。

    [03:04:40 root@C8-3-55 ~]#tail -f /var/log/messages
    Mar  7 01:53:32 C8-3-55 systemd[1]: Starting Cleanup of Temporary Directories...
    Mar  7 01:53:32 C8-3-55 systemd-tmpfiles[7254]: [/usr/lib/tmpfiles.d/mdadm.conf:1] Line references path below legacy directory /var/run/, updating /var/run/mdadm → /run/mdadm; please update the tmpfiles.d/ drop-in file accordingly.
    Mar  7 01:53:32 C8-3-55 systemd[1]: Started Cleanup of Temporary Directories.
    Mar  7 02:48:34 C8-3-55 systemd[1]: Starting dnf makecache...
    Mar  7 02:48:34 C8-3-55 dnf[26938]: Repository AppStream is listed more than once in the configuration
    Mar  7 02:48:34 C8-3-55 dnf[26938]: Repository extras is listed more than once in the configuration
    Mar  7 02:48:34 C8-3-55 dnf[26938]: Repository PowerTools is listed more than once in the configuration
    Mar  7 02:48:34 C8-3-55 dnf[26938]: Repository centosplus is listed more than once in the configuration
    Mar  7 02:48:34 C8-3-55 dnf[26938]: 元数据缓存近期已刷新。
    Mar  7 02:48:34 C8-3-55 systemd[1]: Started dnf makecache.
    

    -f相当于跟踪了文件的描述符

    查看日志和官方文档,是运维工作中解决问题的重要手段


    head和tail应用实例

    利用head和tail组合取出ens33配置文件中IP地址

    [03:17:34 root@C8-3-55 ~]#ifconfig ens33
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.55  netmask 255.0.0.0  broadcast 10.255.255.255
            inet6 fe80::f7bf:c1dd:bbf2:865  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:c2:cc:4e  txqueuelen 1000  (Ethernet)
            RX packets 19645  bytes 1407622 (1.3 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 49644  bytes 5698480 (5.4 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [03:18:55 root@C8-3-55 ~]#ifconfig ens33 | head -n 2 | tail -n 1
            inet 10.0.0.55  netmask 255.0.0.0  broadcast 10.255.255.255
    

    head和tail命令总结

    * * * 胖并快乐着的死肥宅 * * *
  • 相关阅读:
    循环神经网络
    第四次作业:卷积神经网络 part 3
    《Strip Pooling: Rethinking Spatial Pooling for Scene Parsing》——笔记
    《Selective Kernel Networks》——笔记
    《Non-local Neural Networks 》——笔记
    HybridSN中添加SE模块
    第三次作业:卷积神经网络 part 2
    第一次作业:深度学习及pytorch基础
    需要背的板子
    Codeforces 1474F. 1 2 3 4 ... 题解
  • 原文地址:https://www.cnblogs.com/bpzblog/p/14495178.html
Copyright © 2011-2022 走看看