Linux文件内容操作(一) 查看文件内容 cat
Linux使用cat(concatenate)将文件内容的全文显示在屏幕上。
cat常用的命令行选项:
选项 | 作用 |
-A | 显示所有字符,同-vET |
-v | 显示非打印字符,不包括换行符和制表符 |
-E | 将换行符(ASCII 10)显示为$ |
-T | 将制表符(ASCII 9)显示为^I |
-e | 显示除制表符以外的全部字符,同-vE |
-t | 显示除换行符以外的全部字符,同-vT |
-s | 将相邻的多个空行压缩为一个空行 |
-n | 为输出的行标出行号 |
[root@localhost ~]# cat file1/f1
aaa
a a
a a a
aaa
[root@localhost ~]# cat -A file1/f1
aaa$
a^Ia$
a^Ia^Ia$
$
$
aaa$
[root@localhost ~]# cat -E file1/f1
aaa$
a a$
a a a$
$
$
aaa$
[root@localhost ~]# cat -T file1/f1
aaa
a^Ia
a^Ia^Ia
aaa
[root@localhost ~]# cat -e file1/f1
aaa$
a a$
a a a$
$
$
aaa$
[root@localhost ~]# cat -t file1/f1
aaa
a^Ia
a^Ia^Ia
aaa
[root@localhost ~]# cat -n file1/f1
1 aaa
2 a a
3 a a a
4
5
6 aaa
[root@localhost ~]# cat -sn file1/f1
1 aaa
2 a a
3 a a a
4
5 aaa
Linux文件内容操作(二) 分页查看文件内容 more less
cat命令是将文件的全部内容显示在屏幕上,如果文件内容很多,那么看前面的内容就很麻烦了,这时就可以用more或者less命令来分页查看文件内容。
[root@localhost ~]# more file1/f2
aaa
[root@localhost ~]# less file1/f2
aaa
file1/f2 (END)
# file1/f2 (END)这个是屏幕此外为提示文件末尾,需要按键“q”退出。more命令查看的文件,如果中途退出,也需要按键“q”退出。
more和less命令打开文件后,可以使用以下导航命令查看文件。
命令 | more | less | 动作 |
<空格键> | Y | Y | 前进一页 |
【pgDn】 | N | Y | 前进一页 |
b | Y | Y | 后退一页 |
【PgUp】 | N | Y | 后退一页 |
【DnArrow】 | N | Y | 向前一行 |
【UpArrow】 | N | Y | 后退一行 |
/text | Y | Y | 向前搜索文本 |
?text | N | Y | 向后搜索文本 |
n | Y | Y | 重复上个搜索 |
N | N | Y | 反方向重复上个搜索 |
q | Y | Y | 结束(不能用Ctrl+c) |
h | Y | Y | 帮助 |
由于less支持的命令更多,故建议习惯使用less来查看长文件的内容。
Linux文件内容操作(三) 查看文件内容 head
如果只想查看文件内容前面几行,可以用head。
head命令行选项:
选项 | 含义 |
-num | 显示开始num行(默认设置时10行) |
-q | 不显示文件标题,只显示文件内容 |
[root@localhost ~]# head num
1
2
3
4
5
[root@localhost ~]# head -2 num
1
2
[root@localhost ~]# head -n 2 num
1
2
[root@localhost ~]# head -n +2 num
1
2
#前面三个命令均显示文件开始两行
[root@localhost ~]# head -n -2 num
1
2
3
#这个命令显示最后两行之前的所有行
[root@localhost ~]# head num file1/f1
==> num <==
1
2
3
4
5
==> file1/f1 <==
aaa
a a
a a a
aaa
#同时查看多个文件,会显示文件名
[root@localhost ~]# head -q num file1/f1
1
2
3
4
5
aaa
a a
a a a
aaa
#加上-q选项,不显示文件名
Linux文件内容操作(四) 查看文件内容 tail
如果只想查看文件内容后面几行,可以用tail。
tail命令行选项:
选项 | 含义 |
-num | 显示最后num行(默认设置时10行) |
-q | 不显示文件标题,只显示文件内容 |
-f | 保持文件处于打开状态,不断显示新添加的行 |
[root@localhost ~]# tail num
1
2
3
4
5
[root@localhost ~]# tail -2 num
4
5
[root@localhost ~]# tail -n 2 num
4
5
[root@localhost ~]# tail -n -2 num
4
5
#前面三个命令均显示文件最后两行
[root@localhost ~]# tail -n +2 num
2
3
4
5
#这个命令显示从第二行到最后的所有行
Linux文件内容操作(五) 字数统计 wc
使用wc(word calculate)统计文件字数。
wc命令行选项:
选项 | 含义 |
-c | 估计字符数 |
-w | 估计字数(不包括空白字符,横向制表符和换行符) |
-l | 估计行数 |
[root@localhost ~]# cat -A file1/f1
aaa$
a^Ia$
a^Ia^Ia$
, ,$
^I^I$
aaa$
[root@localhost ~]# wc file1/f1
6 9 25 file1/f1
[root@localhost ~]# wc -l file1/f1
6 file1/f1
[root@localhost ~]# wc -w file1/f1
9 file1/f1
[root@localhost ~]# wc -c file1/f1
25 file1/f1
Linux文件内容操作(六) 格式化文本 fmt
使用fmt(format)命令可以将文本格式化。
fmt命令行选项:
选项 | 含义 |
-w,--width=N,-N | 将文本格式化成N列(默认为75) |
-p,--prefix=STRING | 只对以STRING字符串开始的行进行格式化 |
-u,--uniform-spacing | 强行执行字与字之间一个空格,句子之间两个空格 |
[root@localhost ~]# cat fmttest
#
# Generated automatically from man.conf.in by the
# configure script.
#
# man.conf from man-1.6d
#
# For more information about this file, see the man pages man(1)
# and man.conf(5).
#
# This file is read by man to configure the default manpath (also used
[root@localhost ~]# fmt -40 fmttest
# # Generated automatically from
man.conf.in by the # configure script.
# # man.conf from man-1.6d # # For more
information about this file, see the
man pages man(1) # and man.conf(5). #
# This file is read by man to configure
the default manpath (also used
[root@localhost ~]# fmt -40 -p '#' fmttest
# Generated automatically from
# man.conf.in by the configure script.
# man.conf from man-1.6d
# For more information about this
# file, see the man pages man(1)
# and man.conf(5).
# This file is read by man to configure
# the default manpath (also used
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28536251/viewspace-1431544/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28536251/viewspace-1431544/