查看文件可以说是最常用的功能之一了,比如我们的程序出现问题,经常要检查log文件,这时候就要用到查看文件的功能了,经常用到的有以下这几个命令:
=============================================================================================
Description: Concatenate FILE(s), or standard input, to standard output. 主要功能就是将文件内容连续显示在屏幕上面
-A, --show-all, equivalent to -vET. 显示所有(可显示一些特殊字符,而不只是空白)
-b, --number-nonblank, number nonempty output lines. 显示行号(仅针对非空白行,空白行不显示行号)
-e, equivalent to -vE
-E, --show-ends, display $ at end of each line. 以$结束行
-n, --number, number all output lines. 显示行号,包括空白行
-s, --squeeze-blank, suppress repeated empty output lines. 压缩多余的空余行
-t, equivalent to -vT
-T, --show-tabs, display TAB characters as ^I. 用^I显示tab键
-u, (ignored)
-v, --show-nonprinting, use ^ and M- notation, except for LFD and TAB. 显示出一些看不出来的特殊字符,除了LFD和TAB
--help, display this help and exit. 显示帮助信息
--version, output version information and exit. 显示版本信息
=============================================================================================
Description: Write each FILE to standard output, last line first. With no FILE, or when FILE is -, read standard input. Mandatory arguments to long options are mandatory for short options too. 从文件的最后一行开始输出。当没有文件输入,从标准输入读取。
-b, --before, attach the separator before instead of after. 分隔符放在开头而不是结尾
-r, --regex, interpret the separator as a regular expression. 分隔符采用正则表达式
-s, --separator=STRING, use STRING as the separator instead of newline. 用STRING代替newLine作为分隔符
--help, display this help and exit. 显示帮助信息
--version, output version information and exit. 显示版本信息
=============================================================================================
Description: Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.Mandatory arguments to long options are mandatory for short options too. 输出文件,并显示行号
-b, --body-numbering=STYLE, use STYLE for numbering body lines. 使用指定样式STYLE显示body行号
-d, --section-delimiter=CC, use CC for separating logical pages. 用CC作为分隔符
-f, --footer-numbering=STYLE, use STYLE for numbering footer lines. 使用指定样式STYLE显示footer行号
-h, --header-numbering=STYLE, use STYLE for numbering header lines. 使用指定样式STYLE显示header行号
-i, --line-increment=NUMBER, line number increment at each line. 行号以NUMBER递增
-l, --join-blank-lines=NUMBER, group of NUMBER empty lines counted as one. 组合空白行,number个空白行共用一个行号
-n, --number-format=FORMAT, insert line numbers according to FORMAT. 指定显示行号的格式FORMAT
-p, --no-renumber, do not reset line numbers at logical pages. 不重置行号
-s, --number-separator=STRING, add STRING after (possible) line number. 在行号后加上STRING作为分隔符
-v, --starting-line-number=NUMBER, first line number on each logical page. 以NUMBER为起始行号
-w, --number-width=NUMBER, use NUMBER columns for line numbers. 指定行号的位数为NUMBER
--help, display this help and exit. 显示帮助信息
--version, output version information and exit. 显示版本信息
CC是用来分隔逻辑页面的两个分隔符,第二个字符空缺的话默认为'.',输入的时候注意用'\\'反义'\'。
STYLE:
a:number all lines 标记所有行号
t:number only nonempty lines 只标记非空行行号
n:number no lines 不标记行号
FORMAT:
ln: left justified, no leading zeros 左对齐,不补零
rn: right justified, no leading zeros 右对齐,不补零
rz: right justified, leading zeros 右对齐,补零
这里随便拎个鸟哥的例子:
范例一:用 nl 列出 /etc/issue 的内容
[root@www ~]# nl /etc/issue
1 CentOS release 5.3 (Final)
2 Kernel \r on an \m
# 注意看,这个文件其实有三行,第三行为空白(没有任何字节),
# 因为他是空白行,所以 nl 不会加上行号喔!如果确定要加上行号,可以这样做:
[root@www ~]# nl -b a /etc/issue
1 CentOS release 5.3 (Final)
2 Kernel \r on an \m
3
# 呵呵!行号加上来罗~那么如果要让行号前面自动补上 0 呢?可这样
[root@www ~]# nl -b a -n rz /etc/issue
000001 CentOS release 5.3 (Final)
000002 Kernel \r on an \m
000003
# 嘿嘿!自动在自己栏位的地方补上 0 了~默认栏位是六位数,如果想要改成 3 位数?
[root@www ~]# nl -b a -n rz -w 3 /etc/issue
001 CentOS release 5.3 (Final)
002 Kernel \r on an \m
003
# 变成仅有 3 位数罗~
=============================================================================================
more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ]
DESCRIPTION: More is a filter for paging through text one screenful at a time. 单次显示一屏文件
-num, This option specifies an integer which is the screen size (in lines). 设置屏幕大小
-d, more will prompt the user with the message "[Press space to continue, 'q' to quit.]" and will display "[Press 'h' for instructions.]" instead of ringing the bell when an illegal key is pressed. 显示提示信息,而不是在输入非法字符时响警示音
-l, more usually treats ^L (form feed) as a special character, and will pause after any line that contains a form feed. The -l option will prevent this behavior. 禁用Ctil+L换页
-f, Causes more to count logical, rather than screen lines (i.e., long lines are not folded). 计算实际行数,而不是自动换行后的行数
-p, Do not scroll. Instead, clear the whole screen and then display the text. 不支持滚动,显示下一屏的时候将完全清除上一屏内容
-c, Do not scroll. Instead, paint each screen from the top, clearing the remainder of each line as it is displayed. 不支持滚动,从头显示,其余行当显示时再清除
-s, Squeeze multiple blank lines into one. 压缩多余空白行
-u, Suppress underlining. 禁用下划线
+/, The +/ option specifies a string that will be searched for before each file is displayed. 在显示前搜索该字符串,并从此字符串后开始显示
+num, Start at line number num. 从指定行开始显示
下面是显示文件时的一些可执行操作:
h or ? | Help: display a summary of these commands. If youforget all the other commands, remember this one. 显示帮助 |
Space | Display next k lines of text. Defaults to current screen size. 显示下一屏 |
z | Display next k lines of text. Defaults to current screen size. Argument becomes new default. 显示向下k行 |
Return | Display next k lines of text. Defaults to 1. Argument becomes new default. 向下显示k行,默认为1行 |
d or Ctrl-D | Scroll k lines. Default is current scroll size, initially 11. Argument becomes new default. 向下显示k行,默认为11行 |
q or Q or INTERRUPT | Exit. 退出 |
s | Skip forward k lines of text. Defaults to 1. 向前跳过默认一行 |
f | Skip forward k screenfuls of text. Defaults to 1. 向前跳过默认一页 |
b or Ctrl-B | Skip backwards k screenfuls of text. Defaults to 1. Only works with files, not pipes. 向后跳过默认一页 |
' | Go to place where previous search started. 跳至第一个搜索结果 |
= | Display current line number. 显示当前行数 |
/pattern | Search for kth occurrence of regular expression. Defaults to 1. 搜索pattern |
n | Search for kth occurrence of last r.e. Defaults to 1. 下一个搜索结果 |
!<cmd> or :!<cmd> | Execute <cmd> in a subshell. 执行命令 |
v | Start up an editor at current line. The editor is taken from the environment variable VISUAL if defined, or EDITOR if VISUAL is not defined, or defaults to "vi" if neither VISUAL nor EDITOR is defined. 在当前行开启编辑器 |
Ctrl-L | Redraw screen. 重绘屏幕 |
:n | Go to kth next file. Defaults to 1. 后一个文件(more可以同时打开多个文件) |
:p | Go to kth previous file. Defaults to 1. 前一个文件 |
:f | Display current file name and line number. 显示当前文件名和行数 |
. | Repeat previous command. 重复上一个命令 |
我不经常用more查看文件,但有的时候more和管道结合还是挺方便的,如
ls -l | more -5
#这样ls -l的结果就会按页输出,每页五行,可通过空格键翻页。当然,和下面less比起来,还是后者方便得多啊~
=============================================================================================
less -?
less --help
less -V
less --version
less [-[+]aBcCdeEfFgGiIJKLmMnNqQrRsSuUVwWX~] [-b space] [-h lines] [-j line] [-k keyfile] [-{oO} logfile] [-p pattern] [-P prompt] [-t tag] [-T tagsfile] [-x tab,...] [-y lines] [-[z] lines] [-# shift] [+[+]cmd] [--] [filename]...
Description: Less is a program similar to more, but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi. Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.) 简单点说就是less其实和more差不多,但是多了向前翻页的功能。另外还和其他命令做了简单的比较。
less命令的Option什么的太多了,重开了个随笔。这里只放些常用的command(^v表示Ctrl-v, ESC-SPACE表示先按下ESC键再按下空白键):
h or H |
Help: display a summary of these commands. If you forget all the other commands, remember this one. 显示commands的概要 |
SPACE or ^V or f or ^F | Scroll forward N lines. If N is more than the screen size, only the final screenful is displayed. 向下滚动N行 |
z | Like SPACE, but if N is specified, it becomes the new window size. 向下滚动一页,设置n值 |
ESC-SPACE | Like SPACE, but scrolls a full screenful, even if it reaches end-of-file in the process. 向下滚动一屏 |
RETURN or ^N or e or ^E or j or ^J | Scroll forward N lines, default 1. The entire N lines are displayed, even if N is more than the screen size. 向下滚动n行,默认为1,如果n大于屏幕行数,仍然全部显示 |
d or ^D |
Scroll forward N lines, default one half of the screen size. If N is specified, it becomes the new default for subsequent d and u commands. 下前滚动n行,默认半页 |
b or ^B or ESC-v | Scroll backward N lines, default one window. If N is more than the screen size, only the final screenful is displayed. 向前上动n行,默认一屏 |
w | Like ESC-v, but if N is specified, it becomes the new window size. 同ESC-v,但是如果n值设置过,则重设n值 |
y or ^Y or ^P or k or ^K | Scroll backward N lines, default 1. The entire N lines are displayed, even if N is more than the screen size. Warning: some systems use ^Y as a special job control character. 向上滚动n行,默认为1. |
u or ^U | Scroll backward N lines, default one half of the screen size. If N is specified, it becomes the new default for subsequent d and u commands. 向上滚动n行 |
ESC-) or RIGHTARROW | Scroll horizontally right N characters, default half the screen width (see the -# option). If a number N is specified, it becomes the default for future RIGHTARROW and LEFTARROW commands. While the text is scrolled, it acts as though the -S option (chop lines) were in effect. 水平向右n个字符,默认半屏 |
ESC-( or LEFTARROW | Scroll horizontally left N characters, default half the screen width (see the -# option). If a number N is specified, it becomes the default for future RIGHTARROW and LEFTARROW commands. 水平相左移动n个字符,默认半屏 |
r or ^R or ^L | Repaint the screen. 重绘屏幕 |
R | Repaint the screen, discarding any buffered input. Useful if the file is changing while it is being viewed. 重绘屏幕 |
F | Scroll forward, and keep trying to read when the end of file is reached. Normally this command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the "tail -f" command.) 向下滚动,即使已经到了文件结尾(类似于tail -f) |
g or < or ESC-< | Go to line N in the file, default 1 (beginning of file). (Warning: this may be slow if N is large.) 跳至n行,默认第一行 |
G or > or ESC-> | Go to line N in the file, default the end of the file. (Warning: this may be slow if N is large, or if N is not specified and standard input, rather than a file, is being read.) 跳至n行,默认最后一行 |
p or % | Go to a position N percent into the file. N should be between 0 and 100, and may contain a decimal point. 跳至N%的位置 |
P | Go to the line containing byte offset N in the file. 跳到第n个字符所在的行 |
{ | If a left curly bracket appears in the top line displayed on the screen, the { command will go to the matching right curly bracket. The matching right curly bracket is positioned on the bottom line of the screen. If there is more than one left curly bracket on the top line, a number N may be used to specify the N-th bracket on the line. 找到匹配第一个{的}所在的行 |
} | If a right curly bracket appears in the bottom line displayed on the screen, the } command will go to the matching left curly bracket. The matching left curly bracket is positioned on the top line of the screen. If there is more than one right curly bracket on the top line, a number N may be used to specify the N-th bracket on the line. 找到匹配最后一个}的{的行 |
( | Like {, but applies to parentheses rather than curly brackets. 同{ |
) | Like }, but applies to parentheses rather than curly brackets. 同} |
[ | Like {, but applies to square brackets rather than curly brackets. 同{ |
] | Like }, but applies to square brackets rather than curly brackets. 同} |
ESC-^F | Followed by two characters, acts like {, but uses the two characters as open and close brackets, respectively. For example, "ESC ^F < >" could be used to go forward to the > which matches the < in the top displayed line. 同{,但是可以自己设置起始和结尾字符 |
ESC-^B | Followed by two characters, acts like }, but uses the two characters as open and close brackets, respectively. For example, "ESC ^B < >" could be used to go backward to the < which matches the > in the bottom displayed line. 同},但是各自用两个字符作为起始和结尾 |
m | Followed by any lowercase letter, marks the current position with that letter. 用指定字符标记当前位置 |
' | (Single quote.) Followed by any lowercase letter, returns to the position which was previously marked with that letter. Followed by another single quote, returns to the position at which the last "large" movement command was executed. Followed by a ^ or $, jumps to the beginning or end of the file respectively. Marks are preserved when a new file is examined, so the ' command can be used to switch between input files. 跳至指定标记所在位置。 |
^X^X | Same as single quote. 同' |
/pattern |
Search forward in the file for the N-th line containing the pattern. N defaults to 1. The pattern is a regular expression, as recognized by the regular expression library supplied by your system. The search starts at the second line displayed (but see the -a and -j options, which change this). 向后搜索 ^N or !: Search for lines which do NOT match the pattern. 查找不匹配的行 以上的使用方法是输在/和pattern之间 |
?pattern |
Search backward in the file for the N-th line containing the pattern. The search starts at the line immediately before the top line displayed. 向前搜索 ^N or !: Search for lines which do NOT match the pattern. 查找不匹配的行 |
ESC-/pattern | Same as "/*". 同/* |
ESC-?pattern | Same as "?*". 同?* |
n | Repeat previous search, for N-th line containing the last pattern. If the previous search was modified by ^N, the search is made for the N-th line NOT containing the pattern. If the previous search was modified by ^E, the search continues in the next (or previous) file if not satisfied in the current file. If the previous search was modified by ^R, the search is done without using regular expressions. There is no effect if the previous search was modified by ^F or ^K. 下一个搜索结果 |
N | Repeat previous search, but in the reverse direction. 与n反方向,下一个搜索结果 |
ESC-n | Repeat previous search, but crossing file boundaries. The effect is as if the previous search were modified by *. 下一个搜索结果,支持文件之间的跳跃 |
ESC-N | Repeat previous search, but in the reverse direction and crossing file boundaries. 同ESC-n,不过为反方向 |
ESC-u | Undo search highlighting. Turn off highlighting of strings matching the current search pattern. If highlighting is already off because of a previous ESC-u command, turn highlighting back on. Any search command will also turn highlighting back on. (Highlighting can also be disabled by toggling the -G option; in that case search commands do not turn highlighting back on.) 关闭当前搜索的高亮显示,若高亮之前被ESC-u关闭,则还原之前设置 |
&pattern |
Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type & immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden.只显示匹配pattern的行,若pattern为空,会显示所有行 Certain characters are special as in the / command: ^N or !: Display only lines which do NOT match the pattern. 仅显示没有匹配的行 |
:e [filename] | Examine a new file. If the filename is missing, the "current" file (see the :n and :p commands below) from the list of files in the command line is re-examined. A percent sign (%) in the filename is replaced by the name of the current file. A pound sign (#) is replaced by the name of the previously examined file. However, two consecutive percent signs are simply replaced with a single percent sign. This allows you to enter a filename that contains a percent sign in the name. Similarly, two consecutive pound signs are replaced with a single pound sign. The filename is inserted into the command line list of files so that it can be seen by subsequent :n and :p commands. If the filename consists of several files, they are all inserted into the list of files and the first one is examined. If the filename contains one or more spaces, the entire filename should be enclosed in double quotes (also see the -" option). 执行新文件,若文件名缺失,则当前文件重新执行 |
^X^V or E | Same as :e. Warning: some systems use ^V as a special literalization character. On such systems, you may not be able to use ^V. 同e命令 |
:n | Examine the next file (from the list of files given in the command line). If a number N is specified, the N-th next file is examined. 执行后一个文件,如果n是指定的值,那么会执行后第n个文件 |
:p | Examine the previous file in the command line list. If a number N is specified, the N-th previous file is examined. 执行前一个文件,如果n是指定的值,那么会之情第前n个文件 |
:x | Examine the first file in the command line list. If a number N is specified, the N-th file in the list is examined. 执行第一个文件,如果n是指定的值,那么会执行第n个文件 |
:d | Remove the current file from the list of files. 从打开的文件列表中删除当前文件 |
t | Go to the next tag, if there were more than one matches for the current tag. See the -t option for more details about tags. 移至下一个tag |
T | Go to the previous tag, if there were more than one matches for the current tag. 移至前一个tag |
= or ^G or :f | Prints some information about the file being viewed, including its name and the line number and byte offset of the bottom line being displayed. If possible, it also prints the length of the file, the number of lines in the file and the percent of the file above the last displayed line. 打印当前文件的相关信息:文件名、行数等 |
- | Followed by one of the command line option letters, this will change the setting of that option and print a message describing the new setting. If a ^P (CONTROL-P) is entered immediately after the dash, the setting of the option is changed but no message is printed. If the option letter has a numeric value (such as -b or -h), or a string value (such as -P or -t), a new value may be entered after the option letter. If no new value is entered, a message describing the current setting is printed and nothing is changed. 改变option的设置并打印消息,option后要追加value,若没有value,不会改变option的值,只会打印出当前值 |
-- | Like the - command, but takes a long option name (see OPTIONS below) rather than a single option letter. You must press RETURN after typing the option name. A ^P immediately after the second dash suppresses printing of a message describing the new setting, as in the - command. 同-命令,但是用长option代替 |
-+ | Followed by one of the command line option letters this will reset the option to its default setting and print a message describing the new setting. (The "-+X" command does the same thing as "-+X" on the command line.) This does not work for string-valued options. 恢复option默认值,并打印信息 |
--+ | Like the -+ command, but takes a long option name rather than a single option letter. 同-+command,但是用长option代替 |
-! | Followed by one of the command line option letters, this will reset the option to the "opposite" of its default setting and print a message describing the new setting. This does not work for numeric or string-valued options. 做相反设置,并打印信息 |
--! | Like the -! command, but takes a long option name rather than a single option letter. 和-!相同,用长option代替短的 |
_ | (Underscore.) Followed by one of the command line option letters, this will print a message describing the current setting of that option. The setting of the option is not changed. 打印出option的说明 |
__ | (Double underscore.) Like the _ (underscore) command, but takes a long option name rather than a single option letter. You must press RETURN after typing the option name. 同_,但是用长option名。输完option名之后按下RETURN键 |
+cmd | Causes the specified cmd to be executed each time a new file is examined. For example, +G causes less to initially display each file starting at the end rather than the beginning. 每次less打开一个新文件时执行的命令 |
V | Prints the version number of less being run. 显示版本号 |
q or Q or :q or :Q or ZZ | Exits less. 退出 |
=============================================================================================
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. head很简单,就是显示一个文件的头几行。
-c, --bytes=[-]K, print the first K bytes of each file; with the leading '-', print all but the last K bytes of each file. 显示头k个byte,以-开头表示打印出除了最后k个byte以外的所有内容。
-n, --lines=[-]K, print the first K lines instead of the first 10; with the leading '-', print all but the last K lines of each file. 显示头k行,以-开头表示打印出除了最后k行以外的所有内容。
-q, --quiet, --silent, never print headers giving file names. 不打印出文件名
-v, --verbose, always print headers giving file names. 总是打印出文件名
--help, display this help and exit. 显示帮助信息。
--version, output version information and exit. 显示版本信息并退出命令。
=============================================================================================
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. tail和head相反,是打印最后几行。这个命令很有用,可以动态打印文件。
-c, --bytes=K,output the last K bytes; alternatively, use -c +K to output bytes starting with the Kth of each file. 打印最后k个byte。如果以+开头表示打印从k个byte开始向后的所有内容。
-f, --follow[={name|descriptor}], output appended data as the file grows. 动态显示。
-F, same as --follow=name --retry.
-n, --lines=K, output the last K lines, instead of the last 10; or use -n +K to output lines starting with the Kth. 打印最后k行,以+开头表示从k行起打印。
--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. 与--follow=name一起使用,如果连续n次都没有更新到新内容,就重新打开文件。
--pid=PID, with -f, terminate after process ID, PID dies. 和-f一起使用,当pid所指向的程序进程结束时,不再动态更新。
-q, --quiet, --silent, never output headers giving file names. 不打印文件名。
--retry, keep trying to open a file even when it is or becomes 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 atleast once every N seconds. 和-f一起用,每次搜索更新时休眠n秒。
-v, --verbose, always output headers giving file names. 总是打印出文件名。
--help, display this help and exit. 显示帮助信息。
--version, output version information and exit. 显示版本并退出。