Linux 查看文件cat tac more less head tail命令
linux 查看文件
Linux 查看文件一共有5个命令为 cat more less head tail.按功能来讲,more和less一组,head和tail一组,cat自己一组。
cat命令
我们通常使用cat命令来查看一个文件内容。
cat [OPTION]... [FILE]... -A, --show-all equivalent to -vET -E, --show-ends display $ at end of each line -T, --show-tabs display TAB characters as ^I -v, --show-nonprinting use ^ and M- notation, except for LFD and TAB -n, --number number all output lines
通常我们只使用 -A和-n . -A选项会显示 tab符号,换行符。-n选项会输出行号。
[root@node1 ~]# cat -n 2.txt
1 HOSTNAME(1) Linux Programmer's Manual HOSTNAME(1)
2
3
4
5 NAME
6 hostname - show or set the system's host name
7 domainname - show or set the system's NIS/YP domain name
8 ypdomainname - show or set the system's NIS/YP domain name
9 nisdomainname - show or set the system's NIS/YP domain name
10 dnsdomainname - show the system's DNS domain name
[root@node1 ~]# cat -A 2.txt
HOSTNAME(1) Linux Programmer's Manual HOSTNAME(1)$
$
$
$
NAME$
hostname - show or set the system's host name$
domainname - show or set the system's NIS/YP domain name$
ypdomainname - show or set the system's NIS/YP domain name$
nisdomainname - show or set the system's NIS/YP domain name$
dnsdomainname - show the system's DNS domain name$
[root@node1 ~]#
tac 命令
tac命令是与cat命令反过来的,它会按末尾到起始的方向去读一个文件。
[root@node1 ~]# cat -n yumtools.txt |tac
23 /usr/sbin/yumdb
22 /usr/sbin/yum-complete-transaction
21 /usr/bin/yumdownloader
20 /usr/bin/yum-groups-manager
19 /usr/bin/yum-debug-restore
18 /usr/bin/yum-debug-dump
17 /usr/bin/yum-config-manager
16 /usr/bin/yum-builddep
15 /usr/bin/verifytree
14 /usr/bin/show-installed
13 /usr/bin/show-changed-rco
12 /usr/bin/repotrack
11 /usr/bin/reposync
10 /usr/bin/repoquery
9 /usr/bin/repomanage
8 /usr/bin/repodiff
7 /usr/bin/repoclosure
6 /usr/bin/repo-rss
5 /usr/bin/repo-graph
4 /usr/bin/package-cleanup
3 /usr/bin/needs-restarting
2 /usr/bin/find-repos-of-install
1 /usr/bin/debuginfo-install
more命令
more命令不像 cat 会一次性输出所有内容,而是会分页显示一个文件里的内容,但是整个文件会被读入到内存中。如果文件内容少于一页的行数,会直接整个文件输出。当内容多于一页的行数时,会分页显示文件。
通常我们使用more命令时不加任何选项。有一个参数 more -number file。这里的number会控制我们一次的行数,同时一些命令,例如空格,z会受这个参数的影响。
有一些快捷键或内部命令我们需要知道。
h or ? 内部帮助指南 SPACE 空格键 向下移动n行,默认是一页的行数。被more -number的number值控制 z 向下移动n行,默认是一页的行数。 被more -number file的number的值控制 RETURN 回车键,向下移动一行。 s 跳过k行,默认是1行 。 f 跳过一页,默认1页 b 向上翻页,默认1页 = 显示当前行数 . 重复上一次操作 q 退出
/ 查找关键字
less 命令
less命令和more命令相似,不同点是,less可以向前和相后查看文件。而且less不会一次性将文件读进内存中,所以对于查看大文件。我们可以使用less命令更加快速。与more命令相比,less命令更加强大。
less命令中常用内部命令:
SPACE或Ctrl+V或f或Ctrl+f 向前前进N行,默认一个屏幕的行数。
z 与SPACE空格类似。
Enter 或Return 或Ctrl N或e或Ctrl E或j 或Ctrl J 向前N行,默认1行。
d或Ctrl D 向前N行,默认半个屏幕。
b或Ctrl B 或Esc+V 往回N行,默认整个屏幕。
w 与Esc +V 类似。
y或Ctrl Y或Ctrl P或Ctrl K 往回N行,默认1行。
u或Ctrl U 往回N行,默认半个屏幕。
g或<或Esc < 调整到N行,默认第一行。
G或>或Esc > 跳转到N行,默认最后一行。
{}[] () 都是寻找对应的符号。
/ 向前寻找匹配的表达式。
\ 往回寻找匹配的表达式。
n 重复之前的寻找。
N 重复之前的寻找不过是反方向。
=或者 Ctrl G或者:f 显示当前文件的信息 ,包括文件名和行数。 - 紧跟一个选项,会修改环境变量的值 -- 与-类似,不过是长选项模式 -+ 紧跟一个选项,会重置这个变量会默认的值 --+ 与-+类似,不过长选项模式 -! 紧跟一个选项,把当前值修改为相反的值,并输出当前的值 --! 与-!类似,不过长选项模式 _下划线 紧跟一个选项,返回选项当前值 __两个下划线 与_类似,不过长选项模式 q或Q或:q或:Q或ZZ 退出less ! shell 命令 less中 调用 shell命令
less的常用选项:
less设置选项的方式 setenv LESS "-options" LESS="-options"; export LESS 常用选项: -N --LINE-NUMBERS 经常显示行数 -n --line-numbers 不显示行数 -yn or --max-forw-scroll=n 向前查看的最大行数 -hn or --max-back-scroll=n 往回查看最大行数
head 命令
head 命令输出文件的前面部分,默认开始10行。
-n n为指定的行数。
[root@node1 ~]# head -10 2.txt
HOSTNAME(1) Linux Programmer's Manual HOSTNAME(1)
NAME
hostname - show or set the system's host name
domainname - show or set the system's NIS/YP domain name
ypdomainname - show or set the system's NIS/YP domain name
nisdomainname - show or set the system's NIS/YP domain name
dnsdomainname - show the system's DNS domain name
tail命令
tail 命令输出文件的末尾部分内容,默认末尾10行。
-n 末尾多少行文件内容
[root@node1 ~]# tail -10 2.txt
/etc/hosts Usually, this is where one sets the domain name by aliasing the host name to the FQDN.
AUTHORS
Peter Tobias, <tobias@et-inf.fho-emden.de>
Bernd Eckenfels, <net-tools@lina.inka.de> (NIS and manpage).
Michael Meskes, <meskes@debian.org>
net-tools
Linux上查看指定行数的文件内容
Linux 上 如何查看指定行数的内容呢,例如11~20行?
[root@node1 ~]# cat -n 2.txt |head -20 |tail -10
11 HOSTNAME(1) Linux Programmer's Manual HOSTNAME(1)
12
13
14
15 NAME
16 hostname - show or set the system's host name
17 domainname - show or set the system's NIS/YP domain name
18 ypdomainname - show or set the system's NIS/YP domain name
19 nisdomainname - show or set the system's NIS/YP domain name
20 dnsdomainname - show the system's DNS domain name

浙公网安备 33010602011771号