【Linux常用命令】之less命令

Linux常用命令之less命令

文章目录

作者简介

听雨:一名在一线从事多年研发的程序员,从事网站后台开发,熟悉java技术栈,对前端技术也有研究,同时也是一名骑行爱好者。
Darren:一个工作经验用了N年的,资深划水人士,除了工作无其他爱好

口号:记录在开发中遇到日常问题、棘手问题的解法和思路

常用命令之less

背景介绍

less 是分页显示文本文件内容,less命令分页显示和more有几分相似,但more只能从前往后,less不仅可以从前往后还可以从后往前,相对更加灵活。(PageDown和PageUp按键)

常用参数

参数 解释
-e 当文件显示结束后自动退出
-m 显示阅读进度百分比
-N 显示文件内容时带行号
-Q 不使用警告音
-s 将连续多个空行视为一行
-S 显示较多的内容而不换行
-x数字 将tab字符显示为指定个数的空格字符

例如

展示分页信息最后不退出,需要按q键退出

shell 复制代码
root@master:~/temp/less# less lesstest.txt 

显示结束后自动退出

shell 复制代码
root@master:~/temp/less# less -e lesstest.txt 

打开文件后显示行数

shell 复制代码
root@master:~/temp/less# less -N lesstest.txt  

忽略告警音

shell 复制代码
root@master:~/temp/less# less -Q lesstest.txt

多个空行视为一行

shell 复制代码
root@master:~/temp/less# less -s lesstest.txt 
And as I gave it to him my heart was torn. 当我递给他时,我的心里千愁万绪。
        Tab
    space       

"You have plans that I do not know about," I said. "你有一些我不知道的计划,"我说。

But he did not answer me. 但他没回答。

每行显示多行时不换行显示,这个注意观察,很多行时最后有>注意观察

shell 复制代码
root@master:~/temp/less# less -S lesstest.txt 
And as I gave it to him my heart was torn. 当我递给他时,我的心里千愁万绪。
        Tab
    space       









"You have plans that I do not know about," I said. "你有一些我不知道的计划,"我说。

But he did not answer me. 但他没回答。

He said to me, instead: "You know, my descent to the earth... 他对我说,"你知道,从我落到地球上······

Tomorrow will be its anniversary." Then, after a silence, he went on: "I came down very near here." And he flushed. 到明天就满一年了。"他沉默了一阵,又接着说:"我就落在这儿附近 >

And once again, without understanding why, I had a queer sense of sorrow. 我再一次意识到,我为什么感受到一种莫名的愁绪。

One question, however, occurred to me: "Then it was not by chance that on the morning when I first met you --- a week ago --- you were strolling along like that, all alone, a thousa>

You were on the your way back to the place where you landed?" 你是想回到你降落的地方对吗?"

The little prince flushed again. 小王子的脸又红了。

将tab字符显示为指定个数的空格字符,注意使用方法和直观感受

shell 复制代码
root@master:~/temp/less# less -x2 lesstest.txt
And as I gave it to him my heart was torn. 当我递给他时,我的心里千愁万绪。
  Tab
    space 

基本命令导航

↓ 或 j:向下滚动一行

↑ 或 k:向上滚动一行

Space:向下滚动一页

b(PgUp):向上滚动一页

f(PgDn):向下滚动一页

g:跳转到文件的开头

G:跳转到文件的结尾

q:退出 less

less 提供强大的搜索功能

/pattern:向前搜索 pattern

?pattern:向后搜索 pattern

n:重复前一个搜索(同方向)

N:重复前一个搜索(反方向)

例如,搜索文件中的单词 "remember":

shell 复制代码
/remember
?remember

总结

less虽然是查看文件命令,主要是分页使用,个人认为比more相对来说好用一些,一定程度上和vi编辑器有一些相同的快捷键

参考:《Linux常用命令自学手册》

相关推荐
chlk1231 天前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑1 天前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件1 天前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒1 天前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash2 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux
蝎子莱莱爱打怪4 天前
Centos7中一键安装K8s集群以及Rancher安装记录
运维·后端·kubernetes