趁周末,复习一下鸟哥的linux私房菜,看到文件内容查阅部分,作个笔记,哈哈,但愿对你有帮助哦。html
cat : 由第一行开始显示文件全部内容linux
cat [-AbEnTv]
参数:
-A : 至关于-vET 的整合参数,可列出一些特殊字符,而不是空白而已
-b :列出行号,仅针对非空白行作行号显示,空白行不标行号
-E :将结尾的断行字符$显示出来
-n : 打印行号,连同空白行也会有行号,与-b的参数不一样
复制代码
范例一:redis
查看cattest.txt的内容bash
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat cattest.txt
test cat command
jaywei
#####
复制代码
范例二:less
查看cattest.txt的内容,而且显示行号ide
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# cat -n cattest.txt
1 test cat command
2 jaywei
3
4 #####
复制代码
tac : 从最后一行开始显示,能够看出tac是cat的倒写形式svn
[root@iZ2zehkwp9rwg4azsvnjbuZ whx]# tac cattest.txt
#####
jaywei
test cat command
复制代码
head :显示文件开头的内容,以行为单位,默认文件开头的前10行学习
head [OPTION]... FILE...
-n<行数> 显示的行数
-q 隐藏文件名
-v 显示文件名
-c<字节> 显示字节数
复制代码
显示 sentinel.conf 文件前12行ui
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# head -n 12 sentinel.conf
# Example sentinel.conf
# *** IMPORTANT ***
#
# By default Sentinel will not be reachable from interfaces different than
# localhost, either use the 'bind' directive to bind to a list of network
# interfaces, or disable protected mode with "protected-mode no" by
# adding it to this configuration file.
#
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
#
复制代码
查看文件的内容,也是以行为单位,默认10行,从尾往前看。监听Java动态日志时,通常跟-f
参数配合使用。this
tail [参数] [文件]
-f 循环读取
-q 不显示处理信息
-v 显示详细的处理信息
-c<数目> 显示的字节数
-n<行数> 显示文件的尾部 n 行内容
复制代码
范例一
显示sentinel.conf文件的最后12行
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -n 12 sentinel.conf
# <role> is either "leader" or "observer"
#
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
复制代码
范例二
持续检测sentinel.conf的内容
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -f sentinel.conf
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
# the old address of the master and the new address of the elected slave
# (now a master).
#
# This script should be resistant to multiple invocations.
#
# Example:
#
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
<==要等到输入[ctrl]-c 以后才离开tail 这个命令的检测
复制代码
范例三
持续检测sentinel.conf的内容,并匹配redis关键字。匹配关键字,通常用grep
,tail
通常也会跟grep
搭档使用。
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# tail -f sentinel.conf | grep redis
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
<==要等到输入[ctrl]-c 以后才离开tail 这个命令的检测
复制代码
tial -f
被用来动态监听Java日志,开发联调常用到,它通常跟grep
一块儿搭档使用。more :一页一页地显示文件内容
more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]
参数:
-num :一次显示的行数
-p :不以卷动的方式显示每一页,而是先清除萤幕后再显示内容
-c : 跟 -p 类似,不一样的是先显示内容再清除其余旧资料
-s : 当遇到有连续两行以上的空白行,就代换为一行的空白行
+/pattern : 在每一个文档显示前搜寻该字串(pattern),而后从该字串以后开始显示
-u :不显示下引号 (根据环境变数 TERM 指定的 terminal 而有所不一样)
+num : 从第 num 行开始显示
fileNames :欲显示内容的文档,可为复数个数
复制代码
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more sentinel.conf
# Example sentinel.conf
...(中间省略) ...
# Note that whatever is the ODOWN quorum, a Sentinel will require to
# be elected by the majority of the known Sentinels in order to
# start a failover, so no failover can be performed in minority.
#
# Slaves are auto-discovered, so you don't need to specify slaves in
--More--(29%)
复制代码
仔细看上面的范例,若是more后面接的文件内容行数大于屏幕输出的行数时,就会出现相似上面的图示。重点在最后一行,最后一行会显示出目前显示的百分比,并且还能够在最后一行输入一些有用的命令。在more这个程序的运行过程当中,你可使用一些经常使用的操做命令:
最经常使用的是:按q
离开,按空格键
往下翻页,按b
往回翻页,以及/字符串
搜索功能,请看如下demo
范例一
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此处省略)...
# Before doing that MAKE SURE the instance is protected from the outside
--More--(4%)
复制代码
分页查看sentinel.conf文件,一页展现10行。按下空格键
,能够往下翻页,
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此处省略)...
# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
--More--(7%)
复制代码
按下b
,能够回退到上一页
# *** IMPORTANT ***
...(此处省略)...
# Before doing that MAKE SURE the instance is protected from the outside
# world via firewalling or other means.
--More--(5%)
复制代码
按下q
,能够马上离开more
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]#
复制代码
范例二
若是想在sentinel.conf文件中,搜寻sentinel关键字,能够这样作
[root@iZ2zehkwp9rwg4azsvnjbuZ redis-4.0.7]# more -10 sentinel.conf
# Example sentinel.conf
...(此处省略)...
# Before doing that MAKE SURE the instance is protected from the outside
/sentinel 输入/以后,光标就会自动跑到最下面一行等待输入
复制代码
如同上面的说明,输入了/以后,光标就会跑到最下面一行,而且等待你的输入,你输入了字符串并按下[Enter]以后,more就会开始向下查询该字符串,而重复查询同一个字符串,能够直接按下n便可。最后不想看了,就按下q离开more。
# Before doing that MAKE SURE the instance is protected from the outside
/sentinel
...skipping
# protected-mode no
# port <sentinel-port>
# The port that this sentinel instance will run on
port 26379
# sentinel announce-ip <ip>
# sentinel announce-port <port>
#
/
...skipping
# Example:
#
# sentinel announce-ip 1.2.3.4
# dir <working-directory>
# Every long running process should have a well-defined working directory.
# For Redis Sentinel to chdir to /tmp at startup is the simplest thing
# for the process to don't interfere with administrative tasks such as
# unmounting filesystems.
--More--(23%)
复制代码
less 与 more 相似,但less的用法比起more又更加有弹性。
范例一
在sentinel.conf文件中,搜寻sentinel关键字,以下
less sentinel.conf
复制代码
输入反斜杠/
,输入关键字sentinel
,回车
重复前一个搜索,能够按n,反向重复前一个搜索,按N
范例二
Linux 动态查看日志文件,通常用tail -f ,可是咱们也能够用less+ F 实现。
less + file-name + 命令 F = tail -f + file-name
复制代码
咱们常常用tail -f +grep 关键字,动态查找报错的日志,也能够用less实现。先输入shirft+g,到达文件结尾
而后输入?
,输入搜索关键字,如sentinel
,回车,而后按n
键往上搜索,效果是否是也不错?尤为日志文件动态刷新太快的时候,奸笑脸。
本文总结了查看文件日志的几个linux命令,cat、tac、head、tail、more、less,其中less真的很适合平常开发日志查看,很是推荐less。