Linux文件目录操作命令-less命令

一、less命令

        less命令也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大。less 的用法比起 more 更加的有弹性。在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup] [pagedown] 等按键的功能来往前往后翻看文件,更容易用来查看一个文件的内容!除此之外,在 less 里头可以拥有更多的搜索功能,不止可以向下搜,也可以向上搜。 

1.命令格式

less [参数] 文件

2.命令功能

lessmore 类似,但使用 less 可以随意浏览文件,而 more 仅能向前移动,却不能向后移动,而且 less 在查看之前不会加载整个文件。

3.命令参数

  • -b <缓冲区大小> 设置缓冲区的大小
  • -e 当文件显示结束后,自动离开
  • -f 强迫打开特殊文件,例如外围设备代号、目录和二进制文件
  • -g 只标志最后搜索的关键词
  • -i 忽略搜索时的大小写
  • -m 显示类似more命令的百分比
  • -N 显示每行的行号
  • -o <文件名> 将less 输出的内容在指定文件中保存起来
  • -Q 不使用警告音
  • -s 显示连续空行为一行
  • -S 行过长时间将超出部分舍弃
  • -x <数字> 将“tab”键显示为规定的数字空格
  • /字符串:向下搜索“字符串”的功能
  • ?字符串:向上搜索“字符串”的功能
  • n:重复前一个搜索(与 / 或 ? 有关)
  • N:反向重复前一个搜索(与 / 或 ? 有关)
  • b 向后翻一页
  • d 向后翻半页
  • h 显示帮助界面
  • Q 退出less 命令
  • u 向前滚动半页
  • y 向前滚动一行
  • 空格键 滚动一行
  • 回车键 滚动一页
  • [pagedown]: 向下翻动一页
  • [pageup]: 向上翻动一页

4.使用实例:

实例1

查看文件
命令:

less log.log

输出:

this is line 1.
this is line 2.
this is line 3.
this is line 4.

this is line5.

this is line 6.
this is line 7.
this is line 8.
this is line 9.
this is line 10.
this is line 11.
this is line 12.
this is line 13.
this is line 14.
this is line 15.
log.log

实例2

ps查看进程信息并通过less分页显示
命令:

ps -ef |less

输出:

UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 19:28 ?        00:00:06 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2      0  0 19:28 ?        00:00:00 [kthreadd]
root          3      2  0 19:28 ?        00:00:00 [ksoftirqd/0]
root          6      2  0 19:28 ?        00:00:00 [kworker/u256:0]
root          7      2  0 19:28 ?        00:00:00 [migration/0]
root          8      2  0 19:28 ?        00:00:00 [rcu_bh]
root          9      2  0 19:28 ?        00:00:01 [rcu_sched]
root         10      2  0 19:28 ?        00:00:00 [watchdog/0]
root         12      2  0 19:28 ?        00:00:00 [khelper]
root         13      2  0 19:28 ?        00:00:00 [kdevtmpfs]
root         14      2  0 19:28 ?        00:00:00 [netns]
root         15      2  0 19:28 ?        00:00:00 [khungtaskd]
root         16      2  0 19:28 ?        00:00:00 [writeback]
root         17      2  0 19:28 ?        00:00:00 [kintegrityd]
root         18      2  0 19:28 ?        00:00:00 [bioset]

实例3

查看命令历史使用记录并通过less分页显示
命令:

history | less

输出:

    1  top
    2  ls
    3  cd test
    4  ll
    5  vi log.log
    6  cat log.log
    7  nl log.log
    8  [tianyuanmengniu@localhost test]$ cat log.log
    9  this is line 1.
   10  this is line 2.
   11  this is line 3.
   12  this is line 4.
   13  [tianyuanmengniu@localhost test]$ nl log.log
   14       1  this is line 1.
   15       2  this is line 2.
   16       3  this is line 3.
   17       4  this is line 4.
:

实例5

浏览多个文件
命令:

less log.txt log.log

输出:

Hello
World
Linux command
PWD=/home/tianyuanmengniu/test
~
~
~
(END) - Next: log.log

说明:

  • 输入 :n 后,切换到 log.log
  • 输入 :p 后,切换到 log.txt

5.附加备注

  • 5.1.全屏导航
    ctrl + F - 向前移动一屏
    ctrl + B - 向后移动一屏
    ctrl + D - 向前移动半屏
    ctrl + U - 向后移动半屏

  • 5.2.单行导航
    j - 向前移动一行
    k - 向后移动一行

  • 5.3.其它导航
    G - 移动到最后一行
    g - 移动到第一行
    q / ZZ - 退出 less 命令

  • 5.4.其它有用的命令
    v - 使用配置的编辑器编辑当前文件
    h - 显示 less 的帮助文档
    &pattern - 仅显示匹配模式的行,而不是整个文件

  • 5.5.标记导航
    当使用 less 查看大文件时,可以在任何一个位置作标记,可以通过命令导航到标有特定标记的文本位置:
    ma - 使用 a 标记文本的当前位置
    ‘a - 导航到标记 a 处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值