目的

熟练使用cat、less、head、tail、diff等命令。
cat命令功能:连接文件和打印文件到标准输出;
less命令功能:一屏一屏幕的查看文件,且不退出,与more命令相反;
head命令功能:输出文件的开始部分;
tail命令功能:输出文件的最后部分;
diff命令功能:一行一行对比多个文件。

前提

可用的centos7系统,连接网络。

命令介绍

1、cat命令:查看文件全部内容

【例1】查看1.sh文件内容

[root@Magedu ~]# cat 1.sh 

this is 111 line

this is 222 line

this is 333 line

this is 444 line

this is 555 line

this is 666 line

this is 777 line

this is 888 line

this is 999 line

2、less命令:分页显示文件内容

【例2】分页查看/var/log/messages文件,文件最后不退出

[root@Magedu ~]# less /var/log/messages

退出按q键。

3、head命令:查看文件首部的内容

【例3】查看1.sh文件的前3行内容

[root@Magedu ~]# head -3 1.sh 

this is 111 line

this is 222 line

this is 333 line

4、tail命令:查看文件尾部的内容

【例4】查看1.sh文件的后3行内容

[root@Magedu ~]# tail -3 1.sh 

this is 777 line

this is 888 line

this is 999 line

【例5】监视查看1.sh文件尾部是否有内容增加

[root@Magedu ~]# tail -f 1.sh

按ctrl+c键退出。

5、diff命令:比较两文件

【例6】比较1.sh和2.sh两文件的不同

[root@Magedu ~]# diff 1.sh 2.sh

1,8d0

< this is 111 line

< this is 222 line

< this is 333 line

< this is 444 line

< this is 555 line

< this is 666 line

< this is 777 line

< this is 888 line

9a2,9

> this is 888 line

> this is 777 line

> this is 666 line

> this is 555 line

> this is 444 line

> this is 333 line

> this is 222 line

> this is 111 line

文章来源于网络,侵删!