Home  >  Article  >  Operation and Maintenance  >  Detailed explanation of what commands are used to view logs in Linux?

Detailed explanation of what commands are used to view logs in Linux?

零下一度
零下一度Original
2017-06-23 11:07:513984browse


This article mainly introduces the commands for viewing logs in Linux. I hope it will be helpful to friends in need!

Detailed explanation of what commands are used to view logs in Linux?

First method: View the log of real-time changes (more memory-consuming)

Most commonly used:

##

tail -f filename  (默认最后10行,相当于增加参数 -n 10)
Ctrl+c 是退出tail命令

Other situations:

##

tail -n 20 filename  (显示filename最后20行)
tail -n +5 filename  (从第5行开始显示文件)

Second type: Search logs near keywords

Most commonly used:

cat -n filename |grep "关键字"

Other situations:  

cat filename | grep -C 5 '关键字'   (显示日志里匹配字串那行以及前后5行)
cat filename | grep -B 5 '关键字'   (显示匹配字串及前5行)
cat filename | grep -A 5 '关键字'   (显示匹配字串及后5行)

Third method: Enter the editor to search: vi(vim)

1. Enter vim editing mode: vim filename

2. Enter "/keyword" and press enter to find

##3 , search for the next one, press "n" to exit: After pressing the ESC key, and then entering the : sign, vi will wait for us to enter the command at the bottom of the screen

wq! Save and exit;

q! Exit without saving;

Other situations :

/关键字
    注:正向查找,按n键把光标移动到下一个符合条件的地方
?关键字
    注:反向查找,按shift+n 键,把光标移动到下一个符合条件的

The above is the detailed content of Detailed explanation of what commands are used to view logs in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn