命令总结"/>
Linux下命令总结
文章目录
- 1. linux下探测端口是否存活【nc】
- 2. linux下显示进程完成信息以及显示进程间关系【ps auxfww】
- 3. bc运算命令【bc】
- 4. seq命令
- 5. shell中关于命令行的一些不好记的参数
- 6. 那些年令我们头疼的grep、awk、sed(三剑客)
- 6.1 grep
- 6.2 awk
- 6.2.1 awk入门讲解
- 6.2.2 awk列处理常用知识
- 6.2.3 awk的判断语法
- 6.2.4 awk数组循环语法
- 6.2.5 awk分析Nginx日志
- 6.3 sed
- 6.3.1 sed文本操作入门
- 6.3.2 sed文本替换以及应用
- 7. find命令
- 7.1 find命令查找入门
- 7.2 find文件查找加动作
- 8. curl命令
- 9. lsof命令
- 10. 怎么找出进程号
- 11.uniq
- 12. sort
- 13. cut
- 14. 系统性能分析
- 14.1. sar
- 14.2. top
- 14.3. vmstat
- 14.4 free
- 15. yum
- 15.1. yum provides 查看命令的安装包
1. linux下探测端口是否存活【nc】
#1. nc命令 使用yum install nmap -y
nc -w 2 -zv 探测机器ip 端口号
示例:
[root@docker mysql]# nc -w 2 -zv 192.168.1.4 22
Ncat: Version 7.50 ( )
Ncat: Connected to 192.168.1.4:22.
Ncat: 0 bytes sent, 0 bytes received in 0.03 seconds.
```
2. linux下显示进程完成信息以及显示进程间关系【ps auxfww】
#具体详细转载自博客:.html
[root@docker mysql]# ps auxfww
root 6699 0.8 2.0 532728 38476 ? Ssl Jul08 6:54 /usr/bin/dockerd
root 7025 0.2 0.4 316188 9288 ? Ssl Jul08 2:01 \_ docker-containerd -l unix:///var/run/docker/libcontainerd/docker-containerd.sock --metrics-interval=0 --start- timeout 2m --state-dir /var/run/docker/libcontainerd/containerd --shim docker- containerd-shim --runtime docker-runc
root 13417 0.0 0.2 272456 3944 ? Sl 02:57 0:00 | \_ docker-containerd-shim 3551c4d8d60e0c7060fbb66bf34dcb03974fdabc110ca947ab421a8ad021c120 /var/run/docker/libcontainerd/3551c4d8d60e0c7060fbb66bf34dcb03974fdabc110ca947 ab421a8ad021c120 docker-runc
root 13434 0.0 0.1 32644 3248 ? Ss 02:57 0:00 | | \_ nginx: master process nginx -g daemon off;
101 13458 0.0 0.0 33100 1604 ? S 02:57 0:00 | | \_ nginx: worker process
```
3. bc运算命令【bc】
因为bash shell不支持浮点数运算,所以用bc命令进行运算
[root@shell_test ~]# yum install bc -y
#scale表示保留几位小数
[root@shell_test ~]# echo "scale=2; 5/2.0"|bc
2.50
4. seq命令
用于产生从某个数到另外一个数之间的所有整数。
阅读博友 .html
【语法】:
seq [选项]... 尾数
seq [选项]... 首数 尾数
seq [选项]... 首数 增量 尾数
【选项】:
-f, --format=格式 使用printf 样式的浮点格式
-s, --separator=字符串 使用指定字符串分隔数字(默认使用:\n)
-w, --equal-width 在列前添加0 使得宽度相同
【例子】:
[root@Gin scripts]# seq -f "%3g" 9 1191011
%后面指定数字的位数 默认是%g,%3g那么数字位数不足部分是空格。
[root@Gin scripts]# seq -f "str%03g" 9 11
str009
str010
str011
这样的话数字位数不足部分是0,%前面制定字符串。
[root@Gin scripts]# seq -w 98 101
098
099
100
101
-w选项:指定输出数字同宽
-s选项:指定分隔符(默认是回车)
[root@Gin scripts]# seq -s" " -f"str%03g" 9 11
str009 str010 str011
[root@Gin scripts]# seq -s"`echo -e "/t"`" 9 11
9/t10/t11
[root@Gin scripts]# seq -s '=' 1 5
1=2=3=4=5
[root@shell_test shell]# seq -s " " -f "sunwei%04g" 100
sunwei0001 sunwei0002 sunwei0003 sunwei0004 sunwei0005 sunwei0006 sunwei0007 sunwei0008 sunwei0009 sunwei0010 sunwei0011 sunwei0012 sunwei0013 sunwei0014 sunwei0015 sunwei0016 sunwei0017 sunwei0018 sunwei0019 sunwei0020
5. shell中关于命令行的一些不好记的参数
$0: 脚本本身文件名称
$1: 命令行第一个参数,$2为第二个,以此类推
$*: 所有参数列表
$@: 所有参数列表
$#: 参数个数
$$: 脚本运行时的PID
$?: 脚本退出码∗与@的区别当命令行为test.sh 1 2 3
"$*“表示"1 2 3”
"$@“表示"1” “2” “3”
二者没有被引号括起来时是一样的都为"1 2 3",只有当被引号括起来后才表现出差异
----------------------------------------------------------------
----------------------------------------------------------------
$() 等同于: ``(反引号):运行一段命令
$(()) 进行数字运算 # a=3;b=2;c=5
# echo $((a+b*c))
6. 那些年令我们头疼的grep、awk、sed(三剑客)
6.1 grep
示例文本如下:
[root@shell_test txt]# cat test1
#sunwei
#sunwei111
sunwei
this is a test1 script
this is a test2 script
THIS IS a test3 script#1) 反向查找
[root@shell_test txt]# cat test1|grep -v "^#"#2) grep忽略大小写
[root@shell_test txt]# cat test1|grep -i "this"#3) grep仅仅打印匹配,不打印整行
[root@shell_test txt]# cat test1 |grep -o "script"#4) grep打印出上下文
[root@shell_test txt]# cat test1|grep -A 2 "test1" ------------->打印匹配行的下2行,行数可以自定义
this is a test1 script
this is a test2 script
THIS IS a test3 script
[root@shell_test txt]# cat test1|grep -B 2 "test1" ------------->打印匹配行的上2行,行数可以自定义
#sunwei111
sunwei
this is a test1 script
[root@shell_test txt]# cat test1|grep -C 2 "test1" ------------->打印匹配行的上下两行,行数可以自定义
#sunwei111
sunwei
this is a test1 script
this is a test2 script
THIS IS a test3 script#5) grep递归查找
grep -r 'ftp' /etc/passwd
[root@shell_test txt]# grep -r 'ftp' /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
------------小练习:过滤nginx官方网站安装包的版本----------------------
[root@shell_test txt]# curl -s .html|egrep -o 'Stable version.*'|egrep -o 'href="[^"]+"'|grep 'tar.gz"$'
[^"]+ 表示双引号内没有引号,否则会匹配到末尾;匹配规则是 ‘href="[^"]+"’中是以href="开头,以“结尾,中间是非"的字符
href="/download/nginx-1.16.0.tar.gz"
href="/download/nginx-1.14.2.tar.gz"
href="/download/nginx-1.12.2.tar.gz"
href="/download/nginx-1.10.3.tar.gz"
href="/download/nginx-1.8.1.tar.gz"
href="/download/nginx-1.6.3.tar.gz"
href="/download/nginx-1.4.7.tar.gz"
href="/download/nginx-1.2.9.tar.gz"
href="/download/nginx-1.0.15.tar.gz"
href="/download/nginx-0.8.55.tar.gz"
href="/download/nginx-0.7.69.tar.gz"
href="/download/nginx-0.6.39.tar.gz"
href="/download/nginx-0.5.38.tar.gz"
6.2 awk
6.2.1 awk入门讲解
grep偏向于行处理
awk偏向于列处理,会把一行分成多个列,awk同时也是一门编程语言,也有变量、数组、判断、循环等语法,awk内置了很多实用函数。awk默认如果没有过滤的化,会全部行数处理。如果没有动作的话,就会打印整行{print $0}
以下着重阐述:列处理、列过滤
示例文本如下:
[root@shell_test txt]# cat test2
sunwei1 28 hangzhou
sunwei2 30 shenzhen
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
zhangsan3 18 beijing
#1) awk简单使用,默认会使用空白字符隔成一列一列的
[root@shell_test txt]# cat test2 |awk '{print $1}' #打印姓名
sunwei1
sunwei2
sunwei3
zhangsan1
zhangsan2
zhangsan3#2) awk内置变量
$0 整行
$1 第一列
$2 第二列
$n 第n列
$NF 最后一列
NR 当前处理到第几行
[root@shell_test txt]# cat test2 |awk '{print NR,$3,$NF}'
1 hangzhou hangzhou
2 shenzhen shenzhen
3 shanghai shanghai
4 hangzhou hangzhou
5 foreign foreign
6 beijing beijing#3) awk打印多列、字符串拼接
cat test2 |awk '{print $1,$2}' #打印多列,默认中间加空格
[root@shell_test txt]# cat test2|awk '{print $1,$2}'
sunwei1 28
sunwei2 30
sunwei3 32
zhangsan1 33
zhangsan2 34
zhangsan3 18
cat test2 |awk '{print "name is "$1", ""age is "$2"."}' #拼接
注意:$1 $2这些不要用双引号引起来!!!!
[root@shell_test txt]# cat test2|awk '{print "name is "$1",""age is "$2}'
name is sunwei1,age is 28
name is sunwei2,age is 30
name is sunwei3,age is 32
name is zhangsan1,age is 33
name is zhangsan2,age is 34
name is zhangsan3,age is 18#4) awk针对列过滤,整数比较
number1 > number2
number1 < number2
number1 == number2
number1 >= number2
number1 <= number2
[root@shell_test txt]# cat test2 |awk '$2>20'
sunwei1 28 hangzhou
sunwei2 30 shenzhen
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
[root@shell_test txt]# cat test2 |awk '$2>30'
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
[root@shell_test txt]# cat test2 |awk '$2==30'
sunwei2 30 shenzhen
[root@shell_test txt]# cat test2 |awk '$1=="sunwei1"'
sunwei1 28 hangzhou#5) awk针对列过滤,使用正则
[root@shell_test txt]# cat test2 |awk '$3~/ei/'
zhangsan2 34 foreign
zhangsan3 18 beijing
[root@shell_test txt]# cat test2 |awk '$3~/(ei)|(ig)/'
zhangsan2 34 foreign
zhangsan3 18 beijing
[root@shell_test txt]# cat test2 |awk '$3~/(ei)&(ig)/'
None
[root@shell_test txt]# cat test2|awk '$2>30 {print $1}'
sunwei3
zhangsan1
zhangsan2
[root@shell_test txt]# cat test2|awk '$2>30 {print $2}'
32
33
34
[root@shell_test txt]# cat test2|awk '$2>30 {print $0}'
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
6.2.2 awk列处理常用知识
示例文本如下:
[root@shell_test txt]# cat test3
sunwei1 28 hangzhou
sunwei2 30 shenzhen
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
zhangsan3 18 beijing
#1) awk的BEGIN、ENDBEGIN{}大括号里: 只运行一次,在文本处理开始前运行{}大括号里: 针对每一行进行处理END{}大括号里: 只运行一次,在文本处理结束后运行
[root@shell_test txt]# cat test3|awk '{print $1} BEGIN{print "start...."} END{print "end...."}'
start....
sunwei1
sunwei2
sunwei3
zhangsan1
zhangsan2
zhangsan3
end....#2) BEGIN可以用来做浮点数运算
[root@shell_test txt]# awk 'BEGIN{printf("%.2f",5/3)}'
1.67#3) awk使用-F指定分隔符默认分隔符是以多个空白字符作为分隔:空格、tab-F参数可指定分隔符,后面跟正则表达式,支持扩展正则
[root@shell_test txt]# cat test3 |awk -F' ' '{print $3}'#4) awk多个单字符分隔
cat test3 |awk -F'[:;]' '{print $3}'
cat test3 |awk -F':|;' '{print $3}'#5) awk支持多字符分隔
[root@shell_test txt]# cat test3|awk -F':+|;' '{print $2}'
28
30
32
33
34
186) awk传参
[root@shell_test txt]# cat test3|awk -v age=30 '$2>age {print $0,$2}'
sunwei3 32 shanghai 32
zhangsan1 33 hangzhou 33
zhangsan2 34 foreign 34
[root@shell_test txt]# cat test3|awk -v age=30 '$2>age {print $0,age}'
sunwei3 32 shanghai 30
zhangsan1 33 hangzhou 30
zhangsan2 34 foreign 30
[root@shell_test txt]# cat test3 |awk -v age1=30 -v age2=20 '$2>age2 && $2<age1'
sunwei1 28 hangzhou
[root@shell_test txt]# cat test3 |awk -v age1=30 -v age2=20 '$2>age2 || $2<age1'
6.2.3 awk的判断语法
文本示例:
[root@shell_test txt]# cat test3
sunwei1 28 hangzhou
sunwei2 30 shenzhen
sunwei3 32 shanghai
zhangsan1 33 hangzhou
zhangsan2 34 foreign
zhangsan3 18 beijing
#数字判断条件
number1 > number2
number1 < number2
number1 == number2
number1 >= number2
number1 <= number2
#字符串简单判断
str1 == str2
str1 != str2#正则判断条件
str1 ~ /str2/[root@shell_test txt]# cat test3 |awk '{if($2>33){print $0;print "ok..."}else{print "sorry..."}}'
sorry...
sorry...
sorry...
sorry...
zhangsan2 34 foreign
ok...
sorry...
[root@shell_test txt]# cat test3 |awk '{if($3~/^hang/){print $0}}'
sunwei1 28 hangzhou
zhangsan1 33 hangzhou
6.2.4 awk数组循环语法
数组基础:
数字变量只能定义一个数,数组可以定义多个数
数组有索引和值,一般的索引是使用0、1、2递增
awk数组也支持使用字符串为索引,类似于字典
#1) 数组(类似于python列表)
numbers[0]=2
numbers[1]=4
numbers[2]=6
numbers[3]=8
echo ${numbers[0]} #第一个数
echo ${numbers[1]} #第二个数
echo ${numbers[*]} #所有数字
unset numbers #删除变量
numbers=(2 4 6 8)
echo ${numbers[0]}#2) 关联数组(类似于python字典)
[root@shell_test txt]# declare -A info
[root@shell_test txt]# info["name"]="sunwei"
[root@shell_test txt]# info["age"]=25
[root@shell_test txt]# echo ${info["name"]}
sunwei
[root@shell_test txt]# echo ${info["age"]}
25#3) awk循环语法
[root@shell_test txt]# awk 'BEGIN{var[0]=1;var[1]=2;var[3]=4;for(i in var){print i,var[i]}}'
0 1
1 2
3 4
#数字默认值为0
[root@shell_test txt]# awk 'BEGIN{var[0]++;var[0]++;var[1]--;for(i in var){print i,var[i]}}'
0 2
1 -1
awk数组字符串索引
[root@shell_test txt]# awk 'BEGIN{info["age"]=27;info["name"]="sunwei";for(i in info){print i,info[i]}}'
age 27
name sunwei
#统计每个城市的次数(此处使用sort、uniq),效率差,浪费cpu资源
[root@shell_test txt]# cat test3 |awk '{print $3}'|sort|uniq -c1 beijing1 foreign2 hangzhou1 shanghai1 shenzhen
#使用awk效率高
[root@shell_test txt]# cat test3 |awk '{count[$3]++}END{for(i in count){print i,count[i]}}'
foreign 1
shanghai 1
beijing 1
hangzhou 5
shenzhen 1
6.2.5 awk分析Nginx日志
[root@shell_test txt]# cat access.log |awk '{count[$1]++}END{for(ip in count){print ip"\t"count[ip]}}'|sort -nk 2
168.28.0.3 1
172.2.0.3 1
172.28.0.4 1
172.48.0.3 1
172.58.0.3 1
192.3.0.3 1
172.28.0.3 5
[root@shell_test txt]# cat access.log |awk '{count[$7]++}END{for(status in count){print status"\t"count[status]}}'
205 1
444 1
353200 1
400 3
324 1
200 3
500 1
[root@shell_test txt]# cat access.log | awk '{count[$7]++}END{for (status in count){print status"\t"count[status]/NR*100"%""\t"count[status]}}'
205 9.09091% 1
444 9.09091% 1
353200 9.09091% 1
400 27.2727% 3
324 9.09091% 1
200 27.2727% 3
500 9.09091% 1
[root@shell_test txt]# cat access.log | awk '{count[$7]++}END{for (status in count){print status"\t"int(count[status]/NR*100)"%""\t"count[status]}}'
205 9% 1
444 9% 1
353200 9% 1
400 27% 3
324 9% 1
200 27% 3
500 9% 1
#每分钟或者每秒或者每天的访问量
[root@shell_test txt]# cat access.log |awk '{print $3}'|awk -F'[' '{print $2}'|awk -F'+' '{print $1}'|awk '{count[$1]++}END{for(time in count){print time,count[time]}}'
31/aug2017:13:43 1
31/aug2017:13:44 9
31/aug2017:13:45 1
#nginx日志过滤
cat access.log|awk '$9~/^2/' #状态码,正常请求
cat access.log|awk '$9~/^5/' #状态码,处理异常
cat access.log |awk -F'"' '$(NF-1) ~ /iPhone/' #过滤含有iphone的ua
cat access.log|awl -F'"' '$(NF-1)~/IPhone/' #过滤含有iphone的ua
6.3 sed
6.3.1 sed文本操作入门
sed说明
vim可编辑文本,Shell脚本中无法使用vim
sed命令可对文本进行更改、删除、添加、打印,可以直接修改文本文件
示例文本文件如下:
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nouse
#1) sed语法
sed '过滤+动作' 文件路径#2) sed过滤(默认无过滤,每一行都处理)/^Port/ #正则过滤Port2,$ #指定行数,n代表第n行,$代表最后一行/PermitRootLogin/,/ListenAddress/ #包含PermitRootLogin的行开始,包含ListenAddress的行结束#3) sed动作p 打印动作a 在行下面添加i 在行上面添加d 删除s/str1/str2/g 全局查找替换,str1替换成st2s/str1/str2/ 查找替换,每行第一次出现的替换#4) sed打印动作,跟-n结合使用(只打印出匹配的行)
[root@shell_test txt]# cat test4 |sed '/Port/p' #此命令会将匹配的行打印两边,其他没有匹配到的行,也会打印出来,所以我们一般在使用sed进行过滤文本的时候,加上-n
Port 22 Port 22 Port 22
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
Port 22
[root@shell_test txt]# cat test4|sed -n '/^Port/p'
Port 22 Port 22 Port 22
Port 22
[root@shell_test txt]# cat test4|sed -r -n '/^Port+/p' #sed默认不支持扩展正则,需要加上-r才可以支持扩展正则
Port 22 Port 22 Port 22
Port 22
[root@shell_test txt]# cat test4|sed -n '3,5p'
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
[root@shell_test txt]# cat test4 |sed -n '/PermitRootLogin/,/sed/p'
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
[root@shell_test txt]# cat test4 |sed -n '/tLogin/,/sed/p'
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
#由上可以看出,sed过滤文本是以行为单位的#5) sed添加文本
[root@shell_test txt]# sed 'a 666' test4 #每一行下面添加”666“
Port 22 Port 22 Port 22
666
PermitRootLogin yes
666
Port 22
666
PasswordAuthentication yes
666
[root@shell_test txt]# sed 'i 666' test4 ##每一行上面添加”666“
666
Port 22 Port 22 Port 22
666
PermitRootLogin yes
666
Port 22
666
PasswordAuthentication yes
[root@shell_test txt]# sed 'i \ 666' test4 #需要增加空格,使用反斜杠666
Port 22 Port 22 Port 22666
PermitRootLogin yes666
Port 22
[root@shell_test txt]# sed '/sedsedsed/a \ sunwei add' test4 #过滤加动作,找出包含”sedsedsed“的一行,并且在其下面添加” sunwei add“
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsedsunwei add
//note
nousePort[root@shell_test txt]# sed -i '/^ sunwei/d' test4 #过滤后删除
[root@shell_test txt]# cat test4
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort
6.3.2 sed文本替换以及应用
示例文本如下:
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nouse
Port
#1) sed普通替换
[root@shell_test txt]# cat test5|sed 's/22/1111/g' #全局替换
Port 1111 Port 1111 Port 1111
PermitRootLogin yes
Port 1111
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort
[root@shell_test txt]# cat test5|sed 's/22/1111/' #匹配行的第一次出现替换
Port 1111 Port 22 Port 22
PermitRootLogin yes
Port 1111
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort#2) sed正则替换,支持普通正则和扩展正则(-r)
[root@shell_test txt]# cat test5|sed 's/ListenAddress.*/ListenAddress 192.168.1.1/g' #普通正则
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 192.168.1.1
sedsedsed
//note
nousePort
[root@shell_test txt]# cat test5|sed -r 's/(sed)+/111/g' #或者使用[root@shell_test txt]# cat test5|sed -r 's/(sed){3}/111/g'
Port 22 Port 22 Port 22
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
111
//note
nousePort#3) sed分隔符可以有多种选择,一般都用/
[root@shell_test txt]# cat test5|sed 's#22#444#g'
Port 444 Port 444 Port 444
PermitRootLogin yes
Port 444
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort#4) sed过滤加替换
[root@shell_test txt]# cat test5|sed '1,2s/2/5/g' #过滤加替换
Port 55 Port 55 Port 55
PermitRootLogin yes
Port 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort
[root@shell_test txt]# cat test5|sed '/^Port/s/Port/PORT/g' #正则过滤加替换
PORT 22 PORT 22 PORT 22
PermitRootLogin yes
PORT 22
PasswordAuthentication yes
ListenAddress 10.175.201.36
sedsedsed
//note
nousePort#5) sed支持直接更改文本文件,使用-i选项
sed 's/22/1111/g' test5
sed -i 's/22/1111/g' test5
sed -i '/PermitRootLogin/a Permitsunwei' test5
注意:使用sed -i选项进行文本编辑时,记得先进行备份! 使用diff -u file1 file2 或者 vimdiff file1 file2进行文本内容比对6) 实战:使用sed过滤出nginx版本包
nginxdownloadurl=".14.1.tar.gz"
[root@shell_test shell]# echo $nginxdownloadurl|sed 's#.*/##'
nginx-1.14.1.tar.gz
7) 修改mfschunkserver.cfg文件中配置(直接锁定到那一行并进行修改)
sed '71s/#//' -i /usr/local/mfs/etc/mfs/mfschunkserver.cfg
7. find命令
7.1 find命令查找入门
find命令用来搜索指定文件
搜索到指定文件后可执行某些动作,例如rm操作
文件准备:
mkdir /tmp/sunwei;cd sunwei
[root@shell_test sunwei]# for line in KaTeX parse error: Expected group after '_' at position 23: …);do touch file_̲line;mkdir dir_$line;done
[root@shell_test sunwei]# ls
dir_1 dir_2 dir_4 dir_6 dir_8 file_1 file_2 file_4 file_6 file_8
dir_10 dir_3 dir_5 dir_7 dir_9 file_10 file_3 file_5 file_7 file_9
ln -s /tmp/sunwei/file_10 /tmp/sunwei/file_link
touch -d “365 days ago” file_9
chown nobody:nobody file_8
#1) find语法find 目录 选项 动作#2) find选项说明选项可按文件类型、更改时间、名字等进行查找无选项默认全查找#3) find动作说明动作默认print,输出查找到的文件路径动作可以自定义
根据文件类型查找-type
f 普通文件 file
d 目录 directory
l 链接文件
b 块设备文件
c 字符设备文件
p 管道文件
find /tmp/sunwei -type f
find /tmp/sunwei -type l
find根据文件名字查找
find /tmp/sunwei -name "file_*" #只支持通配符
根据文件的用户、用户组来查找
find /tmp/sunwei -type f -user nobody
find /tmp/sunwei -type f -group nobody#4) find反向查找
find /tmp/sunwei/ -type f ! -user nobody
7.2 find文件查找加动作
#1) linux文件时间,可用stat查看atime #access time访问时间mtime #modify time修改时间,比较常用。内容修改更新这个时间ctime #change time,包含内容修改或者属性修改(文件属主、文件权限)#2) atime不生效原因
由于atime频繁更改会对文件io产生影响,因此很多线上系统atime的修改是禁止的。挂载的时候使用noatime /etc/fstab#3) 根据修改时间查找
find /tmp/sunwei -type f -mmin +3 #3分钟前修改
find /tmp/sunwei -type f -mtime +6 #6天前修改的
find /tmp/sunwei/ -type f -newer file_6 #相对文件时间查找
find /tmp/sunwei/ -type f -newermt '2018-11-25 13:15:00' #绝对时间查找#4) find结合xargs
find /tmp/sunwei/ -type f -newer file_6 -print | xargs rm
find /tmp/sunwei/ -type f -print0|xargs -0 grep 'sunwei'
find结合xargs存在问题
touch '/tmp/sunwei/blank sunwei'
find /tmp/sunwei -type f -name "blank*"
find /tmp/sunwei -type f -name "blank*" | xargs ls -l #报错,因为默认有空格作为分隔符
find /tmp/sunwei/ -type f -name "blank*" -print0 |xargs -0 ls -l #使用-print0和-0代表以\0作为分隔符
8. curl命令
阅读博客:.html
是通过url语法在命令行下上传或下载文件的工具软件,它支持http,https,ftp,ftps,telnet等多种协议,常被用来抓取网页和监控Web服务器状态。
语法: curl [option] [url]
#1) 基本用法
获取页面内容:当我们不加任何选项使用 curl 时,默认会发送 GET 请求来获取链接内容到标准输出。
curl #2) 显示 HTTP 头
如果我们只想要显示 HTTP 头,而不显示文件内容,可以使用 -I 选项:
curl -I
输出为:
[root@shell_test example_shell]# curl -I
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Thu, 18 Jul 2019 14:37:51 GMT
Content-Type: text/html
Content-Length: 277
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Connection: Keep-Alive
ETag: "575e1f6f-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes
也可以同时显示 HTTP 头和文件内容,使用 -i 选项:
curl -i #3) 把链接页面的内容输出到本地文件中
curl > index.html
也可以通过 curl 自带的 -o/-O 选项将内容保存到文件中
- -o(小写的 o):结果会被保存到命令行中提供的文件名
- -O(大写的 O):URL 中的文件名会被用作保存输出的文件名
curl -o index.html
curl -O .html #结果,文件名为857.html 的文件里保存着内容
###注意:使用 -O 选项时,必须确保链接末尾包含文件名,否则 curl 无法正确保存文件。如果遇到链接中无文件名的情况,应该使用 -o 选项手动指定文件名,或使用重定向符号。#4) 同时下载多个文件
我们可以使用 -o 或 -O 选项来同时指定多个链接,按照以下格式编写命令:
curl -O / -O /
curl -o page1.html / -o page2.html ) 测试网页返回值
curl -o /dev/null -s -w %{http_code} www.linux
9. lsof命令
转载自:.html
10. 怎么找出进程号
阅读博客:
- ps -ef |grep 进程名
- pidof 进程名
11.uniq
- 使用uniq 命令删除重复的行
uniq /etc/passwd - 检查文件并删除文件中重复出现的行,并在行首显示该行重复出现的次数
uniq -c testfile - 当重复的行并不相邻时,uniq 命令是不起作用的,即若文件内容为以下时,uniq 命令不起作用
$ cat testfile1 # 原有内容
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
test 30
Hello 95
Linux 85
这时我们就可以使用 sort:
$ sort testfile1 | uniq
Hello 95
Linux 85
test 30
- 统计各行在文件中出现的次数:
$ sort testfile1 | uniq -c3 Hello 95 3 Linux 85 3 test 30
- 在文件中找出重复的行:
$ sort testfile1 | uniq -d
Hello 95
Linux 85
test 30
12. sort
13. cut
14. 系统性能分析
14.1. sar
14.2. top
14.3. vmstat
14.4 free
15. yum
15.1. yum provides 查看命令的安装包
更多推荐
Linux下命令总结
发布评论