Linux 查看文件

点击访问 Linux 命令大全 >>

前面章节介绍了怎样使用ls/cd/pwd/dir等命令查看目录内容,即目录中有哪些文件和目录。本章节将介绍怎样通过Shell查看文本文件内容。

cat —— 查看文本文件内容

cat 命令用于查看文本文件内容,语法:

cat [OPTIONS] FILENAME...

参数说明:

  • OPTIONS:可选项

  • FILENAME:文件名称,也可以是带有路径的文件名。可以指定多个

实例:查看 .bashrc 文件内容

[root@localhost ~]# cat .bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@localhost ~]#

cat 命令后面可以直接跟多个文件名,如下:

[root@localhost ~]# cat test1.txt
test1 content
[root@localhost ~]# cat test2.txt
test2 content
[root@localhost ~]# cat test1.txt test2.txt
test1 content
test2 content
[root@localhost ~]#

cat -n 可以在每一行前面显示行号。例如:

[root@localhost ~]# cat -n .bashrc
     1  # .bashrc
     2
     3  # User specific aliases and functions
     4
     5  alias rm='rm -i'
     6  alias cp='cp -i'
     7  alias mv='mv -i'
     8
     9  # Source global definitions
    10  if [ -f /etc/bashrc ]; then
    11          . /etc/bashrc
    12  fi
[root@localhost ~]#

注意:

cat命令会一次将文件所有内容全部显示在屏幕上。如果对于一个几千行的文本文件而言,cat 命令显得毫无用处。

more —— 分页查看文本文件内容

more命令也是用于查看文本文件内容,它不像 cat 命令一次性将文件内容全部显示出来,而是一屏幕一屏幕的显示,并且还会给出百分比提示信息。使用 more -d 查看语法,如下:

[root@localhost ~]# more -d
用法:more [选项] 文件...

选项:
  -d        显示帮助,而不是响铃
  -f        统计逻辑行数而不是屏幕行数
  -l        抑制换页(form feed)后的暂停
  -p        不滚屏,清屏并显示文本
  -c        不滚屏,显示文本并清理行尾
  -u        抑制下划线
  -s        将多个空行压缩为一行
  -NUM      指定每屏显示的行数为 NUM
  +NUM      从文件第 NUM 行开始显示
  +/STRING  从匹配搜索字符串 STRING 的文件位置开始显示
  -V        输出版本信息并退出
[root@localhost ~]#

实例:用 more 查看 /etc/profile 文件内容。

[root@localhost ~]# more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
--More--(44%)

可以看到,more 命令最后一行显示百分比为 44%(--More--(44%)),表示已显示内容占整个文件内容的比例。

more 命令使用技巧:

  • 按空格键:向下翻动一页

  • 按Enter键:向下翻动一行

  • 按Q键:退出more命令,终止阅读。

head —— 查看文本文件头部 n 行

head 命令用于显示文本文件头 n 行,可以通过命令行 -n 指定待显示的行数。例如:

[root@localhost ~]# head -n 10 /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

[root@localhost ~]#

上面显示了 /etc/profile 文件的前 10 行文本。

tail —— 查看文本文件尾部 n 行

tail 命令用于显示文本文件尾部 n 行,可以通过 -n 命令行指定待显示的行数。例如:

[root@localhost ~]# tail -n 10 /etc/profile
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
[root@localhost ~]#

上面显示了 /etc/profile 文件的末尾 10 行文本。

less —— 更好的阅读工具,功能比 more 更为强大

less 和 more 非常相识,但功能更为强大。less 改进了 more 命令的很多细节,并且添加了许多有用的特性,这些特性让 less 看起来更像一个不能编辑的文本编辑器。总体来说,less 命令提供了下面特性:

  • 使用光标键在文本文件中前后滚屏。

  • 用行号或者百分比作为书签浏览文件。

  • 实现复杂的检索、高亮显示等操作。

  • 兼容常用的编辑器的键盘操作。如:Emacs、Vim

下面简单地介绍 less 命令的用法。例如:查看 /etc/profile 文件内容。

[root@localhost ~]# less /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
:

可以看到,less 命令在屏幕底部显示了一个“:”冒号,等待用户输入命令。如果想要向下翻一页,可以按空格键。如果想要向上翻一页,按 B 键。也可以使用光标前、后、左右进行导航。

如果要在文件中搜索某个字符串,可以使用正斜杠“/”跟上想要查找的内容,按回车键进行查找。less 命令会把第一个匹配的内容高亮显示。如果要继续查找相同的内容,只需要输入正斜杠“/”即可。如下图:

可以使用 less -M 显示更多的文件信息,如下:

[root@localhost ~]# less -M /etc/profile

可以看到,less 在输出的底部显示当前文件的名称、页码、总页码、当前位置等。

如果你不想继续阅读文件内容了,可以按 Q 退出 less 程序并返回到 Shell 环境。

grep —— 查找文件内容

在很多时候,并不需要列出文件的全部内容,用户需要做的只是找到包含指定信息的一行。这个时候,如果使用 more 命令逐行的查找,无疑是费时的。当文件特别大的时候,这样的做法完全不可行。为了在文件中查找某些信息,可以使用 grep 命令。语法如下:

grep [OPTIONS] PATTERN [FILE...]

在 FILE 中查找符合 PATTERN 的内容行。参数说明:

  • OPTIONS:命令选项

  • PATTERN:指被搜索的模式(关键词),可以使正则表达式或普通文本

  • FILE:要搜索的文件名称,可以指定多个文件

实例:在 /etc/profile 文件中查找“path”内容,并且将包含“path”内容的行输出。如下:

[root@localhost ~]# grep path /etc/profile
pathmunge () {
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
unset -f pathmunge

分别在 /etc/profile 和 /etc/bashrc 文件中搜索“PATH”字符串,如下:

[root@localhost ~]# grep PATH /etc/profile /etc/bashrc
/etc/profile:    case ":${PATH}:" in
/etc/profile:                PATH=$PATH:$1
/etc/profile:                PATH=$1:$PATH
/etc/profile:export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL
/etc/bashrc:        case ":${PATH}:" in
/etc/bashrc:                    PATH=$PATH:$1
/etc/bashrc:                    PATH=$1:$PATH

如果查找的内容中间有空格,则需要使用单引号括起来。例如:在 /etc/profile 文件中查找“unset i”字符串,如下:

[root@localhost ~]# grep 'unset i' /etc/profile
unset i

严格地说,grep 是通过“基础正则表达式”进行搜索。和 grep 相关的一个工具是 egrep,除了使用“扩展的正则表达式”,egrep和grep完全一样。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号