Linux基礎命令二:檢視檔案內容
阿新 • • 發佈:2018-11-12
1.cat
文字檢視。檢視指定檔案的內容
[[email protected] ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
-n 顯示行號
[[email protected] ~]# cat -n /etc/hosts 1 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 2 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
2.tac
按逆序檢視檔案內容
[[email protected] ~]# tac /etc/hosts
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
3.more
分頁檢視檔案內容,支援向後翻。空格(下一頁)、回車(下一行)。
[[email protected] ~]# more /var/log/boot.log
4.less
支援前後翻。空格(下一頁)、方向鍵(上下回翻)、q鍵(退出檢視)
[[email protected] ~]# less /var/log/boot.log
5.head
檢視檔案前多少行,預設為十行。
[[email protected] ~]# head /var/log/boot.log Welcome to CentOS Starting udev: [ OK ] Setting hostname catyuan: [ OK ] Setting up Logical Volume Management: [ OK ] Checking filesystems Checking all file systems. [/sbin/fsck.ext4 (1) -- /] fsck.ext4 -a /dev/vda1 /dev/vda1: clean, 32206/2621440 files, 476339/10485248 blocks [ OK ] Remounting root filesystem in read-write mode: [ OK ]
-n 顯示檔案前幾行內容
[[email protected] ~]# head -n 2 /var/log/boot.log
Welcome to CentOS
Starting udev: [ OK ]
6.tail
檢視檔案後n行,預設為十行
[[email protected] ~]# tail /var/log/boot.log
Cloud-init v. 0.7.6a4 finished at Wed, 07 Nov 2018 10:49:56 +0000. Datasource DataSourceAliYun. Up 12.59 seconds
Starting sshd: [ OK ]
Starting ntpd: [ OK ]
Aegis is running
Starting postfix: [ OK ]
Starting crond: [ OK ]
Starting atd: [ OK ]
aliyun-service stop/waiting
aliyun-service start/running, process 1593
finished
-n 顯示檔案末尾n行內容
[[email protected] ~]# tail -n 2 /var/log/boot.log
aliyun-service start/running, process 1593
finished
-f 檢視檔案後並不退出,滾動顯示追加內容
[[email protected] ~]# tail -f /var/log/boot.log
7.wc
顯示檔案的行、單詞與位元組資訊
[[email protected] ~]# wc /var/log/boot.log
67 570 4566 /var/log/boot.log
-c 顯示檔案的位元組資訊
[[email protected] ~]# wc -c /var/log/boot.log
4566 /var/log/boot.log
-l 顯示檔案行數
[[email protected] ~]# wc -l /var/log/boot.log
67 /var/log/boot.log
-w 顯示檔案單詞個數
[[email protected] ~]# wc -w /var/log/boot.log
570 /var/log/boot.log
8.grep
查詢關鍵詞並輸出有關鍵詞的這一行。
[[email protected] ~]# grep 23 test
23456
12345
[[email protected] ~]# cat test
23456
12345
adcdvcfv
kkkthshss
-i 忽略大小寫
-v 取反匹配
-w 匹配單詞,匹配的是單詞,而不是字母
--color 顯示顏色
9.echo
顯示一行的指定文字
[[email protected] ~]# echo hello
hello
給空白檔案裡新增內容
[[email protected] ~]# echo 123 > test
[[email protected] ~]# cat test
123
給檔案裡追加內容
[[email protected] ~]# echo 456 >> test
[[email protected] ~]# cat test
123
456
注意追加內容時,是兩個>>,否則會覆蓋
[[email protected] ~]# cat test
123
456
[[email protected] ~]# echo 123456 > test
[[email protected] ~]# cat test
123456