前言
在Linux的世界里,掌握查看文件内容的技巧是每位用户的必备技能。 本篇将带你领略cat、more、和less这三个命令的使用方法,让你在探索文件海洋时游刃有余。 最后,简要提及tail和head这两个小指令,助你快速浏览文件的开头或结尾。
原帖在和鲸社区可在线运行
前置操作
由于手头的文件行数过多,我先给一个下期预告:管道和重定向 来制作一个测试文本文件
In [12]:
代码语言:javascript复制
代码语言:javascript复制!tail -n 30 /home/mw/input/meteva7050/nmc_met_tutorial-master/nmc_met_tutorial-master/meteva/input/meb/m1.txt > new.txt
代码语言:javascript复制
1. cat:欲穷千里目
基本用法 cat(concatenate)命令是最直接的查看文件内容的方式,它会一口气把整个文件的内容输出到屏幕上。
In [13]:
代码语言:javascript复制
代码语言:javascript复制cat /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制12585 21.70 50.68 218 1 9999 350 2 193 0 0 0 4 9999 9999 9999 18 8 0 18.7 9999 9999 9999 9999
12595 23.20 50.68 213 1 9999 10 1 197 0 11 11 0.01 9999 9999 9999 18 15 0 18.4 9999 9999 9999 9999
12600 19.00 49.80 399 1 0 50 4 181 0 1 1 9999 9999 9999 9999 17 25 2 19.6 9999 9999 9999 9999
12650 19.97 49.22 1988 1 1 190 6 9999 0 4 1 9999 9999 0 9999 11 50 1 11.9 9999 11 9999 9999
12660 20.68 49.62 295 1 9999 360 2 185 0 0 0 2 9999 9999 9999 18 7 0 18.7 9999 9999 9999 9999
12210 15.40 53.77 55 1 9999 0 1 176 0 0 0 9999 9999 9999 9999 19 9999 100 20.4 9999 9999 9999 9999
12230 16.73 53.12 73 1 9999 0 0 186 0 11 11 9999 9999 9999 9999 18 17 0 21.1 9999 9999 9999 9999
12310 14.58 52.33 24 1 0 140 2 171 0 1 1 9999 9999 9999 9999 17 30 2 22.1 9999 9999 9999 9999
12399 23.62 52.07 137 1 7 0 0 208 0 1 1 9999 35 25 1000 18 8 10 18.4 23 9999 9999 9999
12418 16.53 51.83 93 1 9999 120 1 180 0 11 11 9999 9999 9999 9999 17 19 0 20.9 9999 9999 9999 9999
12469 19.85 51.35 189 1 1 0 0 192 0 1 1 9999 9999 10 9999 17 12 2 18.2 23 11 9999 9999
12488 21.53 51.55 126 1 4 180 1 200 0 1 1 9999 9999 10 9999 16 5 10 17.3 23 11 9999 9999
12540 18.18 50.05 205 1 2 60 1 179 0 1 1 9999 9999 0 9999 17 27 2 21.3 9999 12 9999 9999
16333 18.14 40.24 47 1 9999 310 3 114 0 0 0 9999 9999 9999 9999 22 9999 0 23.1 9999 9999 9999 9999
12560 19.02 50.23 280 1 7 70 1 187 0 1 1 9999 9999 60 9999 17 30 2 19.2 23 12 9999 9999
12625 19.95 49.28 857 1 9 140 1 9999 0 8 1 0.5 35 90 200 15 8 10 16.7 9999 9999 9999 9999
12670 21.77 49.70 331 1 6 80 2 184 0 1 1 9999 9999 60 9999 17 22 2 19.6 25 12 9999 9999
12690 22.33 49.45 422 1 5 90 2 186 0 1 1 9999 9999 40 9999 18 22 2 18.9 23 12 9999 9999
12695 22.77 49.80 280 1 9999 0 1 192 0 0 0 9999 9999 9999 9999 18 9999 100 19.3 9999 9999 9999 9999
64910 9.70 4.02 9 1 9 0 0 144 9999 6 6 15 39 90 300 23 8 60 24.0 9999 9999 9999 9999
01023 18.53 69.05 76 1 9999 0 0 169 0 0 0 9999 9999 9999 9999 17 9999 0 17.3 9999 9999 9999 9999
01425 6.50 58.50 66 1 9999 320 1 184 1 0 0 2 9999 9999 9999 14 9999 0 14.9 9999 9999 9999 9999
01424 6.35 59.63 2 1 9999 0 0 176 1 0 0 26 9999 9999 9999 15 9999 0 16.8 9999 9999 9999 9999
01150 14.95 66.48 251 1 9999 100 1 9999 9999 0 0 0.8 9999 9999 9999 16 9999 0 16.1 9999 9999 9999 9999
01488 10.82 60.12 170 1 9999 350 1 9999 9999 0 0 8 9999 9999 9999 16 9999 0 16.9 9999 9999 9999 9999
48679 103.67 1.63 40 1 9 320 0 118 9999 6 2 9999 39 40 300 24 10 61 24.4 24 18 9999 9999
16364 16.37 38.76 996 1 9999 240 2 9999 0 0 0 9999 9999 9999 9999 17 9999 0 18.4 9999 9999 9999 9999
16415 14.87 38.58 45 1 9999 180 0 137 0 0 0 9999 9999 9999 9999 23 9999 0 24.1 9999 9999 9999 9999
16081 9.26 45.46 107 1 9999 0 0 146 0 0 0 9999 9999 9999 9999 24 9999 0 26.7 9999 9999 9999 9999
16314 16.66 40.68 496 1 9999 10 5 150 0 0 0 9999 9999 9999 9999 17 9999 0 22.9 9999 9999 9999 9999
参数 -n :显示行数 -b :不显示空行行数
In [14]:
代码语言:javascript复制
代码语言:javascript复制cat -n /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制 1 12585 21.70 50.68 218 1 9999 350 2 193 0 0 0 4 9999 9999 9999 18 8 0 18.7 9999 9999 9999 9999
2 12595 23.20 50.68 213 1 9999 10 1 197 0 11 11 0.01 9999 9999 9999 18 15 0 18.4 9999 9999 9999 9999
3 12600 19.00 49.80 399 1 0 50 4 181 0 1 1 9999 9999 9999 9999 17 25 2 19.6 9999 9999 9999 9999
4 12650 19.97 49.22 1988 1 1 190 6 9999 0 4 1 9999 9999 0 9999 11 50 1 11.9 9999 11 9999 9999
5 12660 20.68 49.62 295 1 9999 360 2 185 0 0 0 2 9999 9999 9999 18 7 0 18.7 9999 9999 9999 9999
6 12210 15.40 53.77 55 1 9999 0 1 176 0 0 0 9999 9999 9999 9999 19 9999 100 20.4 9999 9999 9999 9999
7 12230 16.73 53.12 73 1 9999 0 0 186 0 11 11 9999 9999 9999 9999 18 17 0 21.1 9999 9999 9999 9999
8 12310 14.58 52.33 24 1 0 140 2 171 0 1 1 9999 9999 9999 9999 17 30 2 22.1 9999 9999 9999 9999
9 12399 23.62 52.07 137 1 7 0 0 208 0 1 1 9999 35 25 1000 18 8 10 18.4 23 9999 9999 9999
10 12418 16.53 51.83 93 1 9999 120 1 180 0 11 11 9999 9999 9999 9999 17 19 0 20.9 9999 9999 9999 9999
11 12469 19.85 51.35 189 1 1 0 0 192 0 1 1 9999 9999 10 9999 17 12 2 18.2 23 11 9999 9999
12 12488 21.53 51.55 126 1 4 180 1 200 0 1 1 9999 9999 10 9999 16 5 10 17.3 23 11 9999 9999
13 12540 18.18 50.05 205 1 2 60 1 179 0 1 1 9999 9999 0 9999 17 27 2 21.3 9999 12 9999 9999
14 16333 18.14 40.24 47 1 9999 310 3 114 0 0 0 9999 9999 9999 9999 22 9999 0 23.1 9999 9999 9999 9999
15 12560 19.02 50.23 280 1 7 70 1 187 0 1 1 9999 9999 60 9999 17 30 2 19.2 23 12 9999 9999
16 12625 19.95 49.28 857 1 9 140 1 9999 0 8 1 0.5 35 90 200 15 8 10 16.7 9999 9999 9999 9999
17 12670 21.77 49.70 331 1 6 80 2 184 0 1 1 9999 9999 60 9999 17 22 2 19.6 25 12 9999 9999
18 12690 22.33 49.45 422 1 5 90 2 186 0 1 1 9999 9999 40 9999 18 22 2 18.9 23 12 9999 9999
19 12695 22.77 49.80 280 1 9999 0 1 192 0 0 0 9999 9999 9999 9999 18 9999 100 19.3 9999 9999 9999 9999
20 64910 9.70 4.02 9 1 9 0 0 144 9999 6 6 15 39 90 300 23 8 60 24.0 9999 9999 9999 9999
21 01023 18.53 69.05 76 1 9999 0 0 169 0 0 0 9999 9999 9999 9999 17 9999 0 17.3 9999 9999 9999 9999
22 01425 6.50 58.50 66 1 9999 320 1 184 1 0 0 2 9999 9999 9999 14 9999 0 14.9 9999 9999 9999 9999
23 01424 6.35 59.63 2 1 9999 0 0 176 1 0 0 26 9999 9999 9999 15 9999 0 16.8 9999 9999 9999 9999
24 01150 14.95 66.48 251 1 9999 100 1 9999 9999 0 0 0.8 9999 9999 9999 16 9999 0 16.1 9999 9999 9999 9999
25 01488 10.82 60.12 170 1 9999 350 1 9999 9999 0 0 8 9999 9999 9999 16 9999 0 16.9 9999 9999 9999 9999
26 48679 103.67 1.63 40 1 9 320 0 118 9999 6 2 9999 39 40 300 24 10 61 24.4 24 18 9999 9999
27 16364 16.37 38.76 996 1 9999 240 2 9999 0 0 0 9999 9999 9999 9999 17 9999 0 18.4 9999 9999 9999 9999
28 16415 14.87 38.58 45 1 9999 180 0 137 0 0 0 9999 9999 9999 9999 23 9999 0 24.1 9999 9999 9999 9999
29 16081 9.26 45.46 107 1 9999 0 0 146 0 0 0 9999 9999 9999 9999 24 9999 0 26.7 9999 9999 9999 9999
30 16314 16.66 40.68 496 1 9999 10 5 150 0 0 0 9999 9999 9999 9999 17 9999 0 22.9 9999 9999 9999 9999
In [15]:
代码语言:javascript复制
代码语言:javascript复制cat -b /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制 1 12585 21.70 50.68 218 1 9999 350 2 193 0 0 0 4 9999 9999 9999 18 8 0 18.7 9999 9999 9999 9999
2 12595 23.20 50.68 213 1 9999 10 1 197 0 11 11 0.01 9999 9999 9999 18 15 0 18.4 9999 9999 9999 9999
3 12600 19.00 49.80 399 1 0 50 4 181 0 1 1 9999 9999 9999 9999 17 25 2 19.6 9999 9999 9999 9999
4 12650 19.97 49.22 1988 1 1 190 6 9999 0 4 1 9999 9999 0 9999 11 50 1 11.9 9999 11 9999 9999
5 12660 20.68 49.62 295 1 9999 360 2 185 0 0 0 2 9999 9999 9999 18 7 0 18.7 9999 9999 9999 9999
6 12210 15.40 53.77 55 1 9999 0 1 176 0 0 0 9999 9999 9999 9999 19 9999 100 20.4 9999 9999 9999 9999
7 12230 16.73 53.12 73 1 9999 0 0 186 0 11 11 9999 9999 9999 9999 18 17 0 21.1 9999 9999 9999 9999
8 12310 14.58 52.33 24 1 0 140 2 171 0 1 1 9999 9999 9999 9999 17 30 2 22.1 9999 9999 9999 9999
9 12399 23.62 52.07 137 1 7 0 0 208 0 1 1 9999 35 25 1000 18 8 10 18.4 23 9999 9999 9999
10 12418 16.53 51.83 93 1 9999 120 1 180 0 11 11 9999 9999 9999 9999 17 19 0 20.9 9999 9999 9999 9999
11 12469 19.85 51.35 189 1 1 0 0 192 0 1 1 9999 9999 10 9999 17 12 2 18.2 23 11 9999 9999
12 12488 21.53 51.55 126 1 4 180 1 200 0 1 1 9999 9999 10 9999 16 5 10 17.3 23 11 9999 9999
13 12540 18.18 50.05 205 1 2 60 1 179 0 1 1 9999 9999 0 9999 17 27 2 21.3 9999 12 9999 9999
14 16333 18.14 40.24 47 1 9999 310 3 114 0 0 0 9999 9999 9999 9999 22 9999 0 23.1 9999 9999 9999 9999
15 12560 19.02 50.23 280 1 7 70 1 187 0 1 1 9999 9999 60 9999 17 30 2 19.2 23 12 9999 9999
16 12625 19.95 49.28 857 1 9 140 1 9999 0 8 1 0.5 35 90 200 15 8 10 16.7 9999 9999 9999 9999
17 12670 21.77 49.70 331 1 6 80 2 184 0 1 1 9999 9999 60 9999 17 22 2 19.6 25 12 9999 9999
18 12690 22.33 49.45 422 1 5 90 2 186 0 1 1 9999 9999 40 9999 18 22 2 18.9 23 12 9999 9999
19 12695 22.77 49.80 280 1 9999 0 1 192 0 0 0 9999 9999 9999 9999 18 9999 100 19.3 9999 9999 9999 9999
20 64910 9.70 4.02 9 1 9 0 0 144 9999 6 6 15 39 90 300 23 8 60 24.0 9999 9999 9999 9999
21 01023 18.53 69.05 76 1 9999 0 0 169 0 0 0 9999 9999 9999 9999 17 9999 0 17.3 9999 9999 9999 9999
22 01425 6.50 58.50 66 1 9999 320 1 184 1 0 0 2 9999 9999 9999 14 9999 0 14.9 9999 9999 9999 9999
23 01424 6.35 59.63 2 1 9999 0 0 176 1 0 0 26 9999 9999 9999 15 9999 0 16.8 9999 9999 9999 9999
24 01150 14.95 66.48 251 1 9999 100 1 9999 9999 0 0 0.8 9999 9999 9999 16 9999 0 16.1 9999 9999 9999 9999
25 01488 10.82 60.12 170 1 9999 350 1 9999 9999 0 0 8 9999 9999 9999 16 9999 0 16.9 9999 9999 9999 9999
26 48679 103.67 1.63 40 1 9 320 0 118 9999 6 2 9999 39 40 300 24 10 61 24.4 24 18 9999 9999
27 16364 16.37 38.76 996 1 9999 240 2 9999 0 0 0 9999 9999 9999 9999 17 9999 0 18.4 9999 9999 9999 9999
28 16415 14.87 38.58 45 1 9999 180 0 137 0 0 0 9999 9999 9999 9999 23 9999 0 24.1 9999 9999 9999 9999
29 16081 9.26 45.46 107 1 9999 0 0 146 0 0 0 9999 9999 9999 9999 24 9999 0 26.7 9999 9999 9999 9999
30 16314 16.66 40.68 496 1 9999 10 5 150 0 0 0 9999 9999 9999 9999 17 9999 0 22.9 9999 9999 9999 9999
小贴士
当文件较长时,使用cat可能导致屏幕快速滚动,难以阅读。 可以用来合并多个文件内容到一个新文件中,如cat file1.txt file2.txt > combined.txt。
2. more:更上一页书
基本用法
与cat不同,more命令提供分页查看的功能,一次只显示一屏内容,非常适合阅读长文件。
In [16]:
代码语言:javascript复制!
代码语言:javascript复制more /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制12585 21.70 50.68 218 1 9999 350 2 193 0 0 0 4 9999 9999 9
999 18 8 0 18.7 9999 9999 9999 9999
12595 23.20 50.68 213 1 9999 10 1 197 0 11 11 0.01 9999 9999 9
999 18 15 0 18.4 9999 9999 9999 9999
12600 19.00 49.80 399 1 0 50 4 181 0 1 1 9999 9999 9999 9
999 17 25 2 19.6 9999 9999 9999 9999
12650 19.97 49.22 1988 1 1 190 6 9999 0 4 1 9999 9999 0 9
999 11 50 1 11.9 9999 11 9999 9999
12660 20.68 49.62 295 1 9999 360 2 185 0 0 0 2 9999 9999 9
999 18 7 0 18.7 9999 9999 9999 9999
12210 15.40 53.77 55 1 9999 0 1 176 0 0 0 9999 9999 9999 9
999 19 9999 100 20.4 9999 9999 9999 9999
12230 16.73 53.12 73 1 9999 0 0 186 0 11 11 9999 9999 9999 9
999 18 17 0 21.1 9999 9999 9999 9999
12310 14.58 52.33 24 1 0 140 2 171 0 1 1 9999 9999 9999 9
999 17 30 2 22.1 9999 9999 9999 9999
12399 23.62 52.07 137 1 7 0 0 208 0 1 1 9999 35 25 1
000 18 8 10 18.4 23 9999 9999 9999
12418 16.53 51.83 93 1 9999 120 1 180 0 11 11 9999 9999 9999 9
999 17 19 0 20.9 9999 9999 9999 9999
12469 19.85 51.35 189 1 1 0 0 192 0 1 1 9999 9999 10 9
999 17 12 2 18.2 23 11 9999 9999
12488 21.53 51.55 126 1 4 180 1 200 0 1 1 9999 9999 10 9
m--More--(38%)
操作指南
空格键:向下翻页。 回车键:向下滚动一行。 b键:返回上一屏。 q键:退出more。
3. less:灵活自如
基本用法
less是more的一个plus版,所谓less is more 不仅支持上下翻页,还支持向前向后搜索,更加灵活强大。
In [17]:
代码语言:javascript复制!
代码语言:javascript复制less /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制/bin/sh: 1: less: not found
当前镜像似乎没安装less
高级操作
/keyword:向前搜索关键词。 n:查找下一个匹配项。 N:反向查找前一个匹配项。 pgup/pgdn或b/spc:翻页。 q:退出less
4. head 和 tail:瞻前顾后
head
用于查看文件的前几行,默认显示前10行。 tail
用于查看文件的后几行,默认显示后10行,非常适合检查日志文件的最新记录
In [18]:
代码语言:javascript复制!
代码语言:javascript复制head /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制12585 21.70 50.68 218 1 9999 350 2 193 0 0 0 4 9999 9999 9999 18 8 0 18.7 9999 9999 9999 9999
12595 23.20 50.68 213 1 9999 10 1 197 0 11 11 0.01 9999 9999 9999 18 15 0 18.4 9999 9999 9999 9999
12600 19.00 49.80 399 1 0 50 4 181 0 1 1 9999 9999 9999 9999 17 25 2 19.6 9999 9999 9999 9999
12650 19.97 49.22 1988 1 1 190 6 9999 0 4 1 9999 9999 0 9999 11 50 1 11.9 9999 11 9999 9999
12660 20.68 49.62 295 1 9999 360 2 185 0 0 0 2 9999 9999 9999 18 7 0 18.7 9999 9999 9999 9999
12210 15.40 53.77 55 1 9999 0 1 176 0 0 0 9999 9999 9999 9999 19 9999 100 20.4 9999 9999 9999 9999
12230 16.73 53.12 73 1 9999 0 0 186 0 11 11 9999 9999 9999 9999 18 17 0 21.1 9999 9999 9999 9999
12310 14.58 52.33 24 1 0 140 2 171 0 1 1 9999 9999 9999 9999 17 30 2 22.1 9999 9999 9999 9999
12399 23.62 52.07 137 1 7 0 0 208 0 1 1 9999 35 25 1000 18 8 10 18.4 23 9999 9999 9999
12418 16.53 51.83 93 1 9999 120 1 180 0 11 11 9999 9999 9999 9999 17 19 0 20.9 9999 9999 9999 9999
In [20]:
代码语言:javascript复制!
代码语言:javascript复制tail /home/mw/project/new.txt
代码语言:javascript复制
代码语言:javascript复制01023 18.53 69.05 76 1 9999 0 0 169 0 0 0 9999 9999 9999 9999 17 9999 0 17.3 9999 9999 9999 9999
01425 6.50 58.50 66 1 9999 320 1 184 1 0 0 2 9999 9999 9999 14 9999 0 14.9 9999 9999 9999 9999
01424 6.35 59.63 2 1 9999 0 0 176 1 0 0 26 9999 9999 9999 15 9999 0 16.8 9999 9999 9999 9999
01150 14.95 66.48 251 1 9999 100 1 9999 9999 0 0 0.8 9999 9999 9999 16 9999 0 16.1 9999 9999 9999 9999
01488 10.82 60.12 170 1 9999 350 1 9999 9999 0 0 8 9999 9999 9999 16 9999 0 16.9 9999 9999 9999 9999
48679 103.67 1.63 40 1 9 320 0 118 9999 6 2 9999 39 40 300 24 10 61 24.4 24 18 9999 9999
16364 16.37 38.76 996 1 9999 240 2 9999 0 0 0 9999 9999 9999 9999 17 9999 0 18.4 9999 9999 9999 9999
16415 14.87 38.58 45 1 9999 180 0 137 0 0 0 9999 9999 9999 9999 23 9999 0 24.1 9999 9999 9999 9999
16081 9.26 45.46 107 1 9999 0 0 146 0 0 0 9999 9999 9999 9999 24 9999 0 26.7 9999 9999 9999 9999
16314 16.66 40.68 496 1 9999 10 5 150 0 0 0 9999 9999 9999 9999 17 9999 0 22.9 9999 9999 9999 9999
附加技巧
使用-n指定显示行数,如head -n 5显示前5行,tail -n 3显示后3行。 另外,我们经常使用tail -f,可以实时监控文件的末尾变化,常用于日志跟踪。
小结
通过上述介绍,你已经掌握了在Linux中查看文件内容的多种姿势。 无论是快速浏览、详细阅读还是高效定位信息,cat、more、less、head和tail都是你科研路上不可或缺的得力助手。