linux 常用命令

linux常用命令

文件内容查看命令

文件查看命令主要为cat 、more、less。关于这几个命令在文件命令写得比较详细,大家可以参考。

个人对以上几个命令进行了基本实践操作

cat

  • 1 创建文章不清除原有内容 cat>>filename 创建之后可直接进行编辑,如果是已存在的文件,则是在原有内容下面进行内容新增(退出cat可以直接按下contral+D),cat filename直接查看内容
➜  Desktop cat >>test1
test1
hello  test

test 3
➜  Desktop cat >> test1
test 4
➜  Desktop cat test1
test1
hello  test

test 3
test 4
➜  Desktop
  • 2 创建文章清除原有内容 cat >filename,创建之后可以直接进行编辑,如果是已存在的文件,则原有内容会被清空
➜  Desktop cat >test2
test11
测试
➜  Desktop cat >test2
新增
➜  Desktop cat test2
新增

所有 cat >>与cat >的区别是对文件原有内容的处理不一样,如果是不想要原有内容则可以直接用cat > ,如果想要原有内容则用cat >>.为了保险起见还是建议用cat >>命令,大不了可以对原有内容进行删除

  • 3 文件合并
    cat file1 file2 >>file3 将file1与file2文件合并为file3,如果file3未创建则直接创建,如果已创建则,在原有内容上新增。
➜  Desktop cat test1 test2 >>test3
➜  Desktop cat test3
test1
hello  test

test 3
test 4
新增
➜  Desktop cat >> test3
我再式下
➜  Desktop cat test1 test2 >>test3
➜  Desktop cat test3
test1
hello  test

test 3
test 4
新增
我再式下
test1
hello  test

test 3
test 4
新增

同样我们可以用 cat file1 file2 > file3 ,合并时如果file3中有内容,则会被清除,与新建文件是类似。前面都是在一个目录下面进行文章的合并,如果是不同目录,则只需要加上目录路径就可以

➜  study cat /Users/huxy/Desktop/test1 /Users/huxy/code/test4 > test5
➜  study cat test5
test1
hello  test

test 3
test 4
件下

其他

  • 1 cat -n file 列出行号
  • 2 cat -b file 与 -n类似,对空白行不进行编号
  • 3 cat -s file 当遇到有连续两行以上的空白行,就代换为一行的空白行
➜  Desktop cat -n test3
     1  test1
     2  hello  test
     3
     4  test 3
     5  test 4
     6  新增
     7  我再式下
     8  test1
     9  hello  test
    10
    11  test 3
    12  test 4
    13  新增
➜  Desktop cat -b test3
     1  test1
     2  hello  test

     3  test 3
     4  test 4
     5  新增
     6  我再式下
     7  test1
     8  hello  test

     9  test 3
    10  test 4
    11  新增
➜  Desktop cat -s test3
test1
hello  test

test 3
test 4
新增
我再式下
test1
hello  test

test 3
test 4
新增

nl

nl 与cat -n很类似

  • nl testcat7.txt 显示行号(空白行不展示)
  • nl -b a testcat7.txt 显示行号,空白行也展示
  • nl -b a -n rz testcat7.txt 显示行号,行号前面自动补0,统一输出格式
  • nl -b a -n rz -w 3 testcat7.txt 显示固定宽度,现在固定3位数的宽度
➜  Desktop nl test3
     1  test1
     2  hello  test

     3  test 3
     4  test 4
     5  新增
     6  我再式下
     7  test1
     8  hello  test

     9  test 3
    10  test 4
    11  新增



    12  test11
➜  Desktop nl -b test3
➜  Desktop nl -b a test3
     1  test1
     2  hello  test
     3
     4  test 3
     5  test 4
     6  新增
     7  我再式下
     8  test1
     9  hello  test
    10
    11  test 3
    12  test 4
    13  新增
    14
    15
    16
    17  test11
➜  Desktop  nl -b a -n rz test3
000001  test1
000002  hello  test
000003
000004  test 3
000005  test 4
000006  新增
000007  我再式下
000008  test1
000009  hello  test
000010
000011  test 3
000012  test 4
000013  新增
000014
000015
000016
000017  test11
➜  Desktop  nl -b a -n rz -w 3 test3
001 test1
002 hello  test
003
004 test 3
005 test 4
006 新增
007 我再式下
008 test1
009 hello  test
010
011 test 3
012 test 4
013 新增
014
015
016
017 test11

more

more命令与cat类似,cat是展示整个文件,而more则是以一页一页的显示方式来显示内容。在网上有一篇比较好的介绍more命令的文章more

  • 分页获取 more -n file 分页获取
  • 上下翻页 control+f 向下翻n行,control+b 向上翻一行
  • 查看当前行数 按下“=”键
  • 查看user message 加上参数-d
  • 不滚动页面,直接对历史数据进行删除 more -20 -c file可以直接只会直接清屏,不是进行滚动屏幕
  • 按下enter,默认加载往下加载一行( 差点把公司日志搞出去)

less

more命令是一次性加载完所有的记录,二less在查看之前是不会加载整个文件。

  • 分屏查看文件 less filename
  • 查看文件并标识行号 less -N filename
  • 读取文件的百分比、行号及总行数 less -M filename
    在less后面有些后续操作,在网上有些比较好的介绍less
    结合查看日志的情况,进行举例说明
  • 打开日志文件 less filename
  • 定位到最后一行 直接按G键,定位到最后
  • 向上翻页查看记录 control+f

这么多命令有时候压根就记不住,所以就需要按下h来查看具体的帮助

head

head 命令显示开头的几行

  • head filename 显示默认前10行:head test3
  • head -n filename 显示前 n行 head -15 test3
  • head -n k filename 显示前k行 head -n 15 test3
  • head -c n filename 显示文件前n个字节 head -c 10 test3
    head命令的重点是输出文件前面的内容

tail

与head对应的命令就是tail ,查看文章最后几行

  • tail filename 默认查看最后10行 tail test3
  • tail -n filename 查看最后n行 tail -12 test3,如果为正数则是tail +12 test3 从test3的12行开始用
  • tail -f filename 不停读取最新内容,达到实时监视的效果 tail -f test3,control +c 退出监控
  • tail -c filename 输出最后的c字节 tail -c 10 test3
  • tail -f -n 指定刷新的行数tal -f -n 5 test3

现在head 与tail的命令都用了,所以可以联合一起使用。比如

  • head -n filename| tail -k 取filename前n行中的最后k行 比如head -20 test3|tail -5取的是test3中的前20行的最后5行

grep

grep为一个强查询命令,是查询日志常用命令

单个简单查询

  • grep XX filename 检索关键字 grep activityId=1929 citybuy.log 查询日志中所有包行 activityId=1929的日志

  • grep XX filename1 filename2在多个文件中检索关键字

  • grep -v XX filename 不包含关键词的行

  • grep ^XX filename 以指定模式开头行

  • grep -r XX /filename 递归查找,grep -r test /etc etc下面所有包行test都会查找出来

  • grep test$ finlename 以关键词模式结尾的行

  • grep -n XX filename 检索关键字并输出行号 grep activityId=1929 citybuy.log,这样就可以知道具体的行号,可以具体结合tail,head命令查询相关前后日志,或是直接使用grep -A,grep -B来查询前后相关日志,比如查到行号是100,可以用 cat citybuy.log |tail -n +100 |head -n 20,从第100行起的前20行日志。

  • grep -c xx filename 检索关键字出现的次数,grep -c activityId=1929 citybuy.log

  • grep -i XX file 不区分大小写 grep -i activityId=1929 citybuy.log

  • grep -w XX file 匹配必须是整个单词 grep -w activityId=1929 citybuy.log

  • -A,-B,-C检索关键字并输出前、后、前后n行,grep -n activityId=1929 citybuy.log -C 5检索关键字并输出相应的前后5行

  • grep -l XX file1 file2 file3 列出包含关键词的文档,可初步定位到内容在哪个文件 grep -l '"branchId":8' citybuy.log citybuy.log.2018-07-02 结果是会列出包行branchId:8的文件,如果是L,则是未匹配到关键词的文件

  • grep -f file1 file2 找出file2中存在的file1中的元素,如果有重复值也正常输出,其实就类似与两个文件中的交集,同样 grep -v -file1 file2 就是取在file2中不在file1中,有重复值时也全部不展示。有种取差集的概念。

多个查询

或查询

grep -E 'xx1|xx2' filename 在filename 中查询包含关键字xx1或是xx2的记录,grep -c -E 'activityId=1929|"branchId":8' citybuy.log。

或者直接是使用egrep -c 'activityId=1929|"branchId":8' citybuy.log

或者使用grep -e activityId=1929 -e "branchId":8 filename

与查询

grep XX1 filename |gep XX2 查找同时包含关键字xx1与xx2的行
例如 grep activityId=1929 citybuy.log|grep "branchId":8'找出日志中含有branchId为8且activityId为 1929的日志

grep -E 'pattern1.pattern2' filename grep -E 'activityId=1929."branchId":8' citybuy.log 这个是有顺序的,先pattern1然后才是pattern2.

grep -E 'pattern1.pattern2|pattern2.pattern1' filename可以实现查询包含pattern1,pattern2的文件。

正则表达式

在使用grep命令时经常用到的就是正则表达式,在网上找到了一篇文章对此进行了说明grep 正则表达式

pattern正则表达式主要参数:


    \: 忽略正则表达式中特殊字符的原有含义。
    
    ^:匹配正则表达式的开始行。
    
    $: 匹配正则表达式的结束行。
    
    \<:从匹配正则表达式的行开始。
    
    \>:到匹配正则表达式的行结束。
    
    [ ]:单个字符,如[A]即A符合要求 。
    
    [ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
    
    .:所有的单个字符。
    
    * :有字符,长度可以为0。
其他操作

以上都是对grep的搜索功能进行说明,有的时候我们是需要对日志进行二次分析的,所以会将过滤数据进行存储

cat -n info.log |grep "XX" >xxx.txt 将过滤出的日志进行存储

cat -n info.log |grep "XX" |more 内容过多时进行可以加more进行分页查看

awk

awk 命令是行处理器,对行的定义是/n来区分。可以对行进行分割取值,然后获取对应的值。通过查阅相关资料,有篇文章对此描述还不错awk 命令awk 入门

分割打印基本使用

默认是空格进行分割,可用 -F‘ XX’进行分割,0是全部,1是分割后得到的第一个参数,$2是分割后得到的第二个参数,以此类推,当然还可以按一定格式进行打印

➜  ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '{print $0,$1, $3}' test3
2 this is a tes 2 is
3 Are you like awk 3 you
This's a test This's test
10 there are orange,apple ,mongo 10 are
测试 测试
我们不一样 我们不一样
哈哈哈   ,我去 哈哈哈
测试,哈哈 测试,哈哈
test , test test test
➜  ~ awk '{printf"%-8s %-10s\n", $1,$4}' test3
2        a
3        like
This's
10       orange,apple
测试
我们不一样
哈哈哈
测试,哈哈
test
➜  ~ awk -F ',' '{print $1,$2}' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange apple
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test   test

内建变量

内建变量有多种,下面就些最基本的进行测试。

  • FILENAME 文件名
  • FNR 各文件分别计数的行号
  • FS 字段分割符
  • NF 一条记录字段数目
  • NR 已经读出的记录数,即行号,从1开始
awk 'BEGIN{printf "%4s %4s %4s %4s %4s \n","FILENAME","FNR","FS","NF","NR";printf "---------------------------------------------\n"} {printf "%4s %4s %4s %4s %4s  \n",FILENAME,FNR,FS,NF,NR}'  test3
FILENAME  FNR   FS   NF   NR
---------------------------------------------
test3    1         5    1
test3    2         5    2
test3    3         3    3
test3    4         5    4
test3    5         1    5
test3    6         1    6
test3    7         2    7
test3    8         1    8
test3    9         3    9

正则匹配

字符匹配有2种

  • 纯字符直接匹配 //纯字符匹配 !//纯字符不匹配
  • 字段值匹配 ~//字段值匹配 !~//字段值不匹配
➜  ~ awk '/are/' test3
10 there are orange,apple ,mongo
➜  ~ awk !'/are/' test3
2 this is a tes
3 Are you like awk
This's a test
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '$2 ~/Are|this/ {print $0}' test3
2 this is a tes
3 Are you like awk
➜  ~ awk '$2 !~/Are|this/ {print $0}' test3
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test

条件表达式

== != > >=用于过滤

~ awk '$1==3||$1=="测试"' test3
3 Are you like awk
测试
➜  ~ awk '$1!=3||$1!="测试"' test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
➜  ~ awk '$1>3' test3
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test

逻辑运算符

&& || 多条件过滤

➜  ~ cat test3
2 this is a tes
3 Are you like awk
This's a test
10 there are orange,apple ,mongo
测试
我们不一样
哈哈哈   ,我去
测试,哈哈
test , test
test1  test2 hahah
test1  test3 go
test   test
➜  ~ awk '$1 ~/test/||$2~/test2/' test3
test , test
test1  test2 hahah
test1  test3 go
test   test
➜  ~ awk '$1 ~/test/&&$2!~/test2/' test3
test , test
test1  test3 go
test   test

条件语句
if语句与while 语句


➜  ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4
➜  ~ awk 'BEGIN {A=0;B=0}{if ($1>10){ A++} else{B++}} END {print A,"\t"B}' test3
9   3
➜  ~ awk 'BEGIN {A=1}{while (A<NF) print NF,$0 ,A++}' test3
5 2 this is a tes 1
5 2 this is a tes 2
5 2 this is a tes 3
5 2 this is a tes 4

之所以学习以上几个命令,其实是源于日志查询的需要。
对某个字段出现次数进行统计

~ awk -F ' ' '{sum[$1]++} END{for (i in sum) print i "\t" sum[i]}' test3
2   1
3   1
哈哈哈 1
This's  1
测试  1
10  1
我们不一样   1
测试,哈哈   1
test1   2
test    2

 sum[$i] 为对第一列中字符出现的次数进行统计

grep与awk 结合使用

zgrep 'com.gexin.rp.base.log.LogManager.slow' rp-slow-20171212-0.log.gz |awk -F':' '{print($1)}'|sort|uniq -c|sort -t' ' -k1n

以上命令的意思就是包含 查找包含com.gexin.rp.base.log.LogManager.slow每行日志,按照: 分域后取第一个域的值,并计算对应的的总数按照统计值从大到小排列

zgrep 'save_list_body|QX3GgC1oXf9IYWX1LCPVE8' *rp-message-20171211* | awk -F'|' '{print($6)}' | awk -F'=' '{print($5)}'|sort| uniq -c | more



zgrep 'QX3GgC1oXf9IYWX1LCPVE8' *rp-bi-20180105* |grep 'RASL'|grep '2018-01-05 '|wc -l

sort 命令补充

cat file1.txt file2.txt | sort | uniq > file.txt 并集
cat file1.txt file2.txt | sort | uniq -d >file.txt 交集
 差集:求file1.txt相对于file2.txt的差集,可先求出两者的交集temp.txt,然后在file1.txt中除去temp.txt即可。

     cat file1.txt file2.txt | sort | uniq -d >temp.txt

     cat file1.txt temp.txt | sort | uniq -u >file.txt

自己在网上查到一篇关于sort命令sort命令

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,392评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,258评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,417评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,992评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,930评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,199评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,652评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,327评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,463评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,382评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,432评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,118评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,704评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,787评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,999评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,476评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,057评论 2 341

推荐阅读更多精彩内容

  • 查询命令 find * find [指定查找目录] [查找规则] [查找完后执行的action]* find ~/...
    chapa阅读 702评论 0 5
  • Linux常用命令大全(非常全!!!)原文链接:http://www.cnblogs.com/yjd_hycf_s...
    JokerJin阅读 573评论 0 3
  • 在 LINUX 命令平台输入 1-2 个字符后按 Tab 键会自动补全后面的部分(前提是要有这个东西,例如在装了 ...
    sudop阅读 611评论 0 5
  • 在最美的年龄, 选择了最美的一条路, 医者仁心, 你用青春, 把爱撒满人间。 一袭白衣, 遮住是你的靓丽身姿, 遮...
    潦倒的文青阅读 363评论 0 1
  • 博文主要讲classloader的模型、作用和使用,内容是作者学习java反射机制有关知识时记录的笔记。 Clas...
    静默加载阅读 7,737评论 0 7