linux 三剑客

sed

grep

awk

find

 

问题:删除3天前创建的文件

案例:删除系统中30天内没有访问过的文件

案例:备份文件仅仅保留30天

案例:通apache的访问日志中每个IP的出现次数

 

## 一、sed命令

**案例1:将httpd.conf中 #serverName www.test.com 中前买的#去掉**

- 选项:-i:直接修改文件内容【默认仅仅修改内存数据】

```shell
方法1
[root@master ~]# sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' httpd.conf

方法2
[root@master ~]# sed -i 's/#\(ServerName www.example.com:80\)/\1/g' b.txt
```

**案例2:将a.txt中行首的空格去掉**

```shell
方法1
[root@master ~]# sed -i "s/^ //g" e.txt
方法2
[root@master ~]# sed -i "s/^[[:space:]]//g" e.txt
```

**案例3:将a.txt以#为开头,且第二个字符是空格,从第三个字符开始为非空号这样的行中的前面的#和空格去掉**

```shell
[root@master ~]# sed -i 's/^# //g' e.txt
```

 

## 二、grep命令

- 过滤字符串
- 过滤单词
- \\< 单词首部
- \\> 单词尾部

- 表示方式
- [[:space:]] 表示一个空格
- [0-9]或[[:digit:]] 表示任意一个数字
- [a-z]或[[:lower:]] 表示任意一个小写字母
- [A-Z]或[[:upper:]]表示任意一个小写字母

**案例:从a.txt中过滤出包含root单词的行**

```shell
方法1
[root@master ~]# grep "\<root\>" a.txt

方法2
[root@master ~]# grep -w "root" a.txt
```

**案例:将a.txt中#开头的行和空白去掉**

```shell
方法1
[root@master ~]# cp httpd.conf httpd.conf.bak
[root@master ~]# grep -E -v "^($|[[:space:]]*#)" httpd.conf.bak>httpd.conf

方法2
[root@master ~]# cp httpd.conf httpd.conf.bak
[root@master ~]# grep -v "^[[:space:]]*#" httpd.conf.bak | grep -v "^$" >httpd.conf
```

 

## 三、awk命令

- 数组
- 模块
- BEGIN
- END

**BEGIN模块**

- 用于定义一个动作,用{}表示要执行的动作
- 这个动作要在读取文件之前执行
- 这里的动作大多用于定义变量,包括内置变量、自定义变量

**END模块**

- 用于定义一个动作,用{}表示要执行的动作
- 这个动作是awk将文件中的内容读取完成之后才会执行
- 这里的动作通常用于输出一个结束

 

**案例:输出每个用户的用户名、id、shell**

```SHELL
[root@ansible tmp]# awk -F ":" '{print $1,$3,$NF}' passwd
[root@ansible tmp]# awk 'BEGIN{FS=":"}{print $1,$3,$NF}' passwd
```

**案例:输出前三个用户的用户名、id、shell**

```
[root@ansible tmp]# awk 'BEGIN{FS=":"}NR<=3{print $1,$3,$NF}' passwd
```

**注意:通常每种都有默认值**

- FS默认值就是空格
- OFS默认值就是空格
- ORS默认是换行符
- RS默认是换行符

**案例:在读取文件前输出一个提示信息**

```
[root@ansible tmp]# awk 'BEGIN{print "start to awk process..."}{print $0}' passwd

```

**案例:在读取文件前和后分别输出一个提示信息**

```
[root@ansible tmp]# awk 'BEGIN{print "start to awk process..."}NR<=3{print $0}END{print "over..."}' passwd
```

**案例 :统计文件中的空白行的行数**

```shell
[root@ansible tmp]# awk 'BEGIN{num=0}/^$/{num+=1}END{print num}' c.txt
4
[root@ansible tmp]# awk '/^$/{num+=1}END{print num}' c.txt
4
```

**案例 :统计文件中以#开头的行的行数**

```shell
[root@ansible tmp]# awk 'BEGIN{num=0}/^#/{num+=1}END{print num}' c.txt
4
[root@ansible tmp]# awk '/^#/{num+=1}END{print num}' c.txt
4
```

**案例:统计系统中uid大于499的用户个数**

```
[root@ansible tmp]# awk 'BEGIN{num=0}$3>499{num+=1}END{print num}' passwd
4
```

**案例:计算1~100的累加和**

```
[root@ansible tmp]# seq 100 | awk 'BEGIN{sum=0}{sum+=$0}END{print sum}'
5050
[root@ansible tmp]# seq -s + 100 | bc
```

 

**简单案例**

```
[root@ansible tmp]# awk 'BEGIN{stu[0]="tom";stu[1]="jerry";stu[2]="jack";print stu[1]}'
jerry
[root@ansible tmp]# awk 'BEGIN{stu[0]="tom";stu[1]="jerry";stu[2]="jack";print stu[2]}'
jack
[root@ansible tmp]# awk 'BEGIN{stu["name"]="tom";stu["age"]="20";stu["tel"]="13011111";print stu["age"]}'
20
[root@ansible tmp]# awk 'BEGIN{stu["name"]="tom";stu["age"]="20";stu["tel"]="13011111";print stu["tel"]}'
13011111

```

 

**for(变量 in 数组名)**

- 变量中存储的是属组的索引

```shell
[root@ansible ~]# awk 'BEGIN{stu["name"]="tom";stu["age"]="20";stu["tel"]="130111"}END{for(key in stu)print key,stu[key]}' /etc/passwd
age 20
name tom
tel 130111
[root@ansible ~]# awk 'BEGIN{stu[0]="tom";stu[1]="jerry";stu[2]="jack"}END{for(key in stu)print key,stu[key]}' /etc/passwd
0 tom
1 jerry
2 jack
```

 

**案例:**
http://www.baidu.com/index.html
http://mail.baidu.com/index.html
http://ftp.baidu.com/index.html
http://ftp.baidu.com/index.html
http://www.baidu.com/index.html
http://ftp.baidu.com/index.html
http://mail.baidu.com/index.html
http://mail.baidu.com/index.html
http://www.baidu.com/index.html
http://ftp.baidu.com/index.html
http://www.baidu.com/index.html
http://www.baidu.com/index.html
http://ftp.baidu.com/index.html
http://ftp.baidu.com/index.html

**获取每个域名的出去次数**

```
[root@ansible ~]# awk -F "/+" '{list[$2]++}END{for(site in list)print site,list[site]}' web.log
ftp.baidu.com 6
www.baidu.com 5
mail.baidu.com 3
```

**基于域名的出现次数进行排序**

```shell
方法1
[root@ansible ~]# awk -F "/+" '{list[$2]++}END{for(site in list)print site,list[site]}' web.log | sort -n -k2 -r
ftp.baidu.com 6
www.baidu.com 5
mail.baidu.com 3

方法2
[root@ansible ~]# awk -F "/+" '{print $2}' web.log | sort |uniq -c | sort -k1 -n
3 mail.baidu.com
5 www.baidu.com
6 ftp.baidu.com
```

 

## 四、find命令

1:找出系统中全部用户都有执行权限的文件

find / -perm -111

 

2:找出系统中30天没有被访问过的文件

find / -atime +30

 

**支持两种方式**

- 精确查找
- 模糊模糊

**精确查找**

- -perm mode

**案例:从/tmp下找文件的全系统为644的文件**

```shell
[root@ansible ~]# find /tmp -perm 644
```

 

mode 9个权限位,必须要全部一致,就认为匹配成功

/mode 9个权限位,只要一位匹配,就认为匹配成功

-mode 目标文件必须包含9个权限位中出现的权限,就认为匹配成功

/444 找任意用户有读取全校性的文件

/111 找任意用户有执行权限的文件

r-- r-- r-- 找其他用户有读取权限的文件

A r-x --x r--

 

 

**模糊查找1**

- -perm /mode

- 文件的9个权限位只要任意一个位置与指定的权限一样,就表示匹配成功

**案例:从tmp下找出任意用户有执行权限的文件**

```shell
[root@ansible ~]# find /tmp -perm /111
```

**案例:从tmp下找出属主用户具有任意权限的文件**

```shell
[root@ansible ~]# find /tmp -perm /700
```

**模糊查找2**

- -perm -mode
- 文件的9个权限位必须包含指定的权限

**案例:从tmp下找出所有用户有执行权限的文件**

```shell
[root@ansible ~]# find /tmp -perm -111
```

**案例:从tmp下找出属主用户有读和写**

```shell
[root@ansible tmp]# find /tmp -perm -600
```

 

 

 

 

**1)查找/var/下属主为root且属组为mail的所有文件**

```shell
# find /var -user root -a -group mail
```

**2、查找/usr下不属于root、bin或hadoop的所用文件**

```shell
# find /usr -not -user root -a -not -user bin -a -not -user hadoop
# find /usr -not \(-user root -o -user bin -o user hadoop\)
```

3)查找/etc/下最近一周内内容修改过的,且不属于root或hadoop的文件

```shell
# find /etc -ctime -7 -a -not \(-user root -o -user hadoop\)
```

4、查找当前系统上没有属主或属组,且最近1个月内曾被访问过的文件

``` shell
# find / \(-nuser -o -ngroup\) -a -atime -30
```

5)查找/etc/目录下大于1M且类型为普通文件的所有文件

```shell
# find /etc -size +1M -a -type f
```

6)查找/etc/目录所有用户都没有写权限的文件

```shell
# find /etc/ -not -perm /222
```

7)查找/etc/目录下至少有一类用户没有写权限

```shell
# find /etc -not -perm -222
```

8)查找/etc/init.d/目录下,所有用户都有执行权限且其它用户有写权限的文件

```shell
# find /etc/init.d -perm -113
```

 

案例:将/tmp下后缀是t.xt的文件删除掉

find /tmp -name "*txt" -exec rm -rf {} \;

 

案例:将tmp下后缀是txt的文件复制到home下

find /tmp -name "*txt" -exec cp {} /home \;

 

posted @ 2020-07-21 10:20  fives  阅读(171)  评论(0编辑  收藏  举报