Linux查看文本文件

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
$
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容