Linux查看文本文件

作者: 学习编程王同学 | 来源:发表于2018-05-10 20:22 被阅读159次

    less

    less程序可以查看一个文本文件的内容:

    $ less .bashrc
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    case $- in
        *i*) ;;
          *) return;;
    esac
    
    # 省略
    

    上面的例子使用less程序查看了.bashrc文件的内容。

    常用的操作如下:

    操作 功能
    空格键 向下翻页
    按键 pagedown 向下翻页
    按键 pageup 向上翻页
    /string 向下搜索string
    ?string 向上搜索string
    n 搜索下一个
    N 搜索上一个
    q 离开less

    cat

    cat命令也可以查看文本文件内容,它的功能很多,下面是一些常见的:

    • -A:显示特殊字符。
    • -n:显示行号。
    • -b:仅对非空白行显示行号。

    下面使用cat查看文件.bashrc内容:

    $ cat .bashrc
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # 省略
    

    使用-n选项查看行号:

    $ cat -n .bashrc
    1  # ~/.bashrc: executed by bash(1) for non-login shells.
    2  # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    3  # for examples
    4
    5  # If not running interactively, don't do anything
    6  case $- in
    7      *i*) ;;
    8        *) return;;
    9  esac
    
    # 省略
    

    使用-b仅对非空白行显示行号:

    $ cat -b .bashrc
    1  # ~/.bashrc: executed by bash(1) for non-login shells.
    2  # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    3  # for examples
    
    4  # If not running interactively, don't do anything
    5  case $- in
    6      *i*) ;;
    7        *) return;;
    8  esac
    
    # 省略
    

    -A选项可以在文档中将Tab用^I显示,将断行符号用$显示:

    $ cat -A .bashrc
    # ~/.bashrc: executed by bash(1) for non-login shells.$
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)$
    # for examples$
    $
    # If not running interactively, don't do anything$
    case $- in$
        *i*) ;;$
          *) return;;$
    esac$
    

    可以看到,每行的最后都有断行符号$

    head tail

    head命令显示文档的“头”,tail命令显示文档的“尾”。

    使用head显示文档的前5行:

    $ head -n 5 .bashrc
    # ~/.bashrc: executed by bash(1) for non-login shells.
    # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
    # for examples
    
    # If not running interactively, don't do anything
    $
    

    使用tail显示文档的最后5行:

    $ tail -n 5 .bashrc
        . /usr/share/bash-completion/bash_completion
      elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
      fi
    fi
    $
    

    相关文章

      网友评论

        本文标题:Linux查看文本文件

        本文链接:https://www.haomeiwen.com/subject/rjvtdftx.html