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

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: Linux 终端命令之文件浏览(4) head, tail


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:

      Report head translation bugs to

COPYRIGHT

      Copyright © 2018 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later .

      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:

      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:

      Report tail translation bugs to

COPYRIGHT

      Copyright © 2018 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later .

      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:

      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.


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
7天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
7天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
4天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
25 9
|
3天前
|
缓存 运维 监控
Linux系统监控利器:探索常用命令及数据保存技巧
Linux系统监控利器:探索常用命令及数据保存技巧
23 4
Linux系统监控利器:探索常用命令及数据保存技巧
|
6天前
|
Linux 索引
linux 文件查找 和文件管理常用命令
linux 文件查找 和文件管理常用命令
15 0
|
6天前
|
安全 Unix Linux
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
一、linux 常用命令之 linux版本信息 系统管理与设置 持续更新******
13 0
|
8天前
|
Prometheus 监控 Cloud Native
【Linux】查看系统内存命令(详细讲解)
【Linux】查看系统内存命令(详细讲解)
|
8天前
|
监控 Linux
|
11天前
|
Linux Perl
Linux| Awk 中“next”命令奇用
Linux| Awk 中“next”命令奇用
26 9
|
12天前
|
Java Unix Linux
Linux系统常用命令速查手册
Linux系统常用命令速查手册