人妻少妇乱子伦精品_日韩人妻潮喷视频网站_日本最新最全无码不卡免费_日韩AV无码中文

當(dāng)前位置: 首頁 > 科技新聞 >

Java程序員必備:查看日志常用的linux命令

時(shí)間:2019-12-02 00:27來源:網(wǎng)絡(luò)整理 瀏覽:
推薦閱讀:秋招Java面試大綱:Java+并發(fā)+spring+數(shù)據(jù)庫+Redis+JVM+Netty等MySQL復(fù)習(xí):20道常見面試題(含答

推薦閱讀:

秋招Java面試大綱:Java+并發(fā)+spring+數(shù)據(jù)庫+Redis+JVM+Netty等

MySQL復(fù)習(xí):20道常見面試題(含答案)+21條MySQL性能調(diào)優(yōu)經(jīng)驗(yàn)

01 前言

趁周末,復(fù)習(xí)一下鳥哥的linux私房菜,看到文件內(nèi)容查閱部分,做個(gè)筆記,哈哈,希望對(duì)你有幫助哦,另外有需要“鳥哥的linux私房菜(PDF)”以及“JAVA核心知識(shí)點(diǎn)整理PDF”的朋友可以私信【資料】免費(fèi)領(lǐng)取~

02 cat

cat : 由第一行開始顯示文件所有內(nèi)容

2.1 參數(shù)說明

cat [-AbEnTv]
參數(shù):
-A : 相當(dāng)于-vET 的整合參數(shù),可列出一些特殊字符,而不是空白而已
-b :列出行號(hào),僅針對(duì)非空白行做行號(hào)顯示,空白行不標(biāo)行號(hào)
-E :將結(jié)尾的斷行字符$顯示出來
-n : 打印行號(hào),連同空白行也會(huì)有行號(hào),與-b的參數(shù)不同

2.2 范例demo

范例一:

查看cattest.txt的內(nèi)容

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat cattest.txt 
test cat command
jaywei
#####

范例二:

查看cattest.txt的內(nèi)容,并且顯示行號(hào)

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat -n cattest.txt 
1test cat command
2jaywei
3
4#####

2.3 適用場景

cat是Concatenate的縮寫,主要功能是將一個(gè)文件的內(nèi)容連續(xù)顯示在屏幕上面。一般文件內(nèi)容行數(shù)較少時(shí),如40行之內(nèi),適合用cat。如果是一般的DOS文件時(shí),就需要特別留意一些奇怪的符號(hào),例如斷行與[Tab]等,要顯示出來,就得加入-a之類的參數(shù)了。03 tac

tac : 從最后一行開始顯示,可以看出tac是cat的倒寫形式

3.1 范例demo

[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# tac cattest.txt 
#####
jaywei
test cat command

3.2 適用場景

tac 的功能跟cat相反,cat是由“第一行到最后一行連續(xù)顯示在屏幕上”,而tac則是“由最后一行到第一行反向在屏幕上顯示出來”。04 head

head :顯示文件開頭的內(nèi)容,以行為單位,默認(rèn)文件開頭的前10行

4.1 參數(shù)說明

head [OPTION]... FILE...
-n<行數(shù)> 顯示的行數(shù)
-q 隱藏文件名
-v 顯示文件名
-c<字節(jié)> 顯示字節(jié)數(shù)

4.2 范例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.
#
05 tail

查看文件的內(nèi)容,也是以行為單位,默認(rèn)10行,從尾往前看。監(jiān)聽Java動(dòng)態(tài)日志時(shí),一般跟-f參數(shù)配合使用。

5.1 參數(shù)說明

tail [參數(shù)] [文件] 
-f 循環(huán)讀取
-q 不顯示處理信息
-v 顯示詳細(xì)的處理信息
-c<數(shù)目> 顯示的字節(jié)數(shù)
-n<行數(shù)> 顯示文件的尾部 n 行內(nèi)容

5.2 范例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

范例二

持續(xù)檢測sentinel.conf的內(nèi)容

[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 這個(gè)命令的檢測

范例三

持續(xù)檢測sentinel.conf的內(nèi)容,并匹配redis關(guān)鍵字。匹配關(guān)鍵字,一般用grep ,tail 一般也會(huì)跟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 這個(gè)命令的檢測

5.3 適用場景

tial -f 被用來動(dòng)態(tài)監(jiān)聽Java日志,開發(fā)聯(lián)調(diào)經(jīng)常使用到,它一般跟grep 一起搭檔使用。06 more

more :一頁一頁地顯示文件內(nèi)容

6.1 參數(shù)說明

-num :一次顯示的行數(shù)
-p :不以卷動(dòng)的方式顯示每一頁,而是先清除螢?zāi)缓笤亠@示內(nèi)容
-c : 跟 -p 相似,不同的是先顯示內(nèi)容再清除其他舊資料
-s : 當(dāng)遇到有連續(xù)兩行以上的空白行,就代換為一行的空白行
+/pattern : 在每個(gè)文檔顯示前搜尋該字串(pattern),然后從該字串之后開始顯示
-u :不顯示下引號(hào) (根據(jù)環(huán)境變數(shù) TERM 指定的 terminal 而有所不同)
+num : 從第 num 行開始顯示
fileNames :欲顯示內(nèi)容的文檔,可為復(fù)數(shù)個(gè)數(shù)

6.2 常用操作命令

[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%)

仔細(xì)看上面的范例,如果more后面接的文件內(nèi)容行數(shù)大于屏幕輸出的行數(shù)時(shí),就會(huì)出現(xiàn)類似上面的圖示。重點(diǎn)在最后一行,最后一行會(huì)顯示出目前顯示的百分比,而且還可以在最后一行輸入一些有用的命令。在more這個(gè)程序的運(yùn)行過程中,你可以使用一些常用的操作命令:

空格鍵 :代表往下翻一頁Enter : 代表往下滾動(dòng)一行/字符串 :代表在這個(gè)顯示的內(nèi)容當(dāng)中,向下查詢“字符串” 這個(gè)關(guān)鍵字:f :立刻顯示出文件名以及目前顯示的行數(shù)q :代表立刻離開more,不再顯示該文件內(nèi)容b或[Ctrl]-b :代表往回翻頁,不過這操作只對(duì)文件有用,對(duì)管道無用。

最常用的是:按q離開,按空格鍵往下翻頁,按b往回翻頁,以及/字符串搜索功能,請(qǐng)看以下demo

6.3 范例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關(guān)鍵字,可以這樣做

[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 輸入/之后,光標(biāo)就會(huì)自動(dòng)跑到最下面一行等待輸入

如同上面的說明,輸入了/之后,光標(biāo)就會(huì)跑到最下面一行,并且等待你的輸入,你輸入了字符串并按下[Enter]之后,more就會(huì)開始向下查詢?cè)撟址?,而重?fù)查詢同一個(gè)字符串,可以直接按下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%)

6.4 適用場景

more使用日志比較大的文件查看,可以一頁一頁查看,不會(huì)讓前面的數(shù)據(jù)看不到。07 less

less 與 more 類似,但less的用法比起more又更加有彈性。

若使用了less時(shí),就可以使用下、下等按鍵的功能來往前往后翻看文件。除此之外,在less里頭可以擁有更多的查詢功能。不止可以向下查詢,也可以向上查詢。

7.1 常用操作命令

空格鍵:往下翻動(dòng)一頁[pagedown]: 向下翻動(dòng)一頁[pageup]: 向上翻動(dòng)一頁Enter : 代表往下滾動(dòng)一行y :向前滾動(dòng)一行/字符串:向下搜索"字符串"的功能?字符串:向上搜索"字符串"的功能n:重復(fù)前一個(gè)搜索(與 / 或 ? 有關(guān))N:反向重復(fù)前一個(gè)搜索(與 / 或 ? 有關(guān))q: 離開less這個(gè)程序b 向后翻一頁

7.2 范例demo

范例一

在sentinel.conf文件中,搜尋sentinel關(guān)鍵字,如下

less sentinel.conf 
輸入反斜杠/,輸入關(guān)鍵字sentinel,回車

重復(fù)前一個(gè)搜索,可以按n,反向重復(fù)前一個(gè)搜索,按N

范例二

Linux 動(dòng)態(tài)查看日志文件,一般用tail -f ,但是我們也可以用less+ F 實(shí)現(xiàn)。

less + file-name + 命令 F = tail -f + file-name 

我們經(jīng)常用tail -f +grep 關(guān)鍵字,動(dòng)態(tài)查找報(bào)錯(cuò)的日志,也可以用less實(shí)現(xiàn)。先輸入shirft+g,到達(dá)文件結(jié)尾

然后輸入?,輸入搜索關(guān)鍵字,如sentinel,回車,然后按n鍵往上搜索,效果是不是也不錯(cuò)?尤其日志文件動(dòng)態(tài)刷新太快的時(shí)候,奸笑臉。

7.3 適用場景

less適合日志比較大的文件查看,可以一頁一頁查看,并且比more更靈活,也可以動(dòng)態(tài)查看日志,我一般用它查看Java日志。08 小結(jié)

本文總結(jié)了查看文件日志的幾個(gè)linux命令,cat、tac、head、tail、more、less,其中l(wèi)ess真的很適合日常開發(fā)日志查看,非常推薦less。

原文鏈接:https://juejin.im/post/5db30ef0e51d452a41010505

推薦內(nèi)容