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

18647a9c4d804f0eacedc1984bee2940.png

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博客

猜你喜欢

转载自blog.csdn.net/boysoft2002/article/details/132256547