1、文件内容查看命令
1.1查看文本文件内容
cat 可以查看文本内容
格式:
cat [OPTION]... [FILE]...
常见选项:
-E:显示行结束符$
-A:显示所有控制符
-n:对显示出的每一行进行编号
-b:非空行编号
tac 逆向显示文本内容
nl 显示行号,相当于
cat -b rev 将同一行的内容逆向显示
-s:压缩连续的空行成一行
1.2查看非文本文件内容
1.2.1 hexdump
范例:
hexdump -C -n 512 /dev/sda
00000000 eb 63 90 10 8e d0 bc 00 b0 b8 00 00 8e d8 8e c0 |.c..............|
echo {a..z} | tr -d ' '|hexdump -C
00000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 |abcdefghijklmnop|
00000010 71 72 73 74 75 76 77 78 79 7a 0a |qrstuvwxyz.|
0000001b
1.2.2 od
od 即 dump files in octal and other formats
范例:
[root@centos8 ~]#echo {a..z} | tr -d ' '|od -t x
0000000 64636261 68676665 6c6b6a69 706f6e6d
0000020 74737271 78777675 000a7a79
0000033
[root@centos8 ~]#echo {a..z} | tr -d ' '|od -t x1
0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70
0000020 71 72 73 74 75 76 77 78 79 7a 0a
0000033
[root@centos8 ~]#echo {a..z} | tr -d ' '|od -t x1z
0000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 >abcdefghijklmnop<
0000020 71 72 73 74 75 76 77 78 79 7a 0a >qrstuvwxyz.<
0000033
1.2.3 xxd
echo {a..z} | tr -d ' '|xxd
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.
2 、分页查看文件内容
more 可以实现分页查看文件,可以配合管道实现输出信息的分页
格式
more [OPTIONS...] FILE...
选项:
-d: 显示翻页及退出提示
less 也可以实现分页查看文件或STDIN输出
查看时有用的命令包括:
/文本 搜索 文本
n/N 跳到下一个 或 上一个匹配
less 命令是man命令使用的分页器
范例:
[root@centos8 ~]#cat /etc/init.d/functions |less
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.
#
TEXTDOMAIN=initscripts
# Make sure umask is sane
umask 022
# Set up a default search path.
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
...省略...
3 、显示文本前或后行内容
head 可以显示文件或标准输入的前面行
格式:
head [OPTION]... [FILE]...
选项:
-c # 指定获取前#字节
-n # 指定获取前#行
-# 同上
范例:
[root@centos8 ~]#head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos8 ~]#head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@centos8 ~]#echo a我b | head -c4
a我[root@centos8 ~]#
[root@centos8 ~]#cat /dev/urandom | tr -dc '[:alnum:]'| head -c10
G755MlZatW[root@centos8 ~]#cat /dev/urandom | tr -dc '[:alnum:]'| head -c10
ASsax6DeBz[root@centos8 ~]#cat /dev/urandom | tr -dc '[:alnum:]'| head -c10 |
tee pass.txt | passwd --stdin mage
Changing password for user mage.
passwd: all authentication tokens updated successfully.
[root@centos8 ~]#cat pass.txt
AGT952Essg[root@centos8 ~]#su - wang
[wang@centos8 ~]$su - mage
Password:
tail 和head 相反,查看文件或标准输入的倒数行
格式:
tail [OPTION]... [FILE]...
-c # 指定获取后#字节
-n # 指定获取后#行
-# 同上
-f 跟踪显示文件fd新追加的内容,常用日志监控,相当于 --follow=descriptor,当文件删除再新建同名文 件,将无法继续跟踪文件
-F 跟踪文件名,相当于--follow=name --retry,当文件删除再新建同名文件,将可以继续跟踪文件
tailf 类似tail –f,当文件不增长时并不访问文件
范例:
[root@centos8 ~]#tail -3 /var/log/messages
Dec 20 09:49:01 centos8 dbus-daemon[952]: [system] Successfully activated
service 'net.reactivated.Fprint'
Dec 20 09:49:01 centos8 systemd[1]: Started Fingerprint Authentication Daemon.
Dec 20 09:49:13 centos8 su[6887]: (to mage) root on pts/0
[root@centos8 ~]#tail -f /var/log/messages
Dec 20 08:36:40 centos8 systemd[1321]: Startup finished in 52ms.
Dec 20 08:36:40 centos8 systemd[1]: Started User Manager for UID 0.
Dec 20 08:47:01 centos8 systemd[1]: Starting dnf makecache...
Dec 20 08:47:02 centos8 dnf[1465]: AppStream
213 kB/s | 4.3 kB 00:00
Dec 20 08:47:02 centos8 dnf[1465]: BaseOS
163 kB/s | 3.9 kB 00:00
Dec 20 08:47:04 centos8 dnf[1465]: EPEL
2.6 kB/s | 5.3 kB 00:02
Dec 20 08:47:09 centos8 dnf[1465]: EPEL
884 kB/s | 4.3 MB 00:05
Dec 20 08:47:12 centos8 dnf[1465]: extras
727 B/s | 1.5 kB 00:02
Dec 20 08:47:12 centos8 dnf[1465]: Metadata cache created.
Dec 20 08:47:12 centos8 systemd[1]: Started dnf makecache.
[root@centos8 data]#ifconfig | head -2 | tail -1
│
inet 10.0.0.8 netmask 255.255.255.0 broadcast 10.0.0.255
4 、按列抽取文本
cut 命令可以提取文本文件或STDIN数据的指定列
格式
cut [OPTION]... [FILE]...
选项
-d DELIMITER: 指明分隔符,默认tab
-f FILEDS:
#: 第#个字段,例如:3
#,#[,#]:离散的多个字段,例如:1,3,6
#-#:连续的多个字段, 例如:1-6
混合使用:1-3,7
-c 按字符切割
--output-delimiter=STRING指定输出分隔符
范例:
[root@centos8 ~]#cut -d: -f1,3-4,7 /etc/passwd
[root@centos8 ~]#ifconfig |head -n2 |tail -n1|cut -d" " -f10
10.0.0.8
[root@centos8 ~]#ifconfig |head -n2 |tail -n1|tr -s " " |cut -d " " -f3
10.0.0.8
[root@centos8 ~]#df | tr -s ' '|cut -d' ' -f5 |tr -dc "[0-9\n]"
0
0
1
0
5
1
15
1
[root@centos8 ~]#df | tr -s ' ' % |cut -d% -f5 |tr -d '[:alpha:]'
0
0
1
0
5
1
15
1
[root@centos8 ~]#df | cut -c44-46 |tr -d '[:alpha:]'
0
0
1
0
5
1
15
1
[root@centos8 ~]#cut -d: -f1,3,7 --output-delimiter="---" /etc/passwd
root---0---/bin/bash
bin---1---/sbin/nologin
daemon---2---/sbin/nologin
cat /etc/passwd | cut -d: -f7
cut -c2-5 /usr/share/dict/words
5 、合并多个文件
paste 合并多个文件同行号的列到一行
格式
paste [OPTION]... [FILE]...
-d 分隔符:指定分隔符,默认用TAB
-s : 所有行合成一行显示
范例:
[root@centos8 ~]#cat alpha.log
a
b
c
d
e
f
g
h
[root@centos8 ~]#cat seq.log
1
2
3
4
5
[root@centos8 ~]#cat alpha.log seq.log
a
b
c
d
e
f
g
h
1
2
3
4
5
[root@centos8 ~]#paste alpha.log seq.log
a 1
b 2
c 3
d 4
e 5
f
g
h
[root@centos8 ~]#paste -d":" alpha.log seq.log
a:1
b:2
c:3
d:4
e:5
f:
g:
h:
[root@centos8 ~]#paste -s seq.log
1 2 3 4 5
[root@centos8 ~]#paste -s alpha.log
a b c d e f g h
[root@centos8 ~]#paste -s alpha.log seq.log
a b c d e f g h
1 2 3 4 5
[root@centos8 ~]#cat title.txt
ceo
coo
cto
[root@centos8 ~]#cat emp.txt
mage
zhang
wang
xu
[root@centos8 ~]#paste title.txt emp.txt
ceo mage
coo zhang
cto wang
xu
[root@centos8 ~]#paste -s title.txt emp.txt
ceo coo cto
mage zhang wang xu
6、 分析文本的工具
文本数据统计:wc
整理文本:sort
比较文件:diff和patch
6.1 收集文本统计数据
wc 命令可用于统计文件的行总数、单词总数、字节总数和字符总数
可以对文件或STDIN中的数据统计
常用选项
-l 只计数行数
-w 只计数单词总数
-c 只计数字节总数
-m 只计数字符总数
-L 显示文件中最长行的长度
范例:
wc story.txt
39 237 1901 story.txt
行数 单词数 字节数
[root@centos8 ~]#ll title.txt
-rw-r--r-- 1 root root 30 Dec 20 11:05 title.txt
[root@centos8 ~]#ll title1.txt
-rw-r--r-- 1 root root 28 Dec 20 11:06 title1.txt
[root@centos8 ~]#cat title.txt
ceo mage
coo zhang
cto 老王
[root@centos8 ~]#cat title1.txt
ceo mage
coo zhang
cto wang
[root@centos8 ~]#wc title.txt
3 6 30 title.txt
[root@centos8 ~]#wc title1.txt
3 6 28 title1.txt
[root@centos8 ~]#wc -l title.txt
3 title.txt
[root@centos8 ~]#cat title.txt | wc -l
3
[root@centos8 ~]#df | tail -n $(echo `df | wc -l`-1|bc)
devtmpfs 910220 0 910220 0% /dev
tmpfs 924728 0 924728 0% /dev/shm
tmpfs 924728 9224 915504 1% /run
tmpfs 924728 0 924728 0% /sys/fs/cgroup
/dev/sda2 104806400 4836160 99970240 5% /
/dev/sda3 52403200 398580 52004620 1% /data
/dev/sda1 999320 131764 798744 15% /boot
tmpfs 184944 4 184940 1% /run/user/0
6.2 文本排序sort
把整理过的文本显示在STDOUT,不改变原始文件
格式:
sort [options] file(s)
常用选项
-r 执行反方向(由上至下)整理
-R 随机排序
-n 执行按数字大小整理
-f 选项忽略(fold)字符串中的字符大小写
-u 选项(独特,unique)删除输出中的重复行
-t c 选项使用c作为字段界定符
-k # 选项按照使用c字符分隔的# 列来整理能够使用多次
范例:
[root@centos8 data]#df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort
0
0
0
1
1
1
15
5
[root@centos8 data]#df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort -n
0
0
0
1
1
1
5
15
[root@centos8 data]#df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort -n
|tail -n1
15
[root@centos8 data]#df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort -nr
15
5
1
1
1
0
0
0
[root@centos8 data]#df | tr -s " " %|cut -d% -f5|tr -d '[:alpha:]' | sort -
nr|head -n1
15
[root@centos8 data]#cut -d: -f1,3 /etc/passwd|sort -t: -k2 -nr |head -n3
nobody:65534
xiaoming:1002
mage:1001
[root@centos8 data]#cut -d" " -f1 access_log |sort -u|wc -l
201
6.3 去重uniq
uniq命令从输入中删除前后相接的重复的行
格式:
uniq [OPTION]... [FILE]...
常见选项:
-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行
uniq常和sort 命令一起配合使用:
范例:
sort userlist.txt | uniq -c
范例:
[root@centos8 data]#cut -d" " -f1 access_log |sort |uniq -c|sort -nr |head -3
4870 172.20.116.228
3429 172.20.116.208
2834 172.20.0.222
[root@centos8 data]#lastb -f btmp-34 | tr -s ' ' |cut -d ' ' -f3|sort |uniq -c
|sort -nr | head -3
86294 58.218.92.37
43148 58.218.92.26
18036 112.85.42.201
范例:
[root@centos8 data]#cat test1.txt
a
b
1
c
[root@centos8 data]#cat test2.txt
b
e
f
c
1
2
#取文件的共同行
[root@centos8 data]#cat test1.txt test2.txt | sort |uniq -d
1
b
c
#取文件的不同行
[root@centos8 data]#cat test1.txt test2.txt | sort |uniq -u
2
a
e
f
6.4 比较文件
diff 命令比较两个文件之间的区别
[root@centos8 ~]#cat f1.txt
mage
zhang
wang
xu
[root@centos8 ~]#cat f2.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#diff f1.txt f2.txt
1,2c1,2
< mage
< zhang
---
> magedu
> zhang sir
4a5
> shi
[root@centos8 ~]#diff -u f1.txt f2.txt
--- f1.txt 2026-03-16T10:36:22+00:00.892775671 +0800
+++ f2.txt 2026-03-16T10:36:22+00:00.373677728 +0800
@@ -1,4 +1,5 @@
-mage
-zhang
+magedu
+zhang sir
wang
xu
+shi
[root@centos8 ~]#diff -u f1.txt f2.txt > f.patch
[root@centos8 ~]#rm -f f2.txt
[root@centos8 ~]#patch -b f1.txt f.patch
patching file f1.txt
[root@centos8 ~]#cat f1.txt
magedu
zhang sir
wang
xu
shi
[root@centos8 ~]#cat f1.txt.orig
mage
zhang
wang
xu
注明第5行有区别(改变)
复制对文件改变patch
diff 命令的输出被保存在一种叫做“补丁”的文件中
使用 -u 选项来输出“统一的(unified)”diff格式文件,最适用于补丁文件
patch 复制在其它文件中进行的改变(要谨慎使用)
适用 -b 选项来自动备份改变了的文件
范例:
diff -u foo.conf foo2.conf > foo.patch
patch -b foo.conf foo.patch
范例:查看二进制文件的不同
[root@centos8 data]#ll /usr/bin/dir /usr/bin/ls
-rwxr-xr-x. 1 root root 166448 May 12 2019 /usr/bin/dir
-rwxr-xr-x. 1 root root 166448 May 12 2019 /usr/bin/ls
[root@centos8 data]#ll /usr/bin/dir /usr/bin/ls -i
201839444 -rwxr-xr-x. 1 root root 166448 May 12 2019 /usr/bin/dir
201839465 -rwxr-xr-x. 1 root root 166448 May 12 2019 /usr/bin/ls
[root@centos8 data]#diff /usr/bin/dir /usr/bin/ls
Binary files /usr/bin/dir and /usr/bin/ls differ
[root@centos8 ~]#cmp /bin/dir /bin/ls
/bin/dir /bin/ls differ: byte 737, line 2
[root@centos8 ~]#hexdump -s 730 -Cn 7 /bin/dir
000002da 00 00 47 4e 55 00 b0 |..GNU..|
000002e1
[root@centos8 ~]#hexdump -s 730 -Cn 7 /bin/ls
000002da 00 00 47 4e 55 00 93 |..GNU..|
000002e1
以上内容由马哥教育整理,由需要详细Linux教程(视频+文档的),可点赞评论文章后,关注我私信“Linux学习”索要!