linux常用命令总结

(linux常用命令)

一.top、ps

1.top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况

[91466766@shell.testing-studio.com ~]$top
top - 23:24:19 up 137 days,  1:39, 14 users,  load average: 0.21, 0.12, 0.32
Tasks: 161 total,   1 running, 159 sleeping,   1 stopped,   0 zombie
%Cpu(s):  0.3 us,  1.3 sy,  0.0 ni, 98.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  1016396 total,    79592 free,   441720 used,   495084 buff/cache
KiB Swap:        0 total,        0 free,        0 used.   335396 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
11712 root      10 -10  139984  18920   2756 S  1.7  1.9 319:03.21 AliYunDun
  533 root      20   0  907508  28840   4788 S  0.3  2.8 324:18.00 CmsGoAgent.+

2.ps 为我们提供了进程的一次性的查看,它所提供的查看结果并不动态连续的

[91466766@shell.testing-studio.com ~]$ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.3 125932  3436 ?        Ss    2019  39:21 /usr/lib/system
root         2  0.0  0.0      0     0 ?        S     2019   0:02 [kthreadd]
root         3  0.0  0.0      0     0 ?        S     2019   9:58 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<    2019   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S     2019   0:00 [migration/0]

二.netstat

查看端口和连接的信息时,能查看到它们对应的进程名和进程号对系统管理员来说是非常有帮助的。举个栗子,Apache 的 httpd 服务开启80端口,如果你要查看 http 服务是否已经启动,或者 http 服务是由 apache 还是 nginx 启动的,这时候你可以看看进程名。

[91466766@shell.testing-studio.com ~]$netstat -lntp
(No info could be read for "-p": geteuid()=2626 but you should be root.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN

三.grep

介绍:grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来
主要参数: grep --help可查看
-c:只输出匹配行的计数。
-i:不区分大小写。
-h:查询多文件时不显示文件名。
-l:查询多文件时只输出包含匹配字符的文件名。
-n:显示匹配行及 行号。
-s:不显示不存在或无匹配文本的错误信息。
-v:显示不包含匹配文本的所有行。
–color=auto :可以将找到的关键词部分加上颜色的显示。
pattern正则表达式主要参数:
\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
<:从匹配正则表达 式的行开始。
>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
.:所有的单个字符。
*:所有字符,长度可以为0。
实战1:1)查找文件中包含root的行数

[91466766@shell.testing-studio.com dm]$grep -n root greptest.txt
1:root
3:root

2)查找文件内容不包含root的行数

[91466766@shell.testing-studio.com dm]$grep -nv root greptest.txt
2:heheh
4:new
5:string
6:newyearn

实战2:1)查找以s开头的行

[91466766@shell.testing-studio.com dm]$grep -n ^s greptest.txt
5:string

2) 查找以n结尾的行

[91466766@shell.testing-studio.com dm]$grep -n n$ greptest.txt
6:newyearn

sed

介绍:Sed是一种非交互式的流编辑器,可动态编辑文件;流编辑器则会在编辑器处理数据之前基于预先提供的一组 规则来编辑数据流 。
Sed本身是一个管道命令,可以分析 standard input 的,主要是用来分析关键字的使用、统计等,此外还可 以将数据进行替换、删除、选中、选取特定行等功能
实战1:1)查看帮助

[91466766@shell.testing-studio.com ~]$man sed
[91466766@shell.testing-studio.com ~]$sed -h
sed:无效选项 -- h
用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...

  -n, --quiet, --silent
                 取消自动打印模式空间
  -e 脚本, --expression=脚本
                 添加“脚本”到程序的运行列表
  -f 脚本文件, --file=脚本文件
                 添加“脚本文件”到程序的运行列表
  --follow-symlinks
                 直接修改文件时跟随软链接
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
  -b, --binary
                 does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
                 open files in binary mode (CR+LFs are not treated specially))
  -l N, --line-length=N
                 指定“l”命令的换行期望长度
  --posix
                 关闭所有 GNU 扩展
  -r, --regexp-extended
                 在脚本中使用扩展正则表达式
  -s, --separate
                 将输入文件视为各个独立的文件而不是一个长的连续输入
  -u, --unbuffered
                 从输入文件读取最少的数据,更频繁的刷新输出
  -z, --null-data
                 separate lines by NUL characters
  --help
                 display this help and exit
  --version
                 output version information and exit

实战2:1)在第四行后添加新字符串

[91466766@shell.testing-studio.com dm]$sed -e '4a newline testfile' sedtest.txt
root
signal
root
string
newline testfile
happy
newyear

2)在第二行前添加新的字符串

[91466766@shell.testing-studio.com dm]$sed '2i new line' sedtest.txt
root
new line
signal
root
string
happy
newyear

实战3:1)文件中的root全部替换hello

[91466766@shell.testing-studio.com dm]$sed -e 's/root/hello/g' sedtest.txt
hello
signal
hello
string
happy
newyear

2)(操作须谨慎,会直接修改文件中的内容)直接修改文件中的root为hello

[91466766@shell.testing-studio.com dm]$cat sedtest.txt
hello
signal
hello
string
happy
newyear

awk

awk程序由一个主输入循环(Main input loop)维持
主输入循环反复执行,直到条件被触发
主输入循环无须由程序员去写,awk已经搭好主输入循环的框架
awk:报告生成器,格式化文本输出,有多种版本:New awk(nawk),GNU awk( gawk)

awk模式匹配
*任何awk语言都由 模式(pattern)和动作(action) 组成
*模式 是由一组用于测试输入行是否需要执行的规则
*动作 是包含语句,函数和表达式的执行过程
*简言之,模式决定动作何时触发和触发事件,动作执行对输入行的处理
*第一种方式:awk '/^KaTeX parse error: Expected group after '^' at position 40: … line."}' file ^̲是模式,{ }中的内容是动作,表示一旦读入的输入文件行是空行,就执行{ }中的动作
*第二种方式:将命令写入文件中,然后调用awk -f one.awk file
*第三种方式:以脚本的方式执行awk命令
*注意:与文件名无关,可以不加.awk后缀

linux实战Nginx日志分析

实战1提取nginx.log中的 404 和 500 报错的数目并打印(grep,awk)
(1)grep
grep -E(扩展正则) 可以传递多个内容 ,使用 | 来分割多个pattern

[91466766@shell.testing-studio.com ~]$ls
dm  nginx.log
[91466766@shell.testing-studio.com ~]$grep -E ' 404 | 500 ' nginx.log |wc -l
267

(2)awk

[91466766@shell.testing-studio.com ~]$awk '$9~/404|500/' nginx.log |wc -l
267

实战2提取nginx.log中的访问量最多的三个ip地址(grep,awk)
(1)grep

[91466766@shell.testing-studio.com ~]$ls
dm  nginx.log
[91466766@shell.testing-studio.com ~]$grep -o '^[0-9]*.[0-9]*.[0-9]*.[0-9]*' nginx.log | sort | uniq -c | sort -nr | head -n 3
    282 216.244.66.241
    130 136.243.151.90
    110 127.0.0.1

-o:只打印匹配行的匹配部分;
^: 匹配字符串的开始;
*: 匹配前面一个字符出现0次或多次;
.: 匹配任意字符;
sort:排序(默认升序)
sort -r:以相反的顺序排序;
sort -n:依照数字的大小排序;
uniq:用于检查和删除文本中重复出现的行列,一般与sort命令结合使用;
uniq -c或–count:在每列旁边显示该行重复出现的次数;
head -3:取出前面三个。
(2)awk

[91466766@shell.testing-studio.com ~]$awk -n '{print $1}' nginx.log |sort|uniq -c| sort -nr -k1 |head -n 3
    282 216.244.66.241
    130 136.243.151.90
    110 127.0.0.1

tail -3 与head -3的区别:tail -3 打印负3条,head -3 打印前三条。
总结
grep擅长查找功能, sed 擅长取行和替换。 awk 擅长取列
1.grep:文本过滤(模式:pattern)工具,grep, egrep
grep [OPTIONS] PATTERN [FILE…]
options部分
-i:忽略大小写
–color:高亮匹配上的字符串
-v: 显示没有被模式匹配到的行
-o:只显示被模式匹配到的字符串
-E:使用扩展的正则表达式
PATTERN部分
以字符串的方式给定匹配模板,可以使用普通字符串以及正则表达式(标准&扩展)。
FILE部分
需要查找内容的文件。
2.sed: 把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。
sed [option]… ‘script’ inputfile
选项:
-n不输出模式空间内容到屏幕,即不自动打印
-f /PATH/SCRIPT_FILE: 从指定文件中读取编辑脚本
a []text1 在指定行后面追加文本,支持使用\n实现多行追加
i []text 在行前面插入文本

3.awk:报告生成器,格式化文本输出,有多种版本:New awk(nawk),GNU awk( gawk)
awk [options] ‘program’ file…
program:pattern{action statements;…}

1)pattern部分决定动作语句何时触发及触发事件
BEGIN,END
2)action statements对数据进行处理,放在{}内指明
print, printf
项目:
-F 指明输入时用到的字段分隔符
-v var=value 自定义变量
分割符、域和记录
awk执行时,由分隔符分隔的字段(域)标记1,1,2…n称为域标识,n称为域标识,0为所有域。
省略action,则默认执行 print $0 的操作

四.bash编程训练

实战1:a=10,b=20,编写if语句比较a,b的值,且输出结果

[91466766@shell.testing-studio.com ~]$a=10
[91466766@shell.testing-studio.com ~]$b=20
[91466766@shell.testing-studio.com ~]$if [ $a -eq $b ]; then echo "equl"; elif [ $a -lt $b ]; then echo "small"; elif [ $a -gt $b ]; then echo "big"; fi
small

实战2:使用for循环打印出一个txt文件中的内容

[91466766@shell.testing-studio.com dm]$for i in $(cat sed.txt); do echo $i; done
1
2
3

实战3:使用while循环打印一个txt文件中的内容

[91466766@shell.testing-studio.com dm]$while read i; do echo $i; done<sed.txt
1
2
3

比较:while和for循环的区别
while:以行读取文件,默认分隔符是空格或者Tab

[91466766@shell.testing-studio.com dm]$while read i; do echo $i; done<sed.txt
1 45
2
3

for:以空格读取文件,也就是碰到空格,就开始执行循环体,所以需要以行读取的话,就要把空格转换成其他字符。

[91466766@shell.testing-studio.com dm]$for i in $(cat sed.txt); do echo $i; done
1
45
2
3

五.bash脚本编写

实战1:传参

#!/bin/bash
echo $1,$2,$3
echo "文件名 "$0
echo "参数数量 "$#
echo "all "$*
echo "return "$?

执行结果:

[91466766@shell.testing-studio.com dm]$bash test.sh 1 2 3
1,2,3
文件名 test.sh
参数数量 3
all 1 2 3
return 0

实战2:a=10,b=20,进行加、减、乘、除、取余运算

#!/bin/bash
a=10
b=20
echo `expr $a + $b`
echo `expr $a - $b`
echo `expr $a \* $b`
echo `expr $a / $b`
echo `expr $a % $b`

执行结果:

[91466766@shell.testing-studio.com dm]$bash test.sh
30
-10
200
0
10

实战3:a=10,b=20,进行比较运算

#!/bin/bash
a=10
b=20
if [ $a -eq $b ]
then
        echo "equl"
elif [ $a -lt $b ]
then
        echo "a小于b"
elif [ $a -gt $b ]
then
        echo "a大于b"
fi

执行结果:

[91466766@shell.testing-studio.com dm]$bash test.sh
a小于b

实验4:bash脚本与linux命令结合使用,取cpu列的信息保存到log文件中

#!/bin/bash
ps aux | awk '{print $3}' > cpu.log #将第三行数据重定向保存到cpu.log文件中

执行结果:

[91466766@shell.testing-studio.com dm]$bash look_cpu.sh
[91466766@shell.testing-studio.com dm]$ls
cpu.log
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值