linux搜索log文件的内容
日志一般是记载每天所做的工作。在计算机科学中,日志是指服务器等电脑设备或软件的运作记录(Server log)。在电脑设备和软件出现问题时,日志是我们在排查问题的一个重要依据。查询日志是用户记录从客户端收到所有数据库或操作系统的查询,同时也包括了每一个客户端的链接和断开链接。
如今各种花式日志系统大行其道,但在此同时也不要忘记linux查看日志的基础功能,今天就讲讲linux查看日志的常用基础功能
1.1 less -N 日志文件名.log
less -N test.log
然后输入"/context"搜索context关键字
点击键盘↑ ↓可以滚动,点击 N 可以查看上一个,n可以查看下一个
1.2 less详解
- SUMMARY OF LESS COMMANDS
- Commands marked with * may be preceded by a number, N.
- Notes in parentheses indicate the behavior if N is given.
- A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.
- h H Display this help.
- q :q Q :Q ZZ Exit.
- ---------------------------------------------------------------------------
- MOVING
- e ^E j ^N CR * Forward one line (or N lines).
- y ^Y k ^K ^P * Backward one line (or N lines).
- f ^F ^V SPACE * Forward one window (or N lines).
- b ^B ESC-v * Backward one window (or N lines).
- z * Forward one window (and set window to N).
- w * Backward one window (and set window to N).
- ESC-SPACE * Forward one window, but don't stop at end-of-file.
- d ^D * Forward one half-window (and set half-window to N).
- u ^U * Backward one half-window (and set half-window to N).
- ESC-) RightArrow * Left one half screen width (or N positions).
- ESC-( LeftArrow * Right one half screen width (or N positions).
- F Forward forever; like "tail -f".
- r ^R ^L Repaint screen.
- R Repaint screen, discarding buffered input.
- ---------------------------------------------------
- Default "window" is the screen height.
- Default "half-window" is half of the screen height.
- ---------------------------------------------------------------------------
- SEARCHING
- /pattern * Search forward for (N-th) matching line.
- ?pattern * Search backward for (N-th) matching line.
- n * Repeat previous search (for N-th occurrence).
- N * Repeat previous search in reverse direction.
- ESC-n * Repeat previous search, spanning files.
- ESC-N * Repeat previous search, reverse dir. & spanning files.
- ESC-u Undo (toggle) search highlighting.
- &pattern * Display only matching lines
- ---------------------------------------------------
- A search pattern may be preceded by one or more of:
- ^N or ! Search for NON-matching lines.
- ^E or * Search multiple files (pass thru END OF FILE).
- ^F or @ Start search at FIRST file (for /) or last file (for ?).
- ^K Highlight matches, but don't move (KEEP position).
- ^R Don't use REGULAR EXPRESSIONS.
- ---------------------------------------------------------------------------
- HELP -- Press RETURN for more, or q when done
2.1 统计文件或者文本中包含匹配字符串的行数 -c 选项
grep -c "context" daily.log
2.2 使用正则表达式 -E 选项
grep -E "[1-9]+" daily.log
搜索以数字2开头的行:
2.3 使用正则表达式 -E 选项
grep -l "text" file1 file2 file3...
2.4 grep详解
- 用法: grep [选项]... PATTERN [FILE]...
- 在每个 FILE 或是标准输入中查找 PATTERN。
- 默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
- 例如: grep -i 'hello world' menu.h main.c
- 正则表达式选择与解释:
- -E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
- -F, --fixed-strings PATTERN 是一组由断行符分隔的定长字符串。
- -G, --basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE)
- -P, --perl-regexp PATTERN 是一个 Perl 正则表达式
- -e, --regexp=PATTERN 用 PATTERN 来进行匹配操作
- -f, --file=FILE 从 FILE 中取得 PATTERN
- -i, --ignore-case 忽略大小写
- -w, --word-regexp 强制 PATTERN 仅完全匹配字词
- -x, --line-regexp 强制 PATTERN 仅完全匹配一行
- -z, --null-data 一个 0 字节的数据行,但不是空行
- Miscellaneous:
- -s, --no-messages suppress error messages
- -v, --invert-match select non-matching lines
- -V, --version display version information and exit
- --help display this help text and exit
- 输出控制:
- -m, --max-count=NUM NUM 次匹配后停止
- -b, --byte-offset 输出的同时打印字节偏移
- -n, --line-number 输出的同时打印行号
- --line-buffered 每行输出清空
- -H, --with-filename 为每一匹配项打印文件名
- -h, --no-filename 输出时不显示文件名前缀
- --label=LABEL 将LABEL 作为标准输入文件名前缀
- -o, --only-matching show only the part of a line matching PATTERN
- -q, --quiet, --silent suppress all normal output
- --binary-files=TYPE assume that binary files are TYPE;
- TYPE is 'binary', 'text', or 'without-match'
- -a, --text equivalent to --binary-files=text
- -I equivalent to --binary-files=without-match
- -d, --directories=ACTION how to handle directories;
- ACTION is 'read', 'recurse', or 'skip'
- -D, --devices=ACTION how to handle devices, FIFOs and sockets;
- ACTION is 'read' or 'skip'
- -r, --recursive like --directories=recurse
- -R, --dereference-recursive
- likewise, but follow all symlinks
- --include=FILE_PATTERN
- search only files that match FILE_PATTERN
- --exclude=FILE_PATTERN
- skip files and directories matching FILE_PATTERN
- --exclude-from=FILE skip files matching any file pattern from FILE
- --exclude-dir=PATTERN directories that match PATTERN will be skipped.
- -L, --files-without-match print only names of FILEs containing no match
- -l, --files-with-matches print only names of FILEs containing matches
- -c, --count print only a count of matching lines per FILE
- -T, --initial-tab make tabs line up (if needed)
- -Z, --null print 0 byte after FILE name
- 文件控制:
- -B, --before-context=NUM 打印以文本起始的NUM 行
- -A, --after-context=NUM 打印以文本结尾的NUM 行
- -C, --context=NUM 打印输出文本NUM 行
- -NUM same as --context=NUM
- --group-separator=SEP use SEP as a group separator
- --no-group-separator use empty string as a group separator
- --color[=WHEN],
- --colour[=WHEN] use markers to highlight the matching strings;
- WHEN is 'always', 'never', or 'auto'
- -U, --binary do not strip CR characters at EOL (MSDOS/Windows)
- -u, --unix-byte-offsets report offsets as if CRs were not there
- (MSDOS/Windows)
- ‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
- 直接使用‘egrep’或是‘fgrep’均已不可行了。
- 若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。
- 如果少于两个FILE 参数,就要默认使用-h 参数。
- 如果有任意行被匹配,那退出状态为 0,否则为 1;
- 如果有错误产生,且未指定 -q 参数,那退出状态为 2。
- 请将错误报告给: bug-grep@gnu.org
- GNU Grep 主页: <http://www.gnu.org/software/grep/>
- GNU 软件的通用帮助: <http://www.gnu.org/gethelp/>
根据 关键词 查看日志 并返回关键词所在行:
grep -i "test" ./test.log
返回test.log中包含test的所有行(-i忽略大小写)
3.1 查看日志前n行
cat test.log | head -n 5
test.log为文件名,5为行数。
3.2 查看日志尾n行
cat test.log | tail -n 5
3.3 根据关键词查看日志
cat daily.log | grep "context"
3.4 cat详解
- 用法:cat [选项]... [文件]...
- 将[文件]或标准输入组合输出到标准输出。
- -A, --show-all 等于-vET
- -b, --number-nonblank 对非空输出行编号
- -e 等于-vE
- -E, --show-ends 在每行结束处显示"$"
- -n, --number 对输出的所有行编号
- -s, --squeeze-blank 不输出多行空行
- -t 与-vT 等价
- -T, --show-tabs 将跳格字符显示为^I
- -u (被忽略)
- -v, --show-nonprinting 使用^ 和M- 引用,除了LFD和 TAB 之外
- --help 显示此帮助信息并退出
- --version 显示版本信息并退出
- 如果没有指定文件,或者文件为"-",则从标准输入读取。
- 示例:
- cat f - g 先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
- cat 将标准输入的内容复制到标准输出。
- GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
- 请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误
- 要获取完整文档,请运行:info coreutils 'cat invocation'
- [toutou@localhost front]$
4.1 tail常见用法
tail -f test.log
可以动态的查看服务器运行状态的日志
head -n 5 test.log
显示top 5行
tail -n 5 test.log
显示last 5行
tail -n +5 test.log
从第5行开始显示,显示第5行以后的
4.2 tail详解
- 用法:tail [选项]... [文件]...
- Print the last 10 lines of each FILE to standard output.
- With more than one FILE, precede each with a header giving the file name.
- With no FILE, or when FILE is -, read standard input.
- Mandatory arguments to long options are mandatory for short options too.
- -c, --bytes=K output the last K bytes; or use -c +K to output
- bytes starting with the Kth of each file
- -f, --follow[={name|descriptor}]
- output appended data as the file grows;
- an absent option argument means 'descriptor'
- -F same as --follow=name --retry
- -n, --lines=K output the last K lines, instead of the last 10;
- or use -n +K to output starting with the Kth
- --max-unchanged-stats=N
- with --follow=name, reopen a FILE which has not
- changed size after N (default 5) iterations
- to see if it has been unlinked or renamed
- (this is the usual case of rotated log files);
- with inotify, this option is rarely useful
- --pid=PID with -f, terminate after process ID, PID dies
- -q, --quiet, --silent never output headers giving file names
- --retry keep trying to open a file if it is inaccessible
- -s, --sleep-interval=N with -f, sleep for approximately N seconds
- (default 1.0) between iterations;
- with inotify and --pid=P, check process P at
- least once every N seconds
- -v, --verbose always output headers giving file names
- --help 显示此帮助信息并退出
- --version 显示版本信息并退出
- If the first character of K (the number of bytes or lines) is a '+',
- print beginning with the Kth item from the start of each file, otherwise,
- print the last K items in the file. K may have a multiplier suffix:
- b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
- GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
- 如果您希望即时追查一个文件的有效名称而非描述内容(例如循环日志),默认
- 的程序动作并不如您所愿。在这种场合可以使用--follow=name 选项,它会使
- tail 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。
- GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
- 请向<http://translationproject.org/team/zh_CN.html> 报告tail 的翻译错误
- 要获取完整文档,请运行:info coreutils 'tail invocation'
5.1 查看文件的第5行到第15行
sed -n '5,15p' daily.log
5.2 sed详解
- sed不与初始化文件打交道,它操作的只是一个拷贝,然后所有的改动如果没有重定向到一个文件,将输出到屏幕。
- sed是一种很重要的文本过滤工具,使用一行命令或者使用管道与grep与awk相结合。是一种非交互性文本流编辑。
- (1)调用sed的三种方式
- 使用sed命令行格式为:sed [options] sed命令 输入文件
- 使用sed脚本文件格式为:sed[options] -f sed脚本文件 输入文件
- sed脚本文件[options] 输入文件
- --不管是使用shell命令行方式或脚本文件方式,如果没有指定输入文件,sed从标准输入中接受输入,一般是键盘或重定向结果。
- (2)sed 命令的options如下
- -n:不打印
- -c:下一命令是编辑命令
- -f:如果正在调用sed脚本文件
- (3)sed在文件中查询文本的方式
- --使用行号,可以是一个简单的数字,或是一个行号的范围
- --使用正则表达式
- (4)读取文本的方式
- x x为一行号
- x,y 表示行号范围从x到y
- /pattern/ 查询包含模式的行
- /pattern/pattern/ 查询包含两个模式的行
- pattern/,x 在给定的行号上查询包含模式的行
- x,/pattern/ 通过行号和模式查询匹配行
- x,y! 查询不包含指定行号x和y的行
- (5)基本sed编辑命令
- p 打印匹配行
- d 删除匹配行
- = 显示文件行号
- a\ 在定位行号后附加新文本信息
- i\ 在定位行号后插入新文本信息
- c\ 用新文本替换定位文本
- s 使用替换模式替换相应模式
- r 从另一个文件中读文件
- w 写文本到一个文件
- q 第一个模式匹配完成后推出或立即退出
- l 显示与八禁止ASCII代码等价的控制字符
- {} 在定位行执行的命令组
- n 从另一个文件中读文本下一行,并附加在下一行
- g 将模式2粘贴到/pattern n/
- y 传送字符
- (6)举例说明:
- sed -n '2p' test.txt 打印第二行的信息(注意:-n是不打印不匹配的信息,若没加-n,则打印文件的所有信息而不是匹配信息)
- sed -n '1,4p' test.txt 打印第一行到第四行的信息
- sed -n '/los/p' test.txt模式匹配los,并打印出来
- sed -n '2,/los/p' test.txt 从第二行开始。。知道匹配第一个los
- sed -n '/^$/p' test.txt 匹配空行
- sed -n -e '/^$/p' -e '/^$/=' test.txt 打印空行及行号
- sed -n '/good/a\morning' test.txt 在匹配到的good后面附加morning
- sed -n '/good/i\morning' test.txt 在匹配到的good前面插入morning
- sed -n '/good/c\morning' test.txt 将匹配到的good替换成morning
- sed '1,2d' test.txt 删除第1和2行
- sed 's/good/good morning/g' test.txt 匹配good并替换成goodmorning
- send 's/good/& hello /p' test.txt 匹配到good就在其后面加上hello
- send 's/good/ hello &/p' test.txt 匹配到good就在其前面加上hello
6.1 查找目录下的所有文件中是否含有某个字符串
find .|xargs grep -ri "context"
6.2 find详解
- (1)查找具有某些特征文件的命令,可遍历当前目录甚至于整个文件系统来查看某些文件或目录,其遍历大的文件系统时一般放在后台执行。
- (2)find命令的一般形式
- find pathname -options [-print -exec -ok]
- -pathname :find命令所查找的目录路径。如用"."来表示当前的目录,用/来表示系统根目录
- -print :find命令将匹配的文件输出到标准输出
- -exec: find命令对匹配的文件执行该参数所给出的shell命令,相应的命令形式为
- 'command'{} \; (注意{}和\之间的空格)
- -ok 和 -exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
- options有如下几种:
- -name :按照文件名查找文件
- -perm :按照文件权限来查找文件
- -user :按照文件属主来查找文件
- -group :按照文件所属的组来查找文件
- -mtime -n +n 按照文件的更改时间来查找文件,-n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。find命令还有-atime 和-ctime选项,但它们都和-mtime选项相似。
- -size n[c]查找文件长度为n块的文件,带有c时表示文件长度以字节计。
- -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在
- -newer file1 !file2查找更改时间比文件file1新但比文件file2旧的文件
- -depth 先查找指定目录有无匹配文件,若无则再在子目录中查找
- -type 查找某一类型的文件,如
- b :块设备文件
- d:目录
- e:字符设备文件
- p;管道文件
- l:符号链接文件
- f:普通文件
- (3)find命令举例
- find -name "*.txt" -print 查找txt结尾的文件并输出到屏幕上
- find /cmd ".sh" -print 查找/cmd目录下所有sh文件,并输出
- find . -perm 755 -print 查找当前目录下权限为755的文件,并输出
- find `pwd` -user root -print 查找当前目录下属主为root的文件,并输出
- find ./ -group sunwill -print 查找当前目录下所属主是sunwill的文件
- find /var -mtime -5 -print 查找/var目录下更改时间为5天内的所有文件
- find /var -mtime +5 -print 查找/var目录下更改时间为5天以前的所有文件
- find /var -newer "myfile1" ! -newer "myfile2" -print 查找/var目录下比myfile1新,但是比myfile2旧的所有文件。
- find /var -type d -print 查找/var目录下所有目录
- find /var -type l -print 查找/var目录下所有的符号链接文件。
- find . -size +1000000c -print 查找当前目录下大于1000000字节的文件
- find / -name "con.file" -depth -print 查找根目录下有无"con.file",若无则在其子目录中查找
- find . -type f -exec ls -l {} \; 查找当前目录下是否有普通文件,若有则执行ls -l
- (4)xargs命令
- 在 使用find命令的-exec选项处理匹配到的文件时,find命令将所有匹配到的文件一起传递给exec。不幸的是,有些系统对能够传递给exec的命 令长度有限制,这样find命令运行几分钟之后就算出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs的用处所在,特别是与 find命令一起使用,exec会发起多个进程,而xargs会多个,只有一个
- find ./ -perm -7 -print | xargs chmod o-w 查找权限为7的文件并传递给chmod处理
作 者:请叫我头头哥
出 处:http://www.cnblogs.com/toutou/
关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
linux搜索log文件的内容的更多相关文章
- Linux如何搜索查找文件里面内容
在Linux系统当中,如何搜.索查找文件里面的内容呢? 这个应该是系统维护.管理当中遇到最常见的需求.那么下面介绍,总结一下如何搜索.查找文件当中的内容. 搜索.查找文件当中的内容,一般最常用的是gr ...
- linux下清空文件全部内容,如log日志
在实际操作中经常需要清空log文件, 比如a.log, 有的人说, 直接删除不就行了, 但是, 直接删除后, 没法使用tail -f a.log了. 有的人说, 先rm再touch一个新文件不就可 ...
- linux中Makefile文件相关内容
第一章.概述什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional(专业)的程序员,m ...
- Linux搜索所有文件中的内容
主要命令如下: grep -rn "{填写关键字}" * : 表示当前目录所有文件,也可以是某个文件名-r 是递归查找-n 是显示行号-R 查找所有文件包含子目录-i 忽略大小写- ...
- linux 下查找文件或者内容常用命令
转自:http://www.cnblogs.com/sunleecn/archive/2011/11/01/2232210.html whereis <程序名称>查找软件的安装路径-b 只 ...
- linux shell 比较文件夹内容 diff
diff -ruNa test1 test2 > test12.diff -r 比较子目录中的文件 -u 以合并的方式显示文件的不同 -N 比较目录时,若文件A仅出现在某个目录中,预设 ...
- Linux命令: 向文件写内容,编辑文件,保存文件,查看文件,不保存文件
1.找到要编辑的文件 2.敲 vi t1.txt ,显示文件内容(vim命令) 3.敲 i,最下面变成INSERT 4.编辑自己想要的内容 5a.敲ESC:wq回车 5b.如果不想保存文件在时敲ES ...
- 【linux】复制文件夹内容到另一个文件夹
我一直觉得cp是个非常简单的指令.结果居然遇到坑了.记录一下. 文件夹1:test1/ 文件夹2:test2/ 目标:将test1/中的所有文件和目录拷贝到test2/中 正确指令: cp -rf t ...
- 合并大量txt文件的内容
首先熟悉一个dos命令 显示文件内容命令——type命令 1.格式:type [盘符:] [路径] 文件名 2.类型:内部命令 3.功能:把指定的文件内容在屏幕上显示或打印机输出,它常用作查阅和显示文 ...
随机推荐
- Struts2框架简单介绍
如需,了解Struts2详情,请点击,传送门 工作原理 在Struts2 框架中的处理大概分为以下步骤: 1.客户端初始化一个指向servlet容器(例如Tomcat)的请求. 2.这个请求经过一系列 ...
- 最全面的PS快捷键使用指南(图文演示)
每次做图的时候都会记错快捷键,很苦恼有木有!!!只能各处搜寻PS快捷键汇总起来,老板再也不会说我作图慢了....... 1.Ctrl+T:自由变形 该快捷键,主要对图层进行旋转.缩放等变形调整,同时可 ...
- jmeter实操及性能测试基础知识整理 - 不断更新
主要基于jmetet工具 有任何疑问直接留言,可以相互讨论 线程组菜单: 线程数:并发数量Rame-Up时间(秒):多久跑完线程数,比如线程是10,Rame-Up时间是10秒,就是10秒内跑完10个线 ...
- git stash与git commit的区别
问题的出现 写这篇文章的缘由是在工作中初次使用Git的时候遇到了一个奇怪的现象,即每次提交代码的时候,如果没有及时拉取代码就会导致本地库的代码不是最新的,这样自己修改代码之后想要push到远程仓 ...
- linux下oracle无法删除用户
Oracle删除用户的提示无法删除当前已连接用户.且无法kill掉用户进程的两种解决方法如下: 1.先锁定用户.然后查询进程号,最后删除对应的进程.在删除对应的用户 SQL>alter user ...
- HeadFirst设计模式---观察者
表达公式 注册者 + 订阅者 = 观察者模式 设计气象站 气象站接口 /** ** 布告板 ** @author lollipop ** @since 2019/11/24 **/ public in ...
- Django 基于 jquery 的 ajax
<1> $.ajax的两种写法: $.ajax("url",{}) $.ajax({}) <2> $.ajax的基本使用 $.ajax({ url:&quo ...
- numpy,matplotlib,pandas
目录 numpy模块 numpy简介 numpy使用 matplotlib模块 条形图 直方图 折线图 散点图+直线图 pandas模块 numpy模块 numpy简介 numpy官方文档:https ...
- Linux系统下安装jdk及环境配置(两种方法)
https://blog.csdn.net/qq_42815754/article/details/82968464 这里介绍两种linux环境下jdk的安装以及环境配置方法在windows系统安装j ...
- 关于UE4音效的一些小问题
前言 前几天实在忍受不了StarterContent默认音效的折磨,去网上翻罗了一下初中时很着迷的游戏<Bounce-Tales>的音效,在导入音效时出了点问题,特此说明一下解决方案,希望 ...