導言
今天博文講述另一個查看文件內容的命令,這個命令是less,less和more命令很相似,但是相對強大了許多 。
less命令
作用
在linux官方說明中,把less是這麼解釋的“ opposite of more” ,可以說是與more命令相對,這就難以理解了,不過不影響我們對less命令的使用和了解。
具體來說,說到less就得說more,Less是類似於more的程序,與more不同,它允許文件中的向後移動以及向前移動。 此外,less不必在啓動之前讀取整個輸入文件,因此對於大輸入文件,它比文本編輯器(如 vi)啓動得更快。 less使用termcap(或某些系統上的terminfo),因此它可以在各種終端上運行,甚至支持硬拷貝終端。 (在硬拷貝終端上,應在屏幕頂部打印的線條以插入符號爲前綴。)
less強大而又方便,對於linux內部的man page則是調用less實現的,其實博主都感覺less不應該叫less,畢竟功能上比more更多。只能猜測可能是因爲less剛開始以很少的內容載入,而more載入整個文件的原因才這麼訂立命令名字的。
less支持[pageup] [pagedown] 等按鍵,又支持搜尋字符串的上下尋找(比起昨天說的more來說,尋找後,只能往後尋找好得多)。
用法
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]...
簡化來說是:
less [參數] 文件
詳細全選項參數解釋
在以下描述中,^ X表示control-X,即快捷鍵ctrl+X。
短選項 | 長選項 | 含義(作用) |
---|---|---|
-? |
--help |
此選項顯示less接受的命令摘要(與h命令相同)。 (根據shell解釋問號的方式,可能需要引用問號,因此:“ - \?”。) |
-a |
--search-skip-screen |
導致搜索在屏幕上顯示的最後一行之後開始,從而跳過屏幕上顯示的所有行。 默認情況下,搜索從屏幕上的第二行開始(或在最後找到的行之後;請參閱-j選項)。 |
-bn |
--buffers=n |
指定將用於每個文件的緩衝區空間量,以千字節(1024字節)爲單位。 默認情況下,每個文件使用64K的緩衝區空間(除非文件是管道;請參閱-B選項)。 -b選項指定應爲每個文件使用n千字節的緩衝區空間。 如果n爲-1,則緩衝區空間不受限制; 也就是說,整個文件可以讀入內存。 |
-B |
--auto-buffers |
默認情況下,從管道讀取數據時,會根據需要自動分配緩衝區。 如果從管道中讀取大量數據,則可能導致分配大量內存。 -B選項禁用管道緩衝區的這種自動分配,因此管道只使用64K(或-b選項指定的空間量)。 警告:使用-B可能導致錯誤顯示,因爲只有最近查看過的管道數據部分纔會保留在內存中; 任何早期數據都會丟失。 |
-c |
--clear-screen |
導致全屏重繪從頂行向下繪製。 默認情況下,通過從屏幕底部滾動來完成全屏重繪。 |
-C |
--CLEAR-SCREEN |
與-c相同,用於與較舊版本的less兼容。 |
-d |
--dumb |
如果終端是dumb的話,-d選項會禁止正常顯示的錯誤消息; 也就是說,缺少一些重要的功能,例如清除屏幕或向後滾動的功能。 -d選項不會改變dumb終端上的less行爲。 |
-Dxcolor |
--color=xcolor |
[僅限MS-DOS]設置顯示的文本的顏色。 x是單個字符,用於選擇正在設置顏色的文本類型:n =正常,s =突出顯示,d =粗體,u =下劃線,k =閃爍。 顏色是由句點分隔的一對數字。 第一個數字選擇前景色,第二個數字選擇文本的背景色。 單個數字N與N.M相同,其中M是正常背景顏色。 |
-e |
--quit-at-eof |
導致第二次到達文件結束時less自動退出。 默認情況下,退出less的唯一方法是通過“q”命令。 |
-E |
--QUIT-AT-EOF |
導致第一次到達文件結束時less自動退出。 |
-f |
--force |
強制打開非常規文件。 (非常規文件是目錄或設備專用文件。)還會在打開二進制文件時禁止顯示警告消息。 默認情況下,less會拒絕打開非常規文件。 請注意,即使設置了-f,某些操作系統也不允許讀取目錄。 |
-F |
--quit-if-one-screen |
如果可以在第一個屏幕上顯示整個文件,則會導致less自動退出。 |
-g |
--hilite-search |
通常情況下,less會突出顯示與上一個搜索命令匹配的所有字符串。 -g選項更改此行爲以僅突出顯示上一個搜索命令找到的特定字符串。 這可以導致運行速度比默認值快一些。 |
-G |
--HILITE-SEARCH |
-G選項禁止搜索命令找到的所有字符串突出顯示。 |
--old-bot |
恢復到屏幕行爲的舊底部。 如果長線在到達終端底部時未正確包裹,同時向前滾動,則有時可能需要這樣做。 | |
-hn |
--max-back-scroll=n |
指定要向後滾動的最大行數。 如果需要向後滾動超過n行,則屏幕將向前重新繪製。 (如果終端無法向後滾動,則暗示-h0。) |
-i |
--ignore-case |
導致搜索忽略大小寫; 也就是說,大寫和小寫被認爲是相同的。 如果搜索模式中出現任何大寫字母,則忽略此選項; 換句話說,如果一個模式包含大寫字母,那麼該搜索不會忽略大小寫。 |
-I |
--IGNORE-CASE |
與-i類似,但即使模式包含大寫字母,搜索也會忽略大小寫。 |
-jn |
--jump-target=n |
指定屏幕上要定位“目標”行的行。目標行是由任何命令指定的行,用於搜索模式,跳轉到行號,跳轉到文件百分比或跳轉到標記。屏幕行可以用數字指定:屏幕上的頂行是1,下一行是2,依此類推。指定相對於屏幕底部的線條的數字可能爲負:屏幕底部的行爲-1,底部的第二行爲-2,依此類推。或者,屏幕線可以指定爲屏幕高度的一小部分,從小數點開始:.5位於屏幕中間,.3是距離第一行的十分之三,依此類推。如果將行指定爲分數,則在調整終端窗口大小時重新計算實際行號,以使目標行保持在屏幕高度的指定分數處。如果使用任何形式的-j選項,則向前搜索從目標行之後的行開始,向後搜索從目標行開始。例如,如果使用“-j4”,則目標行是屏幕上的第四行,因此向前搜索從屏幕上的第五行開始。 |
-J |
--status-column |
在屏幕的左邊緣顯示status列。 status列顯示與當前搜索匹配的行。 如果-w或-W選項生效,也會使用status列。 |
-kfilename |
--lesskey-file=filename |
使得less打開並將命名文件解釋爲lesskey文件。 可以指定多個-k選項。 如果設置了LESSKEY或LESSKEY_SYSTEM環境變量,或者在標準位置找到了lesskey文件(請參閱KEY BINDINGS),則它也可用作lesskey文件。 |
-K |
--quit-on-intr |
當輸入中斷字符(通常是^ C)時,立即退出會導致less。 通常情況下,中斷字符會導致停止執行任何操作並返回其命令提示符。 請注意,使用此選項使得無法從“F”命令返回到命令提示符。 |
-L or --no-lessopen
Ignore the LESSOPEN environment variable (see the INPUT PREPRO-
CESSOR section below). This option can be set from within less,
but it will apply only to files opened subsequently, not to the
file which is currently open.
-m or --long-prompt
Causes less to prompt verbosely (like more), with the percent
into the file. By default, less prompts with a colon.
-M or --LONG-PROMPT
Causes less to prompt even more verbosely than more.
-n or --line-numbers
Suppresses line numbers. The default (to use line numbers) may
cause less to run more slowly in some cases, especially with a
very large input file. Suppressing line numbers with the -n
option will avoid this problem. Using line numbers means: the
line number will be displayed in the verbose prompt and in the =
command, and the v command will pass the current line number to
the editor (see also the discussion of LESSEDIT in PROMPTS
below).
-N or --LINE-NUMBERS
Causes a line number to be displayed at the beginning of each
line in the display.
-ofilename or --log-file=filename
Causes less to copy its input to the named file as it is being
viewed. This applies only when the input file is a pipe, not an
ordinary file. If the file already exists, less will ask for
confirmation before overwriting it.
-Ofilename or --LOG-FILE=filename
The -O option is like -o, but it will overwrite an existing file
without asking for confirmation.
If no log file has been specified, the -o and -O options can be
used from within less to specify a log file. Without a file
name, they will simply report the name of the log file. The "s"
command is equivalent to specifying -o from within less.
-ppattern or --pattern=pattern
The -p option on the command line is equivalent to specifying
+/pattern; that is, it tells less to start at the first occur-
rence of pattern in the file.
-Pprompt or --prompt=prompt
Provides a way to tailor the three prompt styles to your own
preference. This option would normally be put in the LESS envi-
ronment variable, rather than being typed in with each less com-
mand. Such an option must either be the last option in the LESS
variable, or be terminated by a dollar sign. -Ps followed by a
string changes the default (short) prompt to that string. -Pm
changes the medium (-m) prompt. -PM changes the long (-M)
prompt. -Ph changes the prompt for the help screen. -P=
changes the message printed by the = command. -Pw changes the
message printed while waiting for data (in the F command). All
prompt strings consist of a sequence of letters and special
escape sequences. See the section on PROMPTS for more details.
-q or --quiet or --silent
Causes moderately "quiet" operation: the terminal bell is not
rung if an attempt is made to scroll past the end of the file or
before the beginning of the file. If the terminal has a "visual
bell", it is used instead. The bell will be rung on certain
other errors, such as typing an invalid character. The default
is to ring the terminal bell in all such cases.
-Q or --QUIET or --SILENT
Causes totally “quiet” operation: the terminal bell is never
rung.
-r or --raw-control-chars
Causes "raw" control characters to be displayed. The default is
to display control characters using the caret notation; for
example, a control-A (octal 001) is displayed as "^A". Warning:
when the -r option is used, less cannot keep track of the actual
appearance of the screen (since this depends on how the screen
responds to each type of control character). Thus, various dis-
play problems may result, such as long lines being split in the
wrong place.
-R or --RAW-CONTROL-CHARS
Like -r, but only ANSI "color" escape sequences are output in
"raw" form. Unlike -r, the screen appearance is maintained cor-
rectly in most cases. ANSI "color" escape sequences are
sequences of the form:
ESC [ ... m
where the "..." is zero or more color specification characters
For the purpose of keeping track of screen appearance, ANSI
color escape sequences are assumed to not move the cursor. You
can make less think that characters other than "m" can end ANSI
color escape sequences by setting the environment variable
LESSANSIENDCHARS to the list of characters which can end a color
escape sequence. And you can make less think that characters
other than the standard ones may appear between the ESC and the
m by setting the environment variable LESSANSIMIDCHARS to the
list of characters which can appear.
-s or --squeeze-blank-lines
Causes consecutive blank lines to be squeezed into a single
blank line. This is useful when viewing nroff output.
-S or --chop-long-lines
Causes lines longer than the screen width to be chopped rather
than folded. That is, the portion of a long line that does not
fit in the screen width is not shown. The default is to fold
long lines; that is, display the remainder on the next line.
-ttag or --tag=tag
The -t option, followed immediately by a TAG, will edit the file
containing that tag. For this to work, tag information must be
available; for example, there may be a file in the current
directory called "tags", which was previously built by ctags (1)
or an equivalent command. If the environment variable LESSGLOB-
ALTAGS is set, it is taken to be the name of a command compati-
ble with global (1), and that command is executed to find the
tag. (See http://www.gnu.org/software/global/global.html). The
-t option may also be specified from within less (using the -
command) as a way of examining a new file. The command ":t" is
equivalent to specifying -t from within less.
-Ttagsfile or --tag-file=tagsfile
Specifies a tags file to be used instead of "tags".
-u or --underline-special
Causes backspaces and carriage returns to be treated as print-
able characters; that is, they are sent to the terminal when
they appear in the input.
-u or --underline-special
Causes backspaces and carriage returns to be treated as print-
able characters; that is, they are sent to the terminal when
they appear in the input.
-U or --UNDERLINE-SPECIAL
Causes backspaces, tabs and carriage returns to be treated as
control characters; that is, they are handled as specified by
the -r option.
By default, if neither -u nor -U is given, backspaces which
appear adjacent to an underscore character are treated spe-
cially: the underlined text is displayed using the terminal’s
hardware underlining capability. Also, backspaces which appear
between two identical characters are treated specially: the
overstruck text is printed using the terminal’s hardware bold-
face capability. Other backspaces are deleted, along with the
preceding character. Carriage returns immediately followed by a
newline are deleted. other carriage returns are handled as
specified by the -r option. Text which is overstruck or under-
lined can be searched for if neither -u nor -U is in effect.
-V or --version
Displays the version number of less.
-w or --hilite-unread
Temporarily highlights the first "new" line after a forward
movement of a full page. The first "new" line is the line imme-
diately following the line previously at the bottom of the
screen. Also highlights the target line after a g or p command.
The highlight is removed at the next command which causes move-
ment. The entire line is highlighted, unless the -J option is
in effect, in which case only the status column is highlighted.
-W or --HILITE-UNREAD
Like -w, but temporarily highlights the first new line after any
forward movement command larger than one line.
-xn,… or --tabs=n,…
Sets tab stops. If only one n is specified, tab stops are set
at multiples of n. If multiple values separated by commas are
specified, tab stops are set at those positions, and then con-
tinue with the same spacing as the last two. For example,
-x9,17 will set tabs at positions 9, 17, 25, 33, etc. The
default for n is 8.
-X or --no-init
Disables sending the termcap initialization and deinitialization
strings to the terminal. This is sometimes desirable if the
deinitialization string does something unnecessary, like clear-
ing the screen.
-yn or --max-forw-scroll=n
Specifies a maximum number of lines to scroll forward. If it is
necessary to scroll forward more than n lines, the screen is
repainted instead. The -c or -C option may be used to repaint
from the top of the screen if desired. By default, any forward
movement causes scrolling.
-[z]n or --window=n
Changes the default scrolling window size to n lines. The
default is one screenful. The z and w commands can also be used
to change the window size. The "z" may be omitted for compati-
bility with some versions of more. If the number n is negative,
it indicates n lines less than the current screen size. For
example, if the screen is 24 lines, -z-4 sets the scrolling win-
dow to 20 lines. If the screen is resized to 40 lines, the
scrolling window automatically changes to 36 lines.
-“cc or --quotes=cc
Changes the filename quoting character. This may be necessary
if you are trying to name a file which contains both spaces and
quote characters. Followed by a single character, this changes
the quote character to that character. Filenames containing a
space should then be surrounded by that character rather than by
double quotes. Followed by two characters, changes the open
quote to the first character, and the close quote to the second
character. Filenames containing a space should then be preceded
by the open quote character and followed by the close quote
character. Note that even after the quote characters are
changed, this option remains -” (a dash followed by a double
quote).
-~ or --tilde
Normally lines after end of file are displayed as a single tilde
(~). This option causes lines after end of file to be displayed
as blank lines.
-# or --shift
Specifies the default number of positions to scroll horizontally
in the RIGHTARROW and LEFTARROW commands. If the number speci-
fied is zero, it sets the default number of positions to one
half of the screen width. Alternately, the number may be speci-
fied as a fraction of the width of the screen, starting with a
decimal point: .5 is half of the screen width, .3 is three
tenths of the screen width, and so on. If the number is speci-
fied as a fraction, the actual number of scroll positions is
recalculated if the terminal window is resized, so that the
actual scroll remains at the specified fraction of the screen
width.
--no-keypad
Disables sending the keypad initialization and deinitialization
strings to the terminal. This is sometimes useful if the keypad
strings make the numeric keypad behave in an undesirable manner.
--follow-name
Normally, if the input file is renamed while an F command is
executing, less will continue to display the contents of the
original file despite its name change. If --follow-name is
specified, during an F command less will periodically attempt to
reopen the file by name. If the reopen succeeds and the file is
a different file from the original (which means that a new file
has been created with the same name as the original (now
renamed) file), less will display the contents of that new file.
-- A command line argument of "--" marks the end of option argu-
ments. Any arguments following this are interpreted as file-
names. This can be useful when viewing a file whose name begins
with a "-" or "+".
+ If a command line option begins with +, the remainder of that
option is taken to be an initial command to less. For example,
+G tells less to start at the end of the file rather than the
beginning, and +/xyz tells it to start at the first occurrence
of "xyz" in the file. As a special case, +<number> acts like
+<number>g; that is, it starts the display at the specified line
number (however, see the caveat under the "g" command above).
If the option starts with ++, the initial command applies to
every file being viewed, not just the first one. The + command
described previously may also be used to set (or change) an ini-
tial command for every file.