
命令 | 常用选项 | 说明 |
|---|---|---|
cat | -n 对输出内容中的所有行标注行号。 -b 对输出内容中的非空行标注行号。 | 查看文本文件的内容 |
head | -num 指定需要显示文件num行的内容。 | 默认查看文档前10行内容 |
tail | -num 指定需要显示文件num行的内容。-f 使tail不停地去读取和显示文件最新的内容, 这样有实时监视的效果。 tail命令更多的用于查看系统日志文件,按【Ctrl+C】 键终止显示和跟踪。 | 默认查看文档后10行内容 |
more | -c 从顶部清屏然后显示文件内容。 | 分页查看文件内容按Enter键向下逐行滚动 按空格键向下翻一屏按b键 向上翻一屏 文件末尾时more会自动退出 |
less | -c 从顶部清屏然后显示文件内容。-N 其作用是在每行前添加输出行号。 | 分页查看文件内容 按Enter键向下逐行滚动按空格键向下翻一屏按b键 向上翻一屏 按q键退出 |
head
tail
more
less
#:以下所有命令均可以结合管道符使用
[root@master test]# cat /etc/ssh/sshd_config
[root@master test]# head /etc/ssh/sshd_config
[root@master test]# tail /etc/ssh/sshd_config
[root@master test]# more /etc/ssh/sshd_config
[root@master test]# less /etc/ssh/sshd_configgrep 选项…… 关键字符串 文件名称……选项 | 说明 |
|---|---|
-c | 仅显示找到的行数 |
-i | 忽略大小写 |
-n | 显示行号 |
-v | 反向选择——仅列出没有“关键词”的行 |
-A | -A 2 搜索时显示匹配到的那一行以及下2行 |
-B | -B 2 搜索时显示匹配到的那一行以及上2行 |
-C | -C 2 搜索时显示匹配到的那一行以及上下2行 |
在Linux系统中,/etc/passwd文件是保存着所有的用户信息,
而一旦用户的登录终端被设置成/sbin/nologin,则不再允许登录系统,
因此可以使用grep命令来查找出当前系统中不允许登录系统的所有用户信息:
[root@kongd ~]# grep /sbin/nologin /etc/passwd
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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
………………省略部分输出过程信息………………匹配模式 | 说明 |
|---|---|
grep h 文件名 | 查找文件里有字符h的行 |
grep ^[q] 文件名 | 匹配以q开始的行 |
grep ^[qf] 文件名 | 匹配以q或者f开头的行 |
grep ^[ ^qf ] 文件名 | 不匹配以q或者f开头的行 |
grep ^[0-9] 文件名 | 匹配以数字开头的行 |
grep q$ 文件名 | 匹配以q结束的行 |
grep ^$ | 过滤空白行 |
grep -r h ./* | 如果要明确搜索当前目录中的子目录有h的行 |
grep -d skip h ./* | 忽略当前目录下的子目录下的普通文件 |
[root@master test]# grep a /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
………………省略部分输出过程信息………………[root@master test]# grep ^a /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@master test]# grep h$ /etc/passwd
root:x:0:0:root:/root:/bin/bash
centos:x:1000:1000:centos:/home/centos:/bin/bash
[root@master test]# grep -v h$ /etc/passwd
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
………………省略部分输出过程信息………………
cut [选项] 文件名称系统文件在保存用户数据信息时,每一项值之间是采用冒号来间隔的,先查看一下
[root@kongd ~]# head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin所以我们试一试使用下述命令尝试提取出passwd文件中的用户名信息,即提取以冒号(:)为间隔符号的第一列内容
root@kongd ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
………………省略部分输出信息………………
[root@server ~]# cut -c 3 /etc/passwd | head -3
o
n
esort [选项] 文件名称选项 | 作用 |
|---|---|
-f | 忽略大小写 |
-b | 忽略缩进与空格 |
-n | 以数值型排序 |
-r | 反向排序 |
-u | 去除重复行 |
-t | 指定间隔符 |
-k | 设置字段范围 |
[root@kongd ~]# cat fruit.txt
banana
pear
apple
orange
raspaberry
[root@kongd ~]# sort fruit.txt
apple
banana
orange
pear
raspaberry[root@kongd ~]# cat sort.txt
Welcome to kongd.com
Red Hat certified
Welcome to kongd.com
Free Linux Lessons
Linux Course
[root@kongd ~]# sort -u sort.txt
Free Linux Lessons
Red Hat certified
Welcome to kongd.com[root@kongd ~]# cat number.txt
45
12
3
98
82
67
24
56
9
[root@kongd ~]# sort -n number.txt
3
9
12
24
45
56
67
82
98内容是节选自/etc/passwd文件中前五个字段的内容,并进行混乱排序后的样子:
[root@kongd ~]# cat user.txt
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon
polkitd:x:998:996:User for polkitd
geoclue:x:997:995:User for geoclue
rtkit:x:172:172:RealtimeKit
pulse:x:171:171:PulseAudio System Daemon
qemu:x:107:107:qemu user
usbmuxd:x:113:113:usbmuxd user
unbound:x:996:991:Unbound DNS resolver
rpc:x:32:32:Rpcbind Daemon
gluster:x:995:990:GlusterFS daemons可以用-t参数指定间隔符,-k参数指定第几列,-n参数进行数字排序来搞解决它:
[root@kongd ~]# sort -t : -k 3 -n user.txt
rpc:x:32:32:Rpcbind Daemon
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon
qemu:x:107:107:qemu user
usbmuxd:x:113:113:usbmuxd user
pulse:x:171:171:PulseAudio System Daemon
rtkit:x:172:172:RealtimeKit
gluster:x:995:990:GlusterFS daemons
unbound:x:996:991:Unbound DNS resolver
geoclue:x:997:995:User for geoclue
polkitd:x:998:996:User for polkitduniq [选项] 文件名称[root@kongd ~]# cat uniq.txt
Welcome to kongd.com
Welcome to kongd.com
Welcome to kongd.com
Welcome to kongd.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@kongd ~]# uniq uniq.txt
Welcome to kongd.com
Red Hat certified
Free Linux Lessons
Professional guidance
Linux Course
[root@master ~]# uniq -c uniq.txt
4 Welcome to kongd.com
1 Red Hat certified
1 Free Linux Lessons
1 Professional guidance
1 Linux Coursetr [OPTION]…SET1[SET2]
参数说明:
-c 反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换
-d 删除指令字符
-s 缩减连续重复的字符成指定的单个字符将文件testfile中的小写字母全部转换成大写字母
[root@master ~]#cat testfile |tr a-z A-Z