Home > Operation and Maintenance > Linux Operation and Maintenance > What are the differences between less and more in linux?

What are the differences between less and more in linux?

青灯夜游
Release: 2022-05-11 18:59:13
Original
25181 people have browsed it

Difference: 1. Less can press the up and down arrow keys to display the upper and lower content, while more cannot control the display through the up and down arrow keys; 2. Less does not have to read the entire file, and the loading speed is faster than more; 3. Less After exiting, the shell will not leave the content just displayed, but after more exits, it will leave the content just displayed on the shell.

What are the differences between less and more in linux?

#The operating environment of this tutorial: CentOS 6 system, Dell G3 computer.

Linux more and less commands

more command

more command can display text files in pages The user can read the content of the file page by page. The basic format of this command is as follows:

[root@localhost ~]# more [选项] 文件名
Copy after login

more The command is relatively simple and generally does not require any options. For the options listed in Table 1, readers only need to read Just get to know it.

Table 1 more command options and meaning
          Options           Meaning
          -f             When calculating the number of lines, use the actual number of lines, not the number of lines after automatic line wrapping.
          -p             Instead of scrolling through each page, the screen is cleared first and then the content is displayed.
          -c             Similar to the -p option, except that the content is displayed first and other old data is cleared.
-s             When two or more consecutive blank lines are encountered, they are replaced with one blank line.
          -u             Do not display lower quotes (depending on the terminal specified by the environment variable TERM).
          n           Display the file contents starting from line n, where n represents a number.
          -n             The number of rows displayed at one time, n represents a number.

less 命令

less 命令的作用和 more 十分类似,都用来浏览文本文件中的内容,不同之处在于,使用 more 命令浏览文件内容时,只能不断向后翻看,而使用 less 命令浏览,既可以向后翻看,也可以向前翻看。

不仅如此,为了方面用户浏览文本内容,less 命令还提供了以下几个功能:

  • 使用光标键可以在文本文件中前后(左后)滚屏;

  • 用行号或百分比作为书签浏览文件;

  • 提供更加友好的检索、高亮显示等操作;

  • 兼容常用的字处理程序(如 Vim、Emacs)的键盘操作;

  • 阅读到文件结束时,less 命令不会退出;

  • 屏幕底部的信息提示更容易控制使用,而且提供了更多的信息。

less 命令的基本格式如下:

[root@localhost ~]# less [选项] 文件名
Copy after login

此命令可用的选项以及各自的含义如表 1 所示。

表 1 less 命令选项及含义
选项 选项含义
-N 显示每行的行号。
-S 行过长时将超出部分舍弃。
-e 当文件显示结束后,自动离开。
-g 只标志最后搜索到的关键同。
-Q 不使用警告音。
-i 忽略搜索时的大小写。
-m 显示类似 more 命令的百分比。
-f 强迫打开特殊文件,比如外围设备代号、目录和二进制文件。
-s 显示连续空行为一行。
-b <缓冲区大小> 设置缓冲区的大小。
-o <文件名> 将 less 输出的内容保存到指定文件中。
-x <数字> 将【Tab】键显示为规定的数字空格。

more 和 less的区别:

1、 less可以按键盘上下方向键显示上下内容,而more不能通过上下方向键控制显示

2、less不必读整个文件,加载速度会比more更快

3、less退出后shell不会留下刚显示的内容,而more退出后会在shell上留下刚显示的内容

4、阅读到文件结束时,less不会退出,而more会

5、less可用行号或百分比作为书签浏览文件,而more不行

6、相比more,less提供更加友好的检索、高亮显示等操作

扩展知识:Linux less命令常用实例

查看文件内容

语法

less file
Copy after login

案例

我们使用 less 命令,查看 /etc/vimrc 的文件内容,具体命令如下:

less /etc/vimrc
Copy after login

运行后,终端输出如下:

What are the differences between less and more in linux?

我们看到,我们使用了 less 命令,查看了文件内容,现在,我们就可以使用上面的交互命令,对该文本进行操作了,比如,我们按下回车键,可以向下移动一行,运行后,终端输出如下:

What are the differences between less and more in linux?

同时,我们还可以使用空格键,向下移动一页,运行后,终端输出如下:

What are the differences between less and more in linux?

即,我们使用了 less 命令,分屏查看了文件内容。

查看文件内容并显示行号

语法

less -N file
Copy after login

案例

我们使用 less 命令,查看 /etc/vimrc 的文件内容,并显示行号,具体命令如下:

less -N /etc/vimrc
Copy after login

运行后,终端输出如下:

What are the differences between less and more in linux?

这次,我们使用了 less 命令,查看了文件内容,并显示了行号。

less命令字符串搜索

语法

/str
Copy after login

案例

我们使用 less 命令,查看 /etc/vimrc 的文件内容,具体命令如下:

less /etc/vimrc
Copy after login

运行后,终端输出如下:

What are the differences between less and more in linux?

现在,我们可以使用 /str 的形式,开始搜索字符串,如下图所示:

What are the differences between less and more in linux?

现在,我们按下回车,开始搜索,此时如下图所示:

What are the differences between less and more in linux?

我们看到,所有匹配的字符串都被高亮了,即,我们使用了 /str 的形式搜索了我们指定的字符串,同时,我们还可以使用 n 匹配下一个,使用 N 匹配上一个。

相关推荐:《Linux视频教程

The above is the detailed content of What are the differences between less and more in linux?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template