加入收藏 | 设为首页 | 会员中心 | 我要投稿 济南站长网 (https://www.0531zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Linux > 正文

Common Linux log files name and usage--reference

发布时间:2021-01-24 03:29:30 所属栏目:Linux 来源:网络整理
导读:副标题#e# div id="post-body-7256318887016413630" class="post-single-body post-body" div dir="ltr"If you spend lot of time in Linux environment,it is essential that you know where the log files are located,and what is contained in each and

In the following example,you can view the content of var/log/cron from line number 101 to 110.M – Starting line numberN – Ending line number<div class="block-panel">
Syntax: sed -n M,Np FILENAME$ sed -n 101,110p /var/log/cron

Example 2: Display first N lines of a file using head commandThis example displays only first 15 lines of /var/log/maillog file. Change 15 to 10 to display the first 10 lines of a log file.?<div class="block-panel">
Syntax:?head -n N FILENAME$ head -n 15 /var/log/maillog

Example 3: Ignore last N lines of a file using head commandThis example shows how to ignore the last N lines,and show only the remaining lines from the top of file. The following example will display all the lines of the /var/log/secure except the last 250 lines.<div class="block-panel">
Syntax:?head -n -N FILENAME$ head -n -250 /var/log/secure

Example 4: Display last N lines of the file using tail commandThis example displays only last 50 lines of /var/log/messages file. Change 50 to 100 to display the last 100 lines of the log file.<div class="block-panel">
Syntax:?tail -n N FILENAME$ tail -n 50 /var/log/messages

Example 5: Ignore first N-1 lines of the file using tail commandThis example shows how to ignore the first N-1 lines and show only the remaining of the lines. The following example ignores the 1st four lines of the /etc/xinetd.conf,which contains only the comments.<div class="block-panel">
Syntax:?tail -n +N FILENAME$ tail -n +5 /etc/xinetd.conf?defaults{instances = 60log_type = SYSLOG authprivlog_on_success = HOST PIDlog_on_failure = HOSTcps = 25 30}includedir /etc/xinetd.d

Example 6: View growing log file in real time using tail commandThis is probably one of the most used command by sysadmins.To view a growing log file and see only the newer contents use tail -f as shown below.The following example shows the content of the /var/log/syslog command in real-time.<div class="block-panel">
Syntax:?tail -f FILENAME$ tail -f /var/log/syslog

Example 7: Display specific lines (based on line number) of a file using head and tail commandThe example below will display line numbers 101 – 110 of /var/log/anaconda.log fileM – Starting line numberN – Ending line number<div class="block-panel">
Syntax:?cat file | tail -n +N | head -n (M-N+1)$ cat /var/log/anaconda.log | tail -n +101 | head -n10

cat : prints the whole file to the stdout.tail -n +101 : ignores lines upto the given line number,and then start printing lines after the given number.head -n 10 : prints the first 10 line,that is 101 to 110 and ignores the remaining lines.Example 8: Display lines matching a pattern,and few lines following the match.The following example displays the line that matches “Initializing CPU” from the /var/log/dmesg and 5 lines immediately after this match.<div class="block-panel">
# grep "Initializing CPU#1" /var/log/dmesgInitializing CPU#1[Note: The above shows only the line matching the pattern]# grep -A 5 "Initializing CPU#1" dmesgInitializing CPU#1Calibrating delay using timer specific routine.. 3989.96 BogoMIPS (lpj=1994982)CPU: After generic identify,caps: bfebfbff 20100000 00000000 00000000CPU: After vendor identify,caps: bfebfbff 20100000 00000000 00000000monitor/mwait feature present.CPU: L1 I cache: 32K,L1 D cache: 32K

[Note: The above shows the line and 5 lines after the pattern matching]Example 9: Displaying specific bytes from a file.The following example explains how to display either the top 40 or the last 30 bytes of a file.<div class="block-panel">Display first 40 bytes from syslog.$ head -c40 /var/log/syslog

<div class="block-panel">Display last 30 bytes from syslog.$ tail -c30 /var/log/syslog

(编辑:济南站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读