Summarize 6 log viewing methods of Linux

  • 2021-07-24 11:57:14
  • OfStack

As a back-end programmer, there are many places to deal with Linux, and he can't read Linux logs, so he is very vulnerable to ridicule from colleagues and interviewers, so it is very important to master one or several methods of viewing logs.

There are many commands for Linux to view logs: tail, cat, tac, head, echo, etc. This article only introduces a few commonly used methods.

1. tail

This is one of the most common ways I view it

Command Format: tail [Required Parameters] [Select Parameters] [File]
-f Loop Read
-q does not display processing information
-v displays detailed processing information
-c < Number > Number of bytes displayed
-n < Number of rows > Number of rows displayed
-q,--quiet,--silent never output headers that give file names
-s,--sleep-interval=S is combined with-f to mean sleeping S seconds at intervals of each iteration

The usage is as follows:

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

1 will also be used in conjunction with grep, for example:

tail-fn 1000 test. log grep 'Keyword'

If the amount of data in a primary query is too large, you can turn pages for viewing, for example:

tail-n 4700 aa. log more-1000 can be displayed on multiple screens (ctrl + f or the space bar can be shortcut keys)

2. head

head, which is the opposite of tail, looks at how many lines of log are there before

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

Refer to tail for other parameters of head

3. cat

cat is continuously displayed on the screen from the first line to the last line

Display the entire file once:


 $ cat filename

Create 1 file from keyboard:


$cat > filename


Merge several files into one file:


$cat file1 file2 > file  You can only create new files , You cannot edit an existing file .


Append the contents of one log file to another:


$cat -n textfile1 > textfile2

Empty 1 log file:


$cat : >textfile2


Note: > It means to create, > > Is appended. Don't get confused.

Refer to tail for other parameters of cat

4. more

The more command is a text filter based on the vi editor, which displays the contents of text files page by page in a full-screen manner and supports keyword positioning operations in vi. There are several shortcut keys built into the more list, such as H (get help information), Enter (scroll down 1 line), space (scroll down 1 screen) and Q (exit command). The more command reads the file from front to back, so it loads the entire file at startup.

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

Syntax for more: more Filename Enter down n line, need to be defined, default is 1 line Ctrl f Scroll Down 1 Screen The space bar scrolls down 1 screen Ctrl b Return to the first screen = Output the line number of the current line : f Output file name and line number of current line v calls the vi editor ! Command calls Shell and executes the command q exits more

5. sed

This command can find a specific segment of the log file, query according to a range of time, and query according to the line number and time range

By line number


sed -n '5,10p' filename  So that you can only view the first section of the file 5 Row to Number 1 10 OK. 

By time period


sed -n '/2014-12-17 16:17:20/,/2014-12-17 16:17:36/p' test.log

6. less

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

less log.log

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

Press n to look up keywords

shift+n Reverse Lookup Keyword
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 whole file before viewing.
less log 2013. log View Files
ps-ef less ps View the process information and display it through less pagination
history less View command history usage and display through less paging
less log 2013. log log 2014. log 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 whole file before viewing.
less log 2013. log View Files
ps-ef less ps View process information and display it through less pagination
history less View command history usage and display through less pagination
less log 2013. log log 2014. log Browse multiple files
Common command parameters:
-b < Buffer size > Set the size of the buffer
-g only marks the last searched keyword
-i ignores case when searching
-m shows the percentage of commands similar to more
-N displays the line number of each line
-o < Filename > Save the contents of less output in the specified file
-Q does not use warning tones
-s shows continuous empty lines for 1 line
/String: The ability to search down for "strings"
? String: The ability to search up for "strings"
n: Repeat the first 1 searches (related to/or?)
N: Repeat the first 1 searches in reverse (related to/or?)
b Turn Back 1 Page
h Displays Help Interface
q Exit less Command

1. I check the log to cooperate with other commands applied

history//All History

history grep XXX//History contains records of certain instructions

history more//View records in pages

history-c//Empty all history records

! ! Repeat the last command

After inquiring the records, select:! 323

linux Log File Description

/var/log/message information and error log after system startup is one of the most commonly used logs in Red Hat Linux
/var/log/secure Security-related log information
/var/log/maillog Message-related log information
/var/log/cron Log information related to timed 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 This log file permanently records the login and logout events of each user and the startup and downtime of the system

The above is all the contents of this site, hoping to help everyone.


Related articles: