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?

May 11, 2022 pm 06:59 PM
linux

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
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
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles