Home>Article>Operation and Maintenance> What is the command to view logs in Linux?

What is the command to view logs in Linux?

青灯夜游
青灯夜游 Original
2022-11-23 15:55:45 48262browse

linux commands to view logs: 1. the tail command can real-time changing logs, the syntax is "tail [required parameters] [select [file]"; 2. head previous lines log, "head -n line number file"; 3. cat search for logs near keywords; 4. more display contents of log file in full-screen mode by page; 5. sed search a specific section file; 6. less file. < p>< blockquote>

What is the command to view logs in Linux?< p>

#the operating environment this tutorial: linux7.3 system, dell g3 computer.

as back-end programmer, i deal with linux lot. don’t know how read and am very susceptible ridicule from colleagues interviewers, so it important master one or several methods viewing logs. important.

there are many linux: tail, cat, tac, head, echo, etc. this article only introduces few common methods.

1. tail--view logs< strong>< h2>

command format: tail[required parameters][select parameters][file]< code>
<

  • -f loop reading< li>

  • -q do not processing information<

  • - v display detailed

  • -cc8f01a3f8889dcf657849dd45bc0fc4c number bytes displayed<

  • -n4beafa5ba568da9d3dad3a4091727ee6 rows number<

  • -q, --quiet, --silent never output header given name<

  • -s, --sleep-interval="S" used together -f sleep s seconds between each iteration. ul>

    as follows:

    tail 10 test.log 查询日志尾部最后10行的日志; +10 查询10行之后的所有日志; -fn 循环实时查看最后1000行记录(最常用的)< pre>< div> usually grep, example :< 1000 | grep '关键字'< 

    if amount data queried at time too large, you turn pages view, example: 4700 aa.log |more -1000 可以进行多屏显示(ctrl + f 或者 空格键可以快捷键)< >2, head< opposite tail. head see front.< >head 查询日志文件中的头10行日志; -10 查询日志文件除了最后10行的其他所有日志;<

    for other parameters please refer tail< >3 , - keywords<

    cat displayed continuously on screen first last line<

    the most commonly used:cat filename "keyword"< code><

    other situations:<

    cat app.log -c 5 'keyword'< code>   (display matching string before after)< -b 'keyword'<  (display lines)<

    -a  (display next

    expand knowledge:<

    display entire once file: $ filename< create keyboard: >$cat merge files into one: file1 file2 只能创建新文件,不能编辑已有文件.< pre>

    append another:< textfile1 textfile2<

    clear file:< : >textfile2<

    注意:>< 意思是创建,>>< code>是追加。千万不要弄混了。<

    cat< code>其他参数参考tail<


    < >4、more<

    more< code>命令是一个基于vi< code>编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作。more< code>名单中内置了若干快捷键,常用的有h(获得帮助信息),enter< code>(向下翻滚一行),空格(向下滚动一屏),q< code>(退出命令)。more< code>命令从前向后读取文件,因此在启动时就加载整个文件。<

    该命令一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今己显示的该文件的百分比:–more–(xx%)< code>的语法:more 文件名<

  • enter< 向下n< code>行,需要定义,默认为1行<

  • ctrl f< 向下滚动一屏<

  • 空格键 b< 返回上一屏<

  • ="" 输出当前行的行号<

  • :f< 输出文件名和当前行的行号<

  • v< 调用vi编辑器<

  • !< code>命令 调用shell< code>,并执行命令<

  • q< code>退出more< >5、sed<

    这个命令可以查找日志文件特定的一段 根据时间的一个范围查询,可以按照行号和时间范围查询<

    按照行号< >sed '5,10p' 这样你就可以只查看文件的第5行到第10行。<

    按照时间段< ' 2014-12-17 16:17:20 16:17:36 p' test.log< >6、less<

    less命令在查询日志时,一般流程是这样的< >less log.log shift g 命令到文件尾部 然后输入 ?加上你要搜索的关键字例如 ?1213 按 n 向上查找关键字 shift+n 反向查找关键字< >less与more类似,使用less可以随意浏览文件,而more仅能向前移动,不能向后移动,而且 在查看之前不会加载整个文件。 log2013.log 查看文件 ps -ef ps查看进程信息并通过less分页显示 history 查看命令历史使用记录并通过less分页显示 log2014.log 浏览多个文件<

    常用命令参数:< 浏览多个文件 常用命令参数: -b <缓冲区大小> 设置缓冲区的大小 -g 只标志最后搜索的关键词 -i 忽略搜索时的大小写 -m 显示类似more命令的百分比 -n 显示每行的行号 -o <文件名> 将less 输出的内容在指定文件中保存起来 -q 不使用警告音 -s 显示连续空行为一行 字符串:向下搜索"字符串"的功能 ?字符串:向上搜索"字符串"的功能 n:重复前一个搜索(与 或 ? 有关) n:反向重复前一个搜索(与 b 向后翻一页 h 显示帮助界面 q 退出less 命令<

    linux日志文件说明< var message 系统启动后的信息和错误日志,是red hat linux中最常用的日志之一 secure 与安全相关的日志信息 maillog 与邮件相关的日志信息 cron 与定时任务相关的日志信息 spooler 与uucp和news设备相关的日志信息 boot.log 守护进程启动和停止相关的日志消息 wtmp 该日志文件永久记录每个用户登录、注销及系统的启动、停机的事件<

    相关推荐:《linux视频教程< a>》< p>

    The above is the detailed content of What is the command to view logs in Linux?. For more information, please follow other related articles on the PHP Chinese website!

linux>

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
Previous article:What user is linux nobody? Next article:What user is linux nobody?