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
目录
相关文章
|
1天前
|
存储 算法 Linux
【Linux】文件打包解压_tar_zip
【Linux】文件打包解压_tar_zip
14 0
【Linux】文件打包解压_tar_zip
|
1天前
|
Linux 开发者
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
【Linux】:文件查看 stat、cat、more、less、head、tail、uniq、wc
10 1
|
2天前
|
Linux Python
【专栏】Linux 中某个目录中的文件数如何查看?这篇教程分分钟教会你!
【4月更文挑战第28天】在Linux中查看目录文件数的方法包括:使用`ls`结合`wc -l`,如`ls &lt;directory_path&gt; | wc -l`;使用`find`命令,如`find &lt;directory_path&gt; -type f | wc -l`;使用`tree`命令,如`tree &lt;directory_path&gt;`(可能需额外安装);以及通过编程方式,例如Python代码实现。注意权限、效率和选择适用方法以提升操作效率。本文提供了详细步骤和示例,助你轻松掌握!
|
2天前
|
监控 Linux Perl
【专栏】Linux 命令小技巧:显示文件指定行的内容
【4月更文挑战第28天】本文介绍了Linux中显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分,利用`sed`的行号指定功能以及`awk`处理文本数据。文章还列举了在代码审查、日志分析和文本处理中的应用场景,并提醒注意文件编码、行号准确性及命令组合使用。通过练习和实践,可以提升Linux文本文件处理的效率。
|
2天前
|
存储 数据挖掘 Linux
【专栏】教你如何快速在 Linux 中找到某个目录中最大的文件
【4月更文挑战第28天】在 Linux 中查找目录中最大文件的方法包括:使用 `du` 结合 `sort`,`find` 结合 `xargs` 和 `sort`,以及编写 Python 脚本。这些技巧适用于服务器管理、数据分析和文件清理等场景,能帮助用户快速定位大文件进行分析、清理或优化。注意文件权限、目录深度和文件系统类型可能影响结果,可结合其他命令增强功能。
|
4月前
|
Linux 索引 关系型数据库
Linux 终端命令之文件目录操作,对比Dos相关命令
Linux 终端命令之文件目录操作,对比Dos相关命令
79 0
Linux 终端命令之文件目录操作,对比Dos相关命令
|
4月前
|
Shell Unix Linux
Linux 终端命令之文件浏览(3) less
Linux 终端命令之文件浏览(3) less
31 0
Linux 终端命令之文件浏览(3) less
|
4月前
|
Go Linux Shell
Linux 终端命令之文件浏览(2) more
Linux 终端命令之文件浏览(2) more
32 0
Linux 终端命令之文件浏览(2) more
|
4月前
|
Linux
Linux 终端命令之文件浏览(1) cat
Linux 终端命令之文件浏览(1) cat
32 0
Linux 终端命令之文件浏览(1) cat
|
4月前
|
Shell 机器学习/深度学习 Linux
Linux 终端操作命令(2)内部命令
Linux 终端操作命令(2)内部命令
21 0
Linux 终端操作命令(2)内部命令