目的

熟練使用cat、less、head、tail、diff等命令。
cat命令功能:連接文件和打印文件到標(biāo)準(zhǔn)輸出;
less命令功能:一屏一屏幕的查看文件,且不退出,與more命令相反;
head命令功能:輸出文件的開始部分;
tail命令功能:輸出文件的最后部分;
diff命令功能:一行一行對比多個文件。

前提

可用的centos7系統(tǒng),連接網(wǎng)絡(luò)。

命令介紹

1、cat命令:查看文件全部內(nèi)容

【例1】查看1.sh文件內(nèi)容

[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命令:分頁顯示文件內(nèi)容

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

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

退出按q鍵。

3、head命令:查看文件首部的內(nèi)容

【例3】查看1.sh文件的前3行內(nèi)容

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

this is 111 line

this is 222 line

this is 333 line

4、tail命令:查看文件尾部的內(nèi)容

【例4】查看1.sh文件的后3行內(nèi)容

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

this is 777 line

this is 888 line

this is 999 line

【例5】監(jiān)視查看1.sh文件尾部是否有內(nèi)容增加

[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

文章來源于網(wǎng)絡(luò),侵刪!