for命令不跳过空白行_Java程序员必备:查看日志常用的linux命令
趁周末,復習一下鳥哥的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 行內容范例一
顯示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。原文鏈接:https://blog.csdn.net/weiwenhou/article/details/102774286
總結
以上是生活随笔為你收集整理的for命令不跳过空白行_Java程序员必备:查看日志常用的linux命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 局域网工具
- 下一篇: underscore.js 页面数据渲