前言
趁週末,複習一下鳥哥的linux私房菜,看了文件內容查閱部分,做個筆記,哈哈,希望對你有幫助哦。
cat
cat : 由第一行開始顯示文件所有內容
參數說明
cat [-AbEnTv]
參數:
-A : 相當於-vET 的整合參數,可列出一些特殊字符,而不是空白而已
-b :列出行號,僅針對非空白行做行號顯示,空白行不標行號
-E :將結尾的斷行字符$顯示出來
-n : 打印行號,連同空白行也會有行號,與-b的參數不同
範例demo
範例一:
查看cattest.txt的內容
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat cattest.txt
test cat command
jaywei
#####
範例二:
查看cattest.txt的內容,並且顯示行號
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat -n cattest.txt
1 test cat command
2 jaywei
3
4 #####
適用場景
cat是Concatenate的縮寫,主要功能是將一個文件的內容連續顯示在屏幕上面。
一般文件內容行數較少時,如40行之內,適合用cat。
如果是一般的DOS文件時,就需要特別留意一些奇怪的符號,例如斷行與[Tab]等,要顯示出來,就得加入-a之類的參數了。
tac
tac : 從最後一行開始顯示,可以看出tac是cat的倒寫形式
範例demo
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# tac cattest.txt
#####
jaywei
test cat command
適用場景
tac 的功能跟cat相反,cat是由“第一行到最後一行連續顯示在屏幕上”,而tac則是“由最後一行到第一行反向在屏幕上顯示出來”。
head
head :顯示文件開頭的內容,以行爲單位,默認文件開頭的前10行
參數說明
head [OPTION]... FILE...
-n<行數> 顯示的行數
-q 隱藏文件名
-v 顯示文件名
-c<字節> 顯示字節數
範例demo
顯示 sentinel.conf 文件前12行
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# head -n 12 sentinel.conf
# Example sentinel.conf
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
tail
查看文件的內容,也是以行爲單位,默認10行,從尾往前看。監聽Java動態日誌時,一般跟 -f
參數配合使用。
參數說明
tail [參數] [文件]
-f 循環讀取
-q 不顯示處理信息
-v 顯示詳細的處理信息
-c<數目> 顯示的字節數
-n<行數> 顯示文件的尾部 n 行內容
範例demo
範例一
顯示sentinel.conf文件的最後12行
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -n 12 sentinel.conf
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
範例二
持續檢測sentinel.conf的內容
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -f sentinel.conf
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
<==要等到輸入[ctrl]-c 之後才離開tail 這個命令的檢測
範例三
持續檢測sentinel.conf的內容,並匹配redis關鍵字。匹配關鍵字,一般用 grep
, tail
一般也會跟 grep
搭檔使用。
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -f sentinel.conf | grep redis
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
<==要等到輸入[ctrl]-c 之後才離開tail 這個命令的檢測
適用場景
tial-f
被用來動態監聽Java日誌,開發聯調經常使用到,它一般跟grep
一起搭檔使用。
more
more :一頁一頁地顯示文件內容
參數說明
more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]
參數:
-num :一次顯示的行數
-p :不以捲動的方式顯示每一頁,而是先清除螢幕後再顯示內容
-c : 跟 -p 相似,不同的是先顯示內容再清除其他舊資料
-s : 當遇到有連續兩行以上的空白行,就代換爲一行的空白行
+/pattern : 在每個文檔顯示前搜尋該字串(pattern),然後從該字串之後開始顯示
-u :不顯示下引號 (根據環境變數 TERM 指定的 terminal 而有所不同)
+num : 從第 num 行開始顯示
fileNames :欲顯示內容的文檔,可爲複數個數
常用操作命令
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more sentinel.conf
# Example sentinel.conf
...(中間省略) ...
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
--More--(29%)
仔細看上面的範例,如果more後面接的文件內容行數大於屏幕輸出的行數時,就會出現類似上面的圖示。重點在最後一行,最後一行會顯示出目前顯示的百分比,而且還可以在最後一行輸入一些有用的命令。在more這個程序的運行過程中,你可以使用一些常用的操作命令:
空格鍵 :代表往下翻一頁
Enter : 代表往下滾動一行
/字符串 :代表在這個顯示的內容當中,向下查詢“字符串” 這個關鍵字
:f :立刻顯示出文件名以及目前顯示的行數
q :代表立刻離開more,不再顯示該文件內容
b或[Ctrl]-b :代表往回翻頁,不過這操作只對文件有用,對管道無用。
最常用的是:按 q
離開,按 空格鍵
往下翻頁,按 b
往回翻頁,以及 /字符串
搜索功能,請看以下demo
範例demo
範例一
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此處省略)...
# Before doing that MAKE SURE the instance is protected from the outside
--More--(4%)
分頁查看sentinel.conf文件,一頁展示10行。按下 空格鍵
,可以往下翻頁,
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此處省略)...
# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
--More--(7%)
按下 b
,可以回退到上一頁
# *** IMPORTANT ***
...(此處省略)...
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
--More--(5%)
按下 q
,可以立刻離開more
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]#
範例二
如果想在sentinel.conf文件中,搜尋sentinel關鍵字,可以這樣做
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此處省略)...
# Before doing that MAKE SURE the instance is protected from the outside
/sentinel 輸入/之後,光標就會自動跑到最下面一行等待輸入
如同上面的說明,輸入了/之後,光標就會跑到最下面一行,並且等待你的輸入,你輸入了字符串並按下[Enter]之後,more就會開始向下查詢該字符串,而重複查詢同一個字符串,可以直接按下n即可。最後不想看了,就按下q離開more。
# Before doing that MAKE SURE the instance is protected from the outside
/sentinel
...skipping
# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
/
...skipping
# Example:
#
# sentinel announce-ip 1.2.3.4
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
--More--(23%)
適用場景
more適合日誌比較大的文件查看,可以一頁一頁查看,不會讓前面的數據看不到。
less
less 與 more 類似,但less的用法比起more又更加有彈性。
若使用了less時,就可以使用下、下等按鍵的功能來往前往後翻看文件。
除此之外,在less裏頭可以擁有更多的查詢功能。不止可以向下查詢,也可以向上查詢。
常用操作命令
空格鍵:往下翻動一頁
[pagedown]:向下翻動一頁
[pageup]:向上翻動一頁
Enter : 代表往下滾動一行
y :向前滾動一行
/字符串:向下搜索"字符串"的功能
?字符串:向上搜索"字符串"的功能
n:重複前一個搜索(與 / 或 ? 有關)
N:反向重複前一個搜索(與 / 或 ? 有關)
q: 離開less這個程序
b 向後翻一頁
範例demo
範例一
在sentinel.conf文件中,搜尋sentinel關鍵字,如下
less sentinel.conf
輸入反斜槓 /
,輸入關鍵字 sentinel
,回車
重複前一個搜索,可以按n,反向重複前一個搜索,按N
範例二
Linux 動態查看日誌文件,一般用tail -f ,但是我們也可以用less+ F 實現。
less + file-name + 命令 F = tail -f + file-name
我們經常用tail -f +grep 關鍵字,動態查找報錯的日誌,也可以用less實現。先輸入shirft+g,到達文件結尾
然後輸入 ?
,輸入搜索關鍵字,如 sentinel
,回車,然後按 n
鍵往上搜索,效果是不是也不錯?尤其日誌文件動態刷新太快的時候,奸笑臉。
適用場景
less適合日誌比較大的文件查看,可以一頁一頁查看,並且比more更靈活,也可以動態查看日誌,我一般用它查看Java日誌。
小結
本文總結了查看日誌的幾個linux命令,cat、tac、head、tail、more、less,其中less真的很適合日常開發日誌查看,非常推薦less。
參看與感謝
《鳥哥的linux私房菜》
Linux 命令大全 |菜鳥教程
個人公衆號
如果你是個愛學習的好孩子,可以關注我公衆號,一起學習討論。
如果你覺得本文有哪些不正確的地方,可以評論,也可以關注我公衆號,私聊我,大家一起學習進步哈。