1.cat命令
命令说明
cat命令主要的作用是查看文件,会把文件的全部内容一次性展示出来.
cat命令来自英文concatenate files and print on the standard output (连接文件内容并打印到标准输出)
的首字母缩写,cat命令不仅可以将单个文件输出到屏幕上,还可以把多个文件的内容连接起来一块输出到屏幕上
语法格式
cat [参数] [文件A] [文件B] .....
参数说明
参数 作用 助记 -n 从1开始,对输出的内容进行编号 number实例
使用cat 查看单个文件的内容并输出到屏幕上
[root@localhost ~]# cat fileA.txt
---------------fileA start---------------------
AA
BB
CC
---------------fileA end---------------------
使用cat查看多个文件并输出到屏幕上
[root@localhost ~]# cat fileA.txt fileB.txt
---------------fileA start---------------------
AA
BB
CC
---------------fileA end---------------------
---------------fileB start---------------------
AA
BB
CC
---------------fileB end---------------------
使用-n参数,给输出的内容的每一行编号
[root@localhost ~]# cat -n fileA.txt
1 ---------------fileA start---------------------
2 AA
3 BB
4 CC
5 ---------------fileA end---------------------
因为cat命令结果的最终的去向是一个标准输出,这个标准输出既可以是我们最熟悉的屏幕,也可以是一个文件,所以下面的命令会把文件A和文件B的内容合并,写入到文件C
[root@localhost ~]# cat fileA.txt fileB.txt > fileC.txt
查看文件c的内容
[root@localhost ~]# cat fileC.txt
---------------fileA start---------------------
AA
BB
CC
---------------fileA end---------------------
---------------fileB start---------------------
AA
BB
CC
---------------fileB end---------------------
2.head命令
命令说明
head命令的作用是查看文件开头部分的内容,如果不指定行数,默认查看文件开头十行的内容
命令格式
head [参数] [文件]
参数说明
参数 作用 助记 -n 指定要查看文件内容的行数 number实例
默认查看文件前十行的内容
[root@localhost ~]# head ccc.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
使用-n参数,指定只输出前两行
[root@localhost ~]# head -n 2 ccc.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
使用-n参数的简写
[root@localhost ~]# head -2 ccc.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
3.tail 命令
命令说明
tail命令的主要作用是查看文件结尾部分的内容。
命令格式
tail [参数] [文件]
参数说明
参数 作用 助记 -n 指定要显示的行数(和head命令的-n参数用法基本一致) number -f 显示追加写入的内容 follow实例
tail不带参数,默认显示文件最后十行的内容
[root@localhost ~]# tail ccc.txt
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
user1:x:1000:1000::/home/user1:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
old:x:1001:1001::/home/old:/bin/bash
gitlab-www:x:997:994::/var/opt/gitlab/nginx:/bin/false
git:x:996:993::/var/opt/gitlab:/bin/sh
gitlab-redis:x:995:992::/var/opt/gitlab/redis:/bin/false
gitlab-psql:x:994:991::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh
使用-n 参数指定要查看的行数,只查看文件最后两行的内容
[root@localhost ~]# tail -n 2 ccc.txt
gitlab-psql:x:994:991::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh
和head类似.tail命令的-n参数也可以使用简写
[root@localhost ~]# tail -2 ccc.txt
gitlab-psql:x:994:991::/var/opt/gitlab/postgresql:/bin/sh
gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh
4.grep命令
命令说明
grep命令是 g lobal search r egular e xpression(RE) and p rint out the line的缩写,作用是从文本文件或管道数据流之中筛选匹配数据的文本行
命令格式
grep [选项] 模式 [文件]
参数说明
参数 作用 助记 -n 显示匹配的行和行号 line-number -v 显示不匹配的行 invert-match -i 忽略大小写 ignore-case -E 使用扩展正则 extended-regexp -w 只匹配整个单词,而不是字符串的一部分 word -A <显示行数> 显示匹配行及后面n行的内容 after-context -B<显示行数> 显示匹配行及前面n行的内容 before-context -C<显示行数> 显示匹配行及前后n行的内容 context命令格式
grep [选项] 模式 [文件]
实例
从文件ccc.txt中匹配包含字符串root的行
[root@localhost ~]# grep "root" ccc.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
"root"前后的双引号也可以不写,也会被系统识别为字符串,下面的命令具有相同的效果
[root@localhost ~]# grep root ccc.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
使用-n参数,查看到匹配到字符串root的行是文件ccc.txt的第一行和第十行
[root@localhost ~]# grep -n "root" ccc.txt
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin
grep 命令最强大的地方之一是可以和正则表达式(RE)结合使用,比如下面使用了正则表达式的^符号,该符号的作用是匹配一段文本的开始,所以"^root"的作用是匹配以root开始的行,匹配的结果和上面命令对比,第一行包含root的内容被保留了下来,第十行的内容没有被匹配到
[root@localhost ~]# grep "^root" ccc.txt
root:x:0:0:root:/root:/bin/bash
使用-v参数,反转匹配结果,匹配不包含字符串root的行。为了便于解释结果,同时使用了-n参数,可以非常明显的看到除了第一行和第十行包含字符串root的所有行都被显示了出来
[root@localhost ~]# grep -nv "root" ccc.txt
2:bin:x:1:1:bin:/bin:/sbin/nologin
3:daemon:x:2:2:daemon:/sbin:/sbin/nologin
4:adm:x:3:4:adm:/var/adm:/sbin/nologin
5:lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
9:mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11:games:x:12:100:games:/usr/games:/sbin/nologin
12:ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13:nobody:x:99:99:Nobody:/:/sbin/nologin
14:systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15:dbus:x:81:81:System message bus:/:/sbin/nologin
16:polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17:sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
18:postfix:x:89:89::/var/spool/postfix:/sbin/nologin
19:user1:x:1000:1000::/home/user1:/bin/bash
20:apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
21:saslauth:x:998:76:Saslauthd user:/run/saslauthd:/sbin/nologin
22:old:x:1001:1001::/home/old:/bin/bash
23:gitlab-www:x:997:994::/var/opt/gitlab/nginx:/bin/false
24:git:x:996:993::/var/opt/gitlab:/bin/sh
25:gitlab-redis:x:995:992::/var/opt/gitlab/redis:/bin/false
26:gitlab-psql:x:994:991::/var/opt/gitlab/postgresql:/bin/sh
27:gitlab-prometheus:x:993:990::/var/opt/gitlab/prometheus:/bin/sh
使用-i参数忽略匹配模式字符串的大小写,首先看一下没有使用-i 参数的匹配结果.只配到了一行内容。使用了-i参数之后三行内容都匹配到了
[root@localhost ~]# grep "hello" hello.txt
hello,world!
[root@localhost ~]# grep -i "hello" hello.txt
HELLO, WORLD!
hello,world!
Hello,World!
不使用-e参数的正则表达式是有限的,比如下面命令中{4}正则符号是不能被识别的,其中[0-9]表示匹配0到9之间的任一数字,{4}表示前面的表达式会匹配四次,整个表达式结果是匹配连续四位都是数字的字符串
单纯使用grep,‘{4}‘没有被识别成正则表达式
[root@localhost ~]# grep "[0-9]{4}" ccc.txt
添加-e参数,'{4}'被正确识别识别为正则表达式
[root@localhost ~]# grep -E "[0-9]{4}" ccc.txt
user1:x:1000:1000::/home/user1:/bin/bash
old:x:1001:1001::/home/old:/bin/bash
使用-w参数只匹配me单词,不匹配单词中包含me的
[root@localhost ~]# grep "me" hello2.txt
home
me
[root@localhost ~]# grep -w "me" hello2.txt
me
使用-A参数显示匹配行和该匹配行后面五行的内容
[root@localhost ~]# grep -A 5 "scontext" ls.txt
--scontext
Display only security context and file name.
--help display this help and exit
--version
使用-B参数显示匹配行和该匹配行前面五行的内容
[root@localhost ~]# grep -B 5 "scontext" ls.txt
-Z, --context
Display security context so it fits on most displays. Displays only mode, user, group, security context and file
name.
--scontext
使用-C参数显示匹配行前后五行的内容
[root@localhost ~]# grep -C 5 "scontext" ls.txt
-Z, --context
Display security context so it fits on most displays. Displays only mode, user, group, security context and file
name.
--scontext
Display only security context and file name.
--help display this help and exit
--version
前面介绍的几个查看文件的命令都是非交互式的,输完命令敲击回车之后命令的执行就结束了,下面介绍的more和less命令则属于交互式的命令,敲下回车之后命令的执行不会结束,而是进入了交互式界面,通过输入关键字和快捷键,可以执行对文件内容翻页,搜索关键字等更丰富的操作
5.more命令
命令说明
more命令打开的文件会逐屏显示文件内容,可以向上或者向下滚动,到达文件底部之后,more命令自动退出。
命令格式
more [文件]
交互式指令说明
指令 作用 助记 f 向下(前进)翻页 forward b 向上(后退)翻页 backwards space 和f类似,也是向下翻页 enter 向下滚动一行 q 退出交互式界面 quit实例
下面通过一个gif展示一下交互式指令,左上方实时显示了操作时键盘上的按键
编辑
6.less命令
命令说明
从名字上看less应该是more的弱化版本,但实际恰恰相反,less命令是对more命令的加强(less名称来自俗语“越简单就越丰富”,即less is more)。除了拥有more拥有的功能外,还多了不少功能。
命令格式
less [选项] [文件]
参数说明
选项参数说明
参数 作用 助记 -M 显示类似more命令的百分比 more -N 显示每行的行号 number交互式指令说明
除了拥有more的交互式指令,less还支持字符串搜索
指令 作用 助记 /关键字 向下搜索"关键字" n 跳转到下一个关键字的位置 next N 跳转到上一个关键字的位置 pgDn 向下翻动一行 pgUp 向上翻动一行实例
使用less命令打开ls.txt文件,并在文件中搜索sort关键字,使用n和N进行关键字跳转