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

简介: 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.


目录
相关文章
|
7月前
|
Linux 应用服务中间件 Shell
二、Linux文本处理与文件操作核心命令
熟悉了Linux的基本“行走”后,就该拿起真正的“工具”干活了。用grep这个“放大镜”在文件里搜索内容,用find这个“探测器”在系统中寻找文件,再用tar把东西打包带走。最关键的是要学会使用管道符|,它像一条流水线,能把这些命令串联起来,让简单工具组合出强大的功能,比如 ps -ef | grep 'nginx' 就能快速找出nginx进程。
795 1
二、Linux文本处理与文件操作核心命令
|
7月前
|
安全 Ubuntu Unix
一、初识 Linux 与基本命令
玩转Linux命令行,就像探索一座新城市。首先要熟悉它的“地图”,也就是/根目录下/etc(放配置)、/home(住家)这些核心区域。然后掌握几个“生存口令”:用ls看周围,cd去别处,mkdir建新房,cp/mv搬东西,再用cat或tail看文件内容。最后,别忘了随时按Tab键,它能帮你自动补全命令和路径,是提高效率的第一神器。
1243 58
|
6月前
|
存储 安全 Linux
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
Linux虚拟机遇紧急模式?别慌!多因磁盘挂载失败。本文教你通过日志定位问题,用`xfs_repair`等工具修复文件系统,三步快速恢复。掌握查日志、修磁盘、验重启,轻松应对紧急模式,保障系统稳定运行。
1140 2
|
7月前
|
缓存 监控 Linux
Linux内存问题排查命令详解
Linux服务器卡顿?可能是内存问题。掌握free、vmstat、sar三大命令,快速排查内存使用情况。free查看实时内存,vmstat诊断系统整体性能瓶颈,sar实现长期监控,三者结合,高效定位并解决内存问题。
652 0
Linux内存问题排查命令详解
|
监控 Linux Perl
【专栏】Linux 命令小技巧:显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分
【4月更文挑战第28天】本文介绍了Linux中显示文件指定行内容的方法,包括使用`head`和`tail`命令显示文件头尾部分,利用`sed`的行号指定功能以及`awk`处理文本数据。文章还列举了在代码审查、日志分析和文本处理中的应用场景,并提醒注意文件编码、行号准确性及命令组合使用。通过练习和实践,可以提升Linux文本文件处理的效率。
648 1
|
监控 Linux
linux(十六)文件和目录相关命令-查看文件头部内容head与查看文件尾部内容tail
linux(十六)文件和目录相关命令-查看文件头部内容head与查看文件尾部内容tail
404 0
|
Shell Linux
Linux查看文件内容命令:cat、more、less、head、tail、grep、wc(上)
1 查看文件内容 1.1 cat命令 cat( 英文全拼:concatenate )命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。
1421 0
|
Linux
8.Linux文件管理命令---head显示文件头部--tail显示文件尾部
8.Linux文件管理命令---head显示文件头部--tail显示文件尾部
488 0
|
Linux
Linux查看文件内容命令:cat、more、less、head、tail、grep、wc(下)
1 查看文件内容 1.1 cat命令 cat( 英文全拼:concatenate )命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。
513 0