Linux file content view

Use the following command on a Linux system to view the contents of a file:

  • cat displays the contents of the file starting from the first line
  • tac is displayed from the last line, it can be seen that tac is written backwards of cat!
  • When nl is displayed, output the line number along the way!
  • more Display file contents page by page
  • less is similar to more, but better than more, he can page forward!
  • head only looks at the first few lines
  • tail only looks at a few lines of the tail
  • cat

    Display file contents starting from the first line

    grammar:

    cat [- AbEnTv ]

    Options and parameters:

    • -A : equivalent to the integration option of -vET, which can list some special characters instead of blanks;
    • -b : List the line number, display the line number only for non-blank lines, and do not mark the line number for blank lines!
    • -E : Display the trailing line break byte $;
    • -n : Print the line number, along with the blank line, there will be a line number, which is different from the -b option;
    • -T : Display the [tab] key as ^I;
    • -v : list some invisible special characters

    Check the contents of the /etc/issue file:

    [root@www ~]# cat /etc/issue
    CentOS release 6.4(Final)Kernel \r on an \m

    tac

    The tac command is just the opposite of the cat command. The content of the file is displayed from the last line. It can be seen that tac is the reverse of cat! Such as:

    [root@www ~]# tac /etc/issue
    
    Kernel \r on an \m
    CentOS release 6.4(Final)

    nl

    显示行号

    语法:

    nl [-bnw]文件

    选项与参数:

    • -b :指定行号指定的方式,主要有两种:
      -b a :表示不论是否为空行,也同样列出行号(类似 cat -n);
      -b t :如果有空行,空的那一行不要列出行号(默认值);
    • -n :列出行号表示的方法,主要有三种:
      -n ln :行号在萤幕的最左方显示;
      -n rn :行号在自己栏位的最右方显示,且不加 0 ;
      -n rz :行号在自己栏位的最右方显示,且加 0 ;
    • -w :行号栏位的占用的位数。

    范例一:用 nl 列出 /etc/issue 的内容

    [root@www ~]# nl /etc/issue
         1CentOS release 6.4(Final)2Kernel \r on an \m

    more

    一页一页翻动

    [root@www ~]# more /etc/man.config
    ## Generated automatically from man.conf.in by the# configure script.## man.conf from man-1.6d....(中间省略)....--More--(28%)<==重点在这一行喔!你的光标也会在这里等待你的命令

    在 more 这个程序的运行过程中,你有几个按键可以按的:

    • 空白键 (space):代表向下翻一页;
    • Enter         :代表向下翻『一行』;
    • /字串         :代表在这个显示的内容当中,向下搜寻『字串』这个关键字;
    • :f            :立刻显示出档名以及目前显示的行数;
    • q             :代表立刻离开 more ,不再显示该文件内容。
    • b 或 [ctrl]-b :代表往回翻页,不过这动作只对文件有用,对管线无用。

    less

    一页一页翻动,以下实例输出/etc/man.config文件的内容:

    [root@www ~]# less /etc/man.config
    ## Generated automatically from man.conf.in by the# configure script.## man.conf from man-1.6d....(中间省略)....:<==这里可以等待你输入命令!

    less运行时可以输入的命令有:

    • 空白键    :向下翻动一页;
    • [pagedown]:向下翻动一页;
    • [pageup]  :向上翻动一页;
    • /字串     :向下搜寻『字串』的功能;
    • ?字串     :向上搜寻『字串』的功能;
    • n         :重复前一个搜寻 (与 / 或 ? 有关!)
    • N         :反向的重复前一个搜寻 (与 / 或 ? 有关!)
    • q         :离开 less 这个程序;

    head

    取出文件前面几行

    语法:

    head [-n number]文件

    选项与参数:

    • -n :后面接数字,代表显示几行的意思
    [root@www ~]# head /etc/man.config

    默认的情况中,显示前面 10 行!若要显示前 20 行,就得要这样:

    [root@www ~]# head -n 20/etc/man.config

    tail

    取出文件后面几行

    语法:

    tail [-n number]文件

    选项与参数:

    • -n :后面接数字,代表显示几行的意思
    • -f :表示持续侦测后面所接的档名,要等到按下[ctrl]-c才会结束tail的侦测
    [root@www ~]# tail /etc/man.config
    # 默认的情况中,显示最后的十行!若要显示最后的 20 行,就得要这样:[root@www ~]# tail -n 20/etc/man.config

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270689&siteId=291194637