Linux基础篇之文件的查看(上)

 命令介绍

这个内容分为两篇来讲,因为占用篇幅较长,主要讲的常用命令有:cat、tac、nl、more、less、head、tail、od

本篇主要讲cat、tac、nl、more、less

下篇讲head、tail、od

cat

cat按顺序读取文件

cat命令选项:

-u 在读取每个字节时,毫不延迟地将字节从输入文件写入标准输出。
-b 对非空白行列出行号
-E 将结尾的断行字符用$展示出来
-n 打印出所有行的行号,包括非空白行
-T 将[tab]键以^I显示出来
-v 列出一些其它的特殊字符
-A 相当于-vET的整合

实战演习:

# 先创建一个test.txt文件
# 文件内容:
# Hello,Welcome to Linux World!
# Learning is my happiness!
# One



# Two


# Three
[root@7e4568607a2a linux]# vim test.txt
# 查看文件内容
[root@7e4568607a2a linux]# cat test.txt
Hello,Welcome to Linux World!
Learning is my happiness!
One



Two


Three
# 查看文件内容并显示非空白行行号
[root@7e4568607a2a linux]# cat -b test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One



     4  Two

     5
     6  Three
# 查看文件并显示换行符$
[root@7e4568607a2a linux]# cat -E test.txt
Hello,Welcome to Linux World!$
Learning is my happiness!$
One$
$
$
$
Two$
$
        $
Three$
# 查看文件并显示所有行行号
[root@7e4568607a2a linux]# cat -n test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One
     4
     5
     6
     7  Two
     8
     9
    10  Three
# 查看文件并显示tab符
[root@7e4568607a2a linux]# cat -T test.txt
Hello,Welcome to Linux World!
Learning is my happiness!
One



Two

 ^I
Three
[root@7e4568607a2a linux]# cat -v test.txt
Hello,Welcome to Linux World!
Learning is my happiness!
One



Two


Three
# 查看文件并显示所有特殊符号
[root@7e4568607a2a linux]# cat -A test.txt
Hello,Welcome to Linux World!$
Learning is my happiness!$
One$
$
$
$
Two$
$
 ^I$
Three$

tac

tac按倒序读取文件(与cat正好顺序相反),注意它只是从最后一行开始读,读到第一行,并不是把所有字符倒过来

[root@7e4568607a2a linux]# tac test.txt
Three


Two



One
Learning is my happiness!
Hello,Welcome to Linux World!

nl

nl--添加行号打印

命令选项解释:

-b 指定行号显示的方式:主要有两种:
    -b a 显示所有行行号,包括空行
    -b t 只显示非空行行号
-n 列出行号表示的方法,有三种:
    -n ln 行号显示在左方
    -n rn 行号显示在每行前的最右方,且不加0
    -n rz 行号显示在每行前的最右方,且加0
-w 行号字段占用的字符数

实战演习

[root@7e4568607a2a linux]# nl -b a test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One
     4
     5
     6
     7  Two
     8
     9
    10  Three
[root@7e4568607a2a linux]# nl -b t test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One



     4  Two

     5
     6  Three
[root@7e4568607a2a linux]# nl -b at test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One
     4
     5
     6
     7  Two
     8
     9
    10  Three
[root@7e4568607a2a linux]# nl -n ln test.txt
1       Hello,Welcome to Linux World!
2       Learning is my happiness!
3       One



4       Two

5
6       Three
[root@7e4568607a2a linux]# nl -n rn test.txt
     1  Hello,Welcome to Linux World!
     2  Learning is my happiness!
     3  One



     4  Two

     5
     6  Three
[root@7e4568607a2a linux]# nl -n rz test.txt
000001  Hello,Welcome to Linux World!
000002  Learning is my happiness!
000003  One



000004  Two

000005
000006  Three
[root@7e4568607a2a linux]# nl -w 3 test.txt
  1     Hello,Welcome to Linux World!
  2     Learning is my happiness!
  3     One



  4     Two

  5
  6     Three
[root@7e4568607a2a linux]# nl -b a -n rz -w 3 test.txt
001     Hello,Welcome to Linux World!
002     Learning is my happiness!
003     One
004
005
006
007     Two
008
009
010     Three

more

more命令是为了解决一次性输出过长的文件内容到屏幕的问题,像cat、tac、nl都会一次性的把文件内容输出到屏幕,但是如果文件内容很多,你得手动的翻到前边去找你想看的内容,more就是为了解决这个问题而存在的。

他只会先展示出一部分内容,然后根据你的需要来展示出你想要的部分,而不是全部。

相关的快捷键:

空格键 向下翻一页
Enter键 向下翻一行
/字符串 查找你想要的字符
:f 显示文件名和目前显示的行数
q 退出more模式

 实战演习:

[root@7e4568607a2a linux]# more test.txt
Hello,Welcome to Linux World!
Learning is my happiness!
One



Two


Three


Four

Five

...内容过多已省略
Six
Six
Six
Six
Six
--More--(72%)

可以看到当我用more打开文件时,文件只会输出一屏的内容,并且在最后还会告诉你展示内容所占的百分比,此时你可以使用刚提到的快捷键来爽一下了,由于这几个快捷键不方便演示,希望小伙伴们能自己实操一下,多多动手

less

less比more更好用,因为more只能向后翻页,向后搜寻,而less既可以向前翻页、也可以向前搜寻

相关的快捷键:

空格键 向下翻一页
page up键 向上翻一页
page down键 向下翻一页
/字符串 向下搜寻字符
?字符串 向上搜寻字符
n 重复前一个搜寻
N 反向的重复前一个搜寻
g 定位到文件的第一行
G 定位到最后一行
q 离开less模式

猜你喜欢

转载自blog.csdn.net/Attitude_do_it/article/details/123114406
今日推荐