Linux 终端命令之文件浏览(4) head, tail

这篇具有很好参考价值的文章主要介绍了Linux 终端命令之文件浏览(4) head, tail。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Linux 终端命令之文件浏览(4) head, tail,Linux,linux

Linux 文件浏览命令

cat, more, less, head, tail,此五个文件浏览类的命令皆为外部命令。

hann@HannYang:~$ which cat
/usr/bin/cat
hann@HannYang:~$ which more
/usr/bin/more
hann@HannYang:~$ which less
/usr/bin/less
hann@HannYang:~$ which head
/usr/bin/head
hann@HannYang:~$ which tail
/usr/bin/tail

(4) head

英文帮助

NAME
       head - output the first part of files

SYNOPSIS
       head [OPTION]... [FILE]...

DESCRIPTION
       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.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -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
              never print headers giving file names

       -v, --verbose
              always print headers giving file names

       -z, --zero-terminated
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

AUTHOR
       Written by David MacKenzie and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report head translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2018 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.

SEE ALSO
       tail(1)

       Full documentation at: <https://www.gnu.org/software/coreutils/head>
       or available locally via: info '(coreutils) head invocation'

中文注释

head [参数] 文件名——显示文件的前几行

参数选项:
 -n num:显示文件的前num行。
 -c num:显示文件的前num字节的字符,num可以使用K,KB,M,MB等数量单位。
缺省时,head显示文件的前10行。

-v / -q : 首行是否显示文件名。
-z :行分隔符为NUL。一般地,head -z myfile 就等效 cat myfile。

示例:

hann@HannYang:~$ head -n 6 cmds.txt
alias - Define or display aliases.
alias: alias [-p] [name[=value] ... ]
bg - Move jobs to the background.
bg: bg [job_spec ...]
bind - Set Readline key bindings and variables.
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
hann@HannYang:~$ head cmds.txt
alias - Define or display aliases.
alias: alias [-p] [name[=value] ... ]
bg - Move jobs to the background.
bg: bg [job_spec ...]
bind - Set Readline key bindings and variables.
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
break - Exit for, while, or until loops.
break: break [n]
builtin - Execute shell builtins.
builtin: builtin [shell-builtin [arg ...]]
hann@HannYang:~$ head -c 35 cmds.txt
alias - Define or display aliases.
hann@HannYang:~$ head -c 50 cmds.txt
alias - Define or display aliases.
alias: alias [-hann@HannYang:~$ head -c 1K cmds.txt
alias - Define or display aliases.
alias: alias [-p] [name[=value] ... ]
bg - Move jobs to the background.
bg: bg [job_spec ...]
bind - Set Readline key bindings and variables.
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
break - Exit for, while, or until loops.
break: break [n]
builtin - Execute shell builtins.
builtin: builtin [shell-builtin [arg ...]]
caller - Return the context of the current subroutine call.
caller: caller [expr]
case - Execute commands based on pattern matching.
case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
cd - Change the shell working directory.
cd: cd [-L|[-P [-e]] [-@]] [dir]
command - Execute a simple command or display information about commands.
command: command [-pVv] command [arg ...]
compgen - Display possible completions depending on the options.
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist]  [-F function] [-C cohann@HannYang:~$ head -c 1KB cmds.txt
alias - Define or display aliases.
alias: alias [-p] [name[=value] ... ]
bg - Move jobs to the background.
bg: bg [job_spec ...]
bind - Set Readline key bindings and variables.
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
break - Exit for, while, or until loops.
break: break [n]
builtin - Execute shell builtins.
builtin: builtin [shell-builtin [arg ...]]
caller - Return the context of the current subroutine call.
caller: caller [expr]
case - Execute commands based on pattern matching.
case: case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac
cd - Change the shell working directory.
cd: cd [-L|[-P [-e]] [-@]] [dir]
command - Execute a simple command or display information about commands.
command: command [-pVv] command [arg ...]
compgen - Display possible completions depending on the options.
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlishann@HannYang:~$ head -v cmds.txt
==> cmds.txt <==
alias - Define or display aliases.
alias: alias [-p] [name[=value] ... ]
bg - Move jobs to the background.
bg: bg [job_spec ...]
bind - Set Readline key bindings and variables.
bind: bind [-lpsvPSVX] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
break - Exit for, while, or until loops.
break: break [n]
builtin - Execute shell builtins.
builtin: builtin [shell-builtin [arg ...]]

注意区别:

-c 35  和 -c 50 的区别,前者刚好有换行符结尾。

-c 1K 和 -c 1KB 的区别,前者1024后者1000;M和MB,G和GB的区别同理。

--help 简要帮助

hann@HannYang:~$ head --help
Usage: head [OPTION]... [FILE]...
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.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -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    never print headers giving file names
  -v, --verbose            always print headers giving file names
  -z, --zero-terminated    line delimiter is NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report head translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/head>
or available locally via: info '(coreutils) head invocation'

--version 版本号

hann@HannYang:~$ head --version
head (GNU coreutils) 8.30
Copyright (C) 2018 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 David MacKenzie and Jim Meyering.

(5) tail

英文帮助

NAME
       tail - output the last part of files

SYNOPSIS
       tail [OPTION]... [FILE]...

DESCRIPTION
       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.

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -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
              line delimiter is NUL, not newline

       --help display this help and exit

       --version
              output version information and exit

       NUM may have a multiplier suffix: b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024, GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

       With  --follow  (-f),  tail defaults to following the file descriptor, which means that even if a tail'ed file is renamed, tail will continue to track its end.  This default behavior is not desirable when you really want to track the actual name of the file, not the file descriptor (e.g., log rotation).  Use --follow=name in that case.  That causes tail to track the named file in a way that accommodates renaming, removal and creation.

AUTHOR
       Written by Paul Rubin, David MacKenzie, Ian Lance Taylor, and Jim Meyering.

REPORTING BUGS
       GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
       Report tail translation bugs to <https://translationproject.org/team/>

COPYRIGHT
       Copyright © 2018 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.

SEE ALSO
       head(1)

       Full documentation at: <https://www.gnu.org/software/coreutils/tail>
       or available locally via: info '(coreutils) tail invocation'

中文注释

tail [参数] 文件名——显示文件的末尾几行
参数选项:
 -n num:显示文件的末尾num行。
 -c num:显示文件的末尾num字节的字符。
tail命令和head命令相反,它显示文件的末尾。
缺省时,tail命令显示文件的末尾10行

这两个参数与head基本相同,还有-q -v -z也一样。

tail命令另外还多了几个参数: -f -s --pid --retry等。

-f:该参数用于实时监控文件并输出最新的内容。例如,tail -f -n 10 /var/log/syslog表示实时监控/var/log/syslog文件的最后10行内容,并输出最新的内容。
-s或--sleep-interval:这个参数用于指定每次输出文件内容之间的休眠时间。例如,tail -f -s 2表示在每次输出文件内容之间休眠2秒。
--pid:该参数用于指定要监控的进程号(PID)。例如,tail -f --pid 12345表示监控进程号12345的进程。
--retry:该参数用于在监控的进程重新启动后继续监控。例如,tail -f --pid 12345 --retry表示在进程重新启动后继续监控。

 --help 简要帮助

Usage: tail [OPTION]... [FILE]...
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.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -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    line delimiter is NUL, not newline
      --help     display this help and exit
      --version  output version information and exit

NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report tail translation bugs to <https://translationproject.org/team/>
Full documentation at: <https://www.gnu.org/software/coreutils/tail>
or available locally via: info '(coreutils) tail invocation'

--version 版本号

hann@HannYang:~$ tail --version
tail (GNU coreutils) 8.30
Copyright (C) 2018 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 Paul Rubin, David MacKenzie, Ian Lance Taylor,
and Jim Meyering.

相关阅读:

Linux 终端命令之文件浏览(1) cat_Hann Yang的博客-CSDN博客

Linux 终端命令之文件浏览(2) more_Hann Yang的博客-CSDN博客

Linux 终端命令之文件浏览(3) less_Hann Yang的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-673017.html

到了这里,关于Linux 终端命令之文件浏览(4) head, tail的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Linux:文件查看:《cat》《more》《less》《head》《tail》《wc》《grep》使用方法

    同样是查看为什么要有这么多查看方法??? 因为他们的用法和扩功能肯定不一样,选择与你需要匹配的一条命令可以节省时间的同时更快速 cat + 文件 可以直接查看文件内的内容  直接可以查看文件内的内容 要直接看更多的文件以空格隔开的方式一起查看文件 more + 文件

    2023年04月26日
    浏览(42)
  • Linux :: 【基础指令篇 :: 文件内容操作:(4)】:: head / tail 指令 :: 查看前后 n 行内容 | 指定查看文件的部分内容

    前言:本篇是 Linux 基本操作篇章的内容! 笔者使用的环境是基于腾讯云服务器:CentOS 7.6 64bit。 学习集: C++ 入门到入土!!!学习合集 Linux 从命令到网络再到内核!学习合集 注: 本文涉及文件内容查看或编辑,故有预先准备的测试示例,若读者无测试示例,可参照 第 0

    2024年02月08日
    浏览(58)
  • Linux 实时查看文件 tail 命令详解

    tail 命令用于显示文件的末尾内容,默认显示文件的最后 10 行。通常情况下,tail 命令用于实时查看日志文件,可以使用 -f 参数跟踪文件内容的变化。tail 命令的语法如下: tail 命令支持以下参数: 1. -n,–lines=K 该选项用于指定要显示的行数,K 为整数。例如,-n 20 表示显示

    2024年02月10日
    浏览(72)
  • Linux--获取某个区间文本的指令:head和tail

    语法: 功能: head 用来显示档案的开头至标准输出中,默认head命令打印其相应文件的开头10行。  选项: -n 行数  显示的行数 示例: ①生成默认十行  ②生成自定义行数 语法: 功能: 用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。常用查看日志文

    2024年02月12日
    浏览(38)
  • Linux命令200例:tail用来显示文件的末尾内容(常用)

    🏆作者简介,黑夜开发者,全栈领域新星创作者✌。CSDN专家博主,阿里云社区专家博主,2023年6月csdn上海赛道top4。 🏆数年电商行业从业经验,历任核心研发工程师,项目技术负责人。 🏆本文已收录于专栏:Linux命令大全。 🏆本专栏我们会通过 具体的系统的命令讲解 加上

    2024年02月12日
    浏览(48)
  • Linux文本处理(cat、more、head、less、tail)

    可以用来显示文本文件的内容(类似于 DOS 下的 type 命令),也可以把几个文件内容附加到另一个文件中,即连接合并文件。 cat 命令的基本格式如下: [root@localhost ~]# cat [选项] 文件名 或者 [root@localhost ~]# cat 文件1 文件2 文件3   这两种格式中,前者用于显示文件的内容,常

    2024年02月15日
    浏览(74)
  • [Linux 基础] linux基础指令(2)head,tail,Cal,find,grep,zip/unzip,tar,bc,uname

    本篇文章接着上一篇基础指令继续讲,将一篇文章 点这里跳转 head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块, head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾。 语法: head [参数]… [文件]… 功能: head用来

    2024年02月08日
    浏览(52)
  • Linux 终端命令之文件目录操作,对比Dos相关命令

    目录 前言 基础命令(文件目录相关的) cd命令 【英文帮助】 【对应Dos命令】 pwd命令 【英文帮助】 【对应Dos命令】 ls命令 【英文帮助】 【对应Dos命令】 tree命令 【英文帮助】 【对应Dos命令】 mkdir命令 【英文帮助】 【对应Dos命令】 rmdir/rm命令 【英文帮助】 【对应Dos命令

    2024年02月11日
    浏览(38)
  • Linux tail & cat & grep 命令详解

    在屏幕上显示指定文件的末尾若干行 tail命令 用于输入文件中的尾部内容。 默认在屏幕上显示指定文件的末尾10行。 处理多个文件时会在各个文件之前附加含有文件名的行。 如果没有指定文件或者文件名为 - ,则读取标准输入。 如果表示字节或行数的 NUM 值之前有一个 + 号

    2024年02月06日
    浏览(41)
  • Linux tail命令详解和高级用法举例

    目            录 一、概述 二、tail命令解释 1.命令格式; 2.功能 3.选项 4.选项的基本用法 (1) 显示行号 (2)忽略指定字符数 (3) 不显示文件名 三.基本功能 1、基本用法 (1) 查看文件末尾内容 (2) 实时监控文件变化 2、高级用法 (1)组合使用1:把一个应用程

    2024年01月16日
    浏览(45)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包