每天2个Linux命令 more less


2017-07-19 每天2个Linux命令 more命令 less命令

more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作。

   该命令一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今己显示的该文件的百分比:

   --More--(XX%)可以用下列不同的方法对提示做出回答:

  按Space键:     显示文本的下一屏内容。

  按Enier键:      只显示文本的下一行内容。 按斜线符|:接着输入一个模式,可以在文本中寻找下一个相匹配的模式。

  按H键:           显示帮助屏,该屏上有相关的帮助信息。

  按B键:           显示上一屏内容。

  按Q键:           退出more命令。

  Ctrl+Z:          退出命令

(1)用法:

用法:  more [-dlfpcsu ] [-num ] [+/ pattern] [+ linenum] [file ... ] 

         more [选项]  [文件]

(2)功能:

功能:查看文件文档中的内容(支持按页查看和直接跳转行)

(3)选项参数:

  1) -<数字>:   指定每屏显示的行数[-num]

  2) -d:        显示“[press space to continue,'q' to quit.]”和“[Press 'h' for instructions]”

  3) -c:        不进行滚屏操作。每次刷新这个屏幕

  4) -s:       将多个空行压缩成一行显示

  5) -u:         禁止下划线

  6) +<数字>:        从指定数字的行开始显示[+linenum]

  7) +/pattern:    在每个档案显示前搜寻该字串(pattern),然后从该字串前两行之后开始显示 

(4)实例:

  1)[sunjimeng@localhost Documents]$ more -dc more_text1 
    显示文件more_text1的内容,但在显示之前先清屏,并且在屏幕的最下方显示完核的百分比。 

    [sunjimeng@localhost ~]$ cd Documents
    [sunjimeng@localhost Documents]$ ll
    总用量 4
    -rw-r--r--. 1 root root 664 5月   8 19:36 more_text1
    [sunjimeng@localhost Documents]$ more -dc more_text1


    执行过以后,屏幕只剩下:

    复制代码
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!

    Don't aim your success if you want it,just do what you love and believe and fina
    lly you will success!

    Maybe you can be laze man like a pig,but you can't feel free as it!

    I am a college school student!
    I am planning to live and work in hangzhou or guangzhou!
    I am from hefei anhui!
    Enough,I have write too many words!
    [sunjimeng@localhost Documents]$ 


    上面的是终端窗口比较大的情况,所有内容能完全显示,下面是显示不完全的情况:

    复制代码
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!
    --More--(51%)[Press space to continue, 'q' to quit.]                  //在这里显示百分比
    复制代码

2)[sunjimeng@localhost Documents]$ more -10 more_text1                       显示文件more_text1的内容,每10行显示一次,而且在显示之前先清屏。

复制代码
[sunjimeng@localhost Documents]$ more -10 more_text1
I am studing orders of Linux!
I am trying to write as many as lines of text!


No matter how low you consider yourself,
there is always someone looking behind you,
hoping that they were that high!

Something you want keep all the time,always you will lose!

3)[sunjimeng@localhost Documents]$ more +4 -10 more_text1                 从第4行开始显示,每页显示10行              

    复制代码
    [sunjimeng@localhost Documents]$ more -d -10 more_text1
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    --More--(38%)[Press space to continue, 'q' to quit.]   //在这里我按了h键,弹出命令的参考。
    Most commands optionally preceded by integer argument k.  Defaults in brackets.
    Star (*) indicates argument becomes new default.
    -------------------------------------------------------------------------------
    <space>                 Display next k lines of text [current screen size]
    z                       Display next k lines of text [current screen size]*
    <return>                Display next k lines of text [1]*
    d or ctrl-D             Scroll k lines [current scroll size, initially 11]*
    q or Q or <interrupt>   Exit from more
    s                       Skip forward k lines of text [1]
    f                       Skip forward k screenfuls of text [1]
    b or ctrl-B             Skip backwards k screenfuls of text [1]
    '                       Go to place where previous search started
    =                       Display current line number
    /<regular expression>   Search for kth occurrence of regular expression [1]
    n                       Search for kth occurrence of last r.e [1]
    !<cmd> or :!<cmd>       Execute <cmd> in a subshell
    v                       Start up /usr/bin/vi at current line
    ctrl-L                  Redraw screen
    :n                      Go to kth next file [1]
    :p                      Go to kth previous file [1]
    :f                      Display current file name and line number
    .                       Repeat previous command
    -------------------------------------------------------------------------------
    复制代码
          和上面的对比可知,此时显示的也是10行,但第一行的内容和上面第一行的不一样

    复制代码
    [sunjimeng@localhost Documents]$ more +4 -10 more_text1

    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!
    --More--(51%)        //上面的百分比是38%

4)[sunjimeng@localhost Documents]$ more +/Never more_text1                  找到Never字符串所在的行,然后从此行显示,之前的跳过

    复制代码
    [sunjimeng@localhost Documents]$ more more_text1                          //先查看文档中的所有文本数据
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!

    Don't aim your success if you want it,just do what you love and believe and finally you will success!

    Maybe you can be laze man like a pig,but you can't feel free as it!

    I am a college school student!
    I am planning to live and work in hangzhou or guangzhou!
    I am from hefei anhui!
    Enough,I have write too many words!                                       //命令详解从这里开始
    [sunjimeng@localhost Documents]$ more +/Never more_text1

    ...跳过
    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!

    Don't aim your success if you want it,just do what you love and believe and finally you will success!

    Maybe you can be laze man like a pig,but you can't feel free as it!

    I am a college school student!
    I am planning to live and work in hangzhou or guangzhou!
    I am from hefei anhui!
    Enough,I have write too many words!

5)[sunjimeng@localhost Documents]$ cat -n more_text1 |more -10  
more命令通常和管道|结合起来使用,配合cat命令带有特定格式输出

复制代码
[sunjimeng@localhost Documents]$ cat -n more_text1 |more -10
     1    I am studing orders of Linux!
     2    I am trying to write as many as lines of text!
     3    
     4    
     5    No matter how low you consider yourself,
     6    there is always someone looking behind you,
     7    hoping that they were that high!
     8    
     9    Something you want keep all the time,always you will lose!
    10    
--More--

6)[sunjimeng@localhost Documents]$ ls -l / | more -5  
 列一个目录下的文件,由于内容太多,我们应该学会用more来分页显示。这得和管道 | 结合起来。

复制代码
[sunjimeng@localhost Documents]$ ls -l / | more -5
总用量 32
lrwxrwxrwx.   1 root root    7 5月   1 08:38 bin -> usr/bin
dr-xr-xr-x.   4 root root 4096 5月   1 18:22 boot
drwxr-xr-x.  19 root root 3180 5月   4 07:17 dev
drwxr-xr-x. 137 root root 8192 5月   4 15:16 etc
--more-- 
复制代码
      说明: 每页显示5个文件信息,按 Ctrl+F 或者 空格键 将会显示下5条文件信息。

(5)其他:

  常用操作命令

     Enter    向下n行,需要定义。默认为1行

    Ctrl+F    向下滚动一屏

    空格键    向下滚动一屏

    Ctrl+B    返回上一屏

    =    输出当前行的行号

    f     输出文件名和当前行的行号

    V    调用vi编辑器

    !命令    调用Shell,并执行命令

    q    退出more****


每天2个Linux命令 less命令

less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件

,而more命令只能向前浏览。

用less命令显示文件时,用PageUp键向上翻页,用PageDown键向下翻页。要退出less程序,应按Q键。


(1)用法:

用法: less  [选项参数] [文件参数]

(2)功能:

功能: less 与more命令类似,但可以通过翻页键查看上下页的内容

(3)选项参数:

1) -e:    文件内容显示完毕后,自动退出

2) -f:       强制显示文件

3) -g:      不加亮显示搜索到的所有关键词,仅显示当前显示的关键字,以提高显示速度

4) -l:       搜索时忽略大小写的差异

5) -N:       每一行行首显示行号

6) -s:      将连续多个空行压缩成一行显示

7) -S:        在单行显示较长的内容,而不换行显示

8) -x<数字>:        将TAB字符显示为指定个数的空格字符。

(4)实例:

  1)[root@localhost Documents]# less  less_text                  在另一个进程页面中显示文本内容,按q键退出                    

    [root@localhost Documents]# mv more_text1 less_text    //将more_text1改名为less_text
    [root@localhost Documents]# ll
    总用量 4
    -rw-r--r--. 1 root root 664 5月   9 07:59 less_text
    [root@localhost Documents]# cat less_text    //在当前终端中显示(和less命令显示的效果还是有差别的)
    复制代码
    [root@localhost Documents]# less less_text   //在另一个页面中显示文本
    I am studing orders of Linux!
    I am trying to write as many as lines of text!


    No matter how low you consider yourself,
    there is always someone looking behind you,
    hoping that they were that high!

    Something you want keep all the time,always you will lose!

    Never forget to say "thanks"!

    Hppay today,also,prepared for happiness in the future!

    Don't aim your success if you want it,just do what you love and believe and finally you will success!

    Maybe you can be laze man like a pig,but you can't feel free as it!

    I am a college school student!
    I am planning to live and work in hangzhou or guangzhou!
    I am from hefei anhui!
    Enough,I have write too many words!
    (END)

2)[root@localhost Documents]# ps -ef |less      显示已有进程信息

    复制代码
    ID         PID   PPID  C STIME TTY          TIME CMD
    root          1      0  0 00:16 ?     00:00:10 /usr/lib/systemd/systemd --switched-root --system --deserialize 24
    root          2      0  0 00:16 ?        00:00:00 [kthreadd]
    root          3      2  0 00:16 ?        00:00:03 [ksoftirqd/0]
    root          5      2  0 00:16 ?        00:00:00 [kworker/0:0H]
    root          7      2  0 00:16 ?        00:00:00 [migration/0]
    root          8      2  0 00:16 ?        00:00:00 [rcu_bh]
    root          9      2  0 00:16 ?        00:00:00 [rcuob/0]
    root         10      2  0 00:16 ?        00:00:00 [rcuob/1]
    root         11      2  0 00:16 ?        00:00:00 [rcuob/2]
    root         12      2  0 00:16 ?        00:00:00 [rcuob/3]
    root         13      2  0 00:16 ?        00:00:00 [rcuob/4]
    root         14      2  0 00:16 ?        00:00:00 [rcuob/5]
    root         15      2  0 00:16 ?        00:00:00 [rcuob/6]
    root         16      2  0 00:16 ?        00:00:00 [rcuob/7]
    root         17      2  0 00:16 ?        00:00:00 [rcuob/8]
    root         18      2  0 00:16 ?        00:00:00 [rcuob/9]
    root         19      2  0 00:16 ?        00:00:00 [rcuob/10]
    root         20      2  0 00:16 ?        00:00:00 [rcuob/11]
    root         21      2  0 00:16 ?        00:00:00 [rcuob/12]
    root         22      2  0 00:16 ?        00:00:00 [rcuob/13]
    root         23      2  0 00:16 ?        00:00:00 [rcuob/14]
    root         24      2  0 00:16 ?        00:00:00 [rcuob/15]
    root         25      2  0 00:16 ?        00:00:00 [rcuob/16]
    root         26      2  0 00:16 ?        00:00:00 [rcuob/17]
    root         27      2  0 00:16 ?        00:00:00 [rcuob/18]
    root         28      2  0 00:16 ?        00:00:00 [rcuob/19]
    root         29      2  0 00:16 ?        00:00:00 [rcuob/20]
    root         30      2  0 00:16 ?        00:00:00 [rcuob/21]
    root         31      2  0 00:16 ?        00:00:00 [rcuob/22]
    root         32      2  0 00:16 ?        00:00:00 [rcuob/23]
    root         33      2  0 00:16 ?        00:00:00 [rcuob/24]
    root         34      2  0 00:16 ?        00:00:00 [rcuob/25]
    root         35      2  0 00:16 ?        00:00:00 [rcuob/26]
    :
    .......//还有很多没有显示

3)[root@localhost Documents]# history | less               显示历史输入的进程信息

复制代码
    1  touch touch_test /home/sunjimeng/Document
    2  cd /home/sunjimeng/Document
    3  ll
    4  cd /
    5  touch touch_text /home/sunjimeng/Document/touch_test
    6  cd /home/sunjimeng/Document
    7  ll
    8  rm touch_test
    9  cd /
   10  ls -l /home/sunjimeng/Document
   11  touch /home/sunjimeng/Document/touch_test_file
   12  cd home/sunjimeng/Document
   13  ll
   14  touch touch_test_file
   15  ll
   16  touch -t 06061806 touch_test_file
   17  ll
   18  touch touch_test_file
   19  ll
   20  touch -t 06061806 touch_test_file
   21  ll
   22  touch touch_test_file2
   23  ll
   24  touch -r touch_testfile touch_test_tile2
   25  touch -r touch_test_file touch_test_file2
   26  ll
   27  touch -d "10 day ago" touch_test_file2
   28  ll
   29  touch -r touch_test_file touch_test_file2
   30  ll
   31  touch -d "10 days ago" touch_test_file2
   32  ll
   33  touch "10 days ago" touch_test_file
   34  ll
:

4)[root@localhost Documents]# less less1 less2 查看多个文件(怎么切换下一个还不清楚)

复制代码
root@localhost Documents]# cat >less1 <<EOF
> Lost means Get!
> 
> No losing No getting!
> 
> End!
> EOF
[root@localhost Documents]# cat >less2 <<EOF
> If you want keep,you always lose!
> 
> Certainly It is!
> End!
> EOF
[root@localhost Documents]# ll
总用量 12
-rw-r--r--. 1 root root  45 5月   9 08:15 less1
-rw-r--r--. 1 root root  57 5月   9 08:16 less2
-rw-r--r--. 1 root root 664 5月   9 07:59 less_text
[root@localhost Documents]# less less1 less2
复制代码
复制代码
Lost means Get!

No losing No getting!

End!
less1 (file 1 of 2) (END) - Next: less2

5)[sunjimeng@localhost ~]$ netstat -tpnl |less      在另一个进程页面中分页显示信息,这里显示的是网络信息

复制代码
[sunjimeng@localhost ~]$ netstat -tpnl |less
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::22                   :::*                    LISTEN      -                   
tcp6       0      0 ::1:631                 :::*                    LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      -     

(5)其他:

  1.全屏导航

  ctrl + F - 向前移动一屏

  ctrl + B - 向后移动一屏

  ctrl + D - 向前移动半屏

  ctrl + U - 向后移动半屏

  2.单行导航

  j - 向前移动一行

  k - 向后移动一行

  3.其它导航

  G - 移动到最后一行

  g - 移动到第一行

  q / ZZ - 退出 less 命令

  4.其它有用的命令

  v - 使用配置的编辑器编辑当前文件

  h - 显示 less 的帮助文档

  &pattern - 仅显示匹配模式的行,而不是整个文件

  5.标记导航

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

  ma - 使用 a 标记文本的当前位置

  ‘a - 导航到标记 a 处


文章作者: 邓滔
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 邓滔 !
评论
  目录