Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to view log files in Linux

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to view log files in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1 、 tail

This is one of the most common ways for me to check.

Command format: tail [necessary parameters] [Select parameters] [File]-f Loop read-Q does not display processing information-v displays detailed processing information-number of bytes displayed by c-n shows lines-Q,-quiet,-silent never outputs the first part of the given file name-s,-sleep-interval=S is used with-f to indicate dormancy for S seconds at intervals of each iteration

The usage is as follows:

Tail-n 10 test.log query the last 10 lines of the log; tail-n + 10 test.log query all logs after 10 lines; tail-fn 10 test.log loop to view the last 1000 rows in real time (the most commonly used)

It is also used in conjunction with grep, such as:

Tail-fn 1000 test.log | grep 'keyword'

If the amount of data queried at one time is too large, you can turn the page to view it, for example:

Tail-n 4700 aa.log | more-1000 can be displayed on multiple screens (ctrl + f or spacebar can be a shortcut key)

2 、 head

As opposed to tail, head looks at the number of previous lines of log.

Head-n 10 test.log queries the first 10 lines of logs in the log file; head-n-10 test.log queries all logs in the log file except the last 10 lines

Other parameters of head refer to tail

3 、 cat

Cat is displayed continuously on the screen from the first line to the last line.

Display the entire file at once:

$cat filename

Create a file from the keyboard:

$cat > filename

Merge several files into one file:

$cat file1 file2 > file can only create new files, not edit existing files.

Append the contents of one log file to another:

$cat-n textfile1 > textfile2

Clear a log file:

$cat: > textfile2

Note: > means create, > > is append. Don't get mixed up.

Other parameters of cat refer to tail

4 、 more

The more command is a text filter based on the vi editor that displays the contents of a text file by page in a full-screen manner and supports keyword positioning in vi. There are several keyboard shortcuts built into the more list, such as H (get help information), Enter (scroll down one line), space (scroll down one screen), and Q (exit command). The more command reads the file back and forth, so the entire file is loaded at startup.

The command displays one screen of text at a time, stops after the screen is full, and displays a message at the bottom of the screen giving the percentage of the file that has been displayed so far:-More- (XX%)

More syntax: more file name Enter down n lines, need to be defined, the default is 1 line Ctrl f scroll down a screen space bar scroll down a screen Ctrl b return the previous screen = output the current line number: F output file name and current line number v call vi editor! Command to call Shell, and execute the command Q to exit more5, sed

This command can find a specific section of the log file. It can be queried by line number and time range according to a range of time.

By line number

Sed-n'5 filename 10p'so you can only look at lines 5 through 10 of the file.

According to the time period

Sed-n'/ 2014-12-17 16 provision17 less 17 less 17 sed-n'/ 17-12-17 16 purge 17 purl 20 Unix Legendre 2014-12-17 16 purse 17 purse 36 gambler p'max

When the less command queries the log, the general process is as follows

Less log.log

Shift + G command to the end of the file and enter? Plus the keywords you want to search for, for example? 1213

Press n to find the keyword up

The shift+n reverse lookup keyword less is similar to more, where you can browse files at will with less, while more can only move forward, not backward, and less does not load the entire file before viewing it. Less log2013.log view file ps-ef | less ps view process information and display history via less paging | less view command history and use less paging display less log2013.log log2014.log to browse multiple files

Common command parameters:

Less is similar to more in that you can browse files at will with less, while more can only move forward, not backward, and less does not load the entire file before viewing it. Less log2013.log view file ps-ef | less ps view process information and display history via less paging | less view command history usage record and display less log2013.log log2014.log browsing multiple files via less paging commonly used command parameters:-b sets the size of the buffer-g only marks the last search keyword-I ignore the case of the search-m shows the percentage similar to the more command Than-N displays the line number of each line-o saves the less output in the specified file-Q does not use the warning tone-s displays a continuous empty behavior line / string: the ability to search down for "string"? String: the ability to search up for "string" n: repeat the previous search (and / or? About) N: reverse repeat the previous search (and / or? About) b turn back one page h to show the help interface Q exit less command

Generally, I check the log and apply other commands.

History / / all historical records

History | grep XXX / / Records that contain certain instructions in the history

History | more / / View records by page

History-c / / clear all history records

!! Repeat the previous command

After querying the record, select:! 323

Linux log file description

/ var/log/message system information and error log after startup Is one of the most commonly used logs in Red Hat Linux / var/log/secure security-related log information / var/log/maillog mail-related log information / var/log/cron log information related to scheduled tasks / var/log/spooler log information related to UUCP and news devices / var/log/boot.log daemon start and stop related log messages / var/log/wtmp that day Log files permanently record each user login, Events of logout and system startup and shutdown

The above is all the contents of the article "how to View Log Files in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report