四剑客

1. 四剑客-grep

  • 过滤:在文件中或管道中进行查找,找出想要的内容(字符串),默认按照行查找
  • grep会把匹配到的行显示出来.

补充:四剑客

awk

sed

grep

find

1.1 概述与选项

grep 选项 说明
- n line-number 显示行号
- v 排除,取反
- i ignore-case 过滤的时候忽略大小写

1.2 案例与应用

1. 基本用法

[root@myvps ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@myvps ~]# 

image

  • grep 也可以对接管道
过滤出叫crond的进程
ps -ef | grep 'crond'

image

其他过滤案例:

在/var/log/secure中过滤出包含Failed password的行 在/var/log/secure中过滤出包含Failed password的行并统计次 数

2. 显示内容和行号

# 显示/etc/passwd中包含root行及行号
grep -n 'root' /etc/passwd

在/etc/services中过滤出包含ssh的行并显示行号
grep -n 'ssh' /etc/passwd

image

3. 过滤的时候不区分大小写

过滤的时候不清楚内容是大写还是小写.

# 过滤出secure文件中failed password的行不区分大小写
grep -i  'failed password' /var/log/secure

4. 排除

查找的时候,不知道具体要什么,但是你知道你不想要什么?

这时候需要使用grep命令的排除选项,过滤出不包含xxx内容的行

# 排除/etc/passwd 中的nologin的行
grep -v nologin ' /etc/passwd

5. 一个网站的由来

一个网站由来:

  1. 公有云服务器:搭建各种环境

  2. 域名

  3. 备案

2.1 概述与选项

  • find 在指定目录中查找文件
find命令选项 说明
-typ 什么类型的文件 f表示文件 d表示目录
-name 文件名
-size 根据大小查找文件 +表示大于 -表示小于 +10k(小写K)+10M(大写) G
-mtime 根据修改时间查找文件

1. 精确查找与模糊查找

# 在/etc/目录中找出文件名叫hostname文件
精确查找,指定文件名
[root@myvps ~]# find /etc/ -type f -name 'hostname'
/etc/hostname

# 找出/etc/下面以.conf结尾的文件
a.conf b.conf c.conf lidao.conf xxxx.conf
find /etc/ -type f -name '*.conf'

温馨提示: 星号: * 表示任意符号或所有

温馨提示:如果想找以xxx开头或包含xxx的文件

找以oldboy开头的文件: -name 'oldboy*'

找包含oldboy的文件: (可能是开头,可能是结尾,可能是中 间)

-name 'oldboy'

可以让find+grep就行:find / -type f |grep 'oldboy' (可能是开头,可能是结尾,可能是中间,路径中包 含)

2. 根据大小查找

-size选项,size大小的意思.根据大小查找文件

大于 使用+加号 -size +10k 大于10k文件

小于 使用-减号 -size -10k 小于10k文件

# 根据大小找出文件 在/etc/目录下面找出大于10kb的文件
find /etc/ /tmp/  -type  f  -size +10k

3. 根据时间查找

一般是根据文件的修改时间进行查找

未来主要用于查找系统日志文件,旧的日志文件。7天之前的文件。

-mtime选项:

  • +7表示 找出7天之前的文件(修改时间是7天之前)
  • -7表示 最近7天内的文件
# 根据修改时间找出文件
根据相对的时间进行查找,比如
修改时间最近7天的文件
修改时间是大于30天
# 找出/etc/目录下以.conf结尾的,7天之前的文件。
find /etc/ -type f -name "*.conf" -mtime +7

find /oldboy/ -type f  -mtime -7 #最近7天
find /oldboy/ -type f  -mtime +7 #7天之前的

image

4. 综合案列

# 找出/etc/中以.conf结尾大于10kb修改时间是7天之前的文件
find /etc/ -type f -name '*.conf' -size +10k -mtime +7
/etc/lvm/lvm.conf
[root@myvps ~]# find /etc/ -type f -name '*.conf' -size +10k -mtime +7
/etc/lvm/lvm.conf
[root@myvps ~]# 

5. 进阶选项

# 查找文件的时候指定最多找多少层目录
find / -maxdepth 2  -type f -name "*.conf"
-maxdepth 1 选项位置第1个,指定find命令查找的最大深度层数),不加上就是所有层。
# 查找的时候不区分文件名的大小写
find /   -type f -iname "*.conf"
#ignore case 

find还可以根据用户名查找,查找属于某个用户的文件。

find还可以根据权限查找。

2.2 find命令与其他命令配合

这部分是find的难点

  • 案例:
    • 核心:find+简单命令:find找出想要的文件删除,看详 细信息,显示文件内容,过滤。
    • find+打包压缩:find找出文件进行打包压缩
    • find+cp/mv:find找出文件后复制或移动
环境准备
mkdir -p /oldboy/find
touch /oldboy/find/lidao{0110}.txt
cd

1. 找出/oldboy/find/以.txt结尾的文 件显示详细信息

这里主要用到find+ls配合,后面的其他配合find+rm, find+cat/head/tail/,find+grep都类似。

find不要与交互式命令配合find+vi/vim ❌

  • 方法01 find ``

find与其他命令配合必会方法 find + 反引号+其他命令

ls -lh find命令的结果
ls -lh `find /oldboy/find/ -type f -name'*.txt'`
ls -lh $(find /oldboy/find/ -type f -name'*.txt')
先运行find命令找出文件,然后运行ls显示文件详细信息。

  • 方法02 find | xargs

我们发现find命令使用管道把数据传输给其他命令失败了!!!

find /oldboy/find/ -type f -name "*.txt" | ls -l 无法使用。

关于管道的秘密:

find /oldboy/find/ -type f -name '*.txt' | ls -lh

ls 选项 参数

错误的心路历程:

默认管道是无法把数据变化为命令的参数,导致传递失败,find 命令找出的内容相当于被丢弃了,就相当于执行ls -lh命令,显示当 前目录下内容并详细信息。

故障原因:

前面的命令通过管道传递给后面命令,传递的是 字符串 .

这个命令(ls)中传递文字符号就不行,传递 参数

所以上面的命令相当于find白白浪费了,仅仅执行了下ls -lh而 已 .

如何解决:

通过 | xargs把前面命令传递过来的字符串转换为后面命令可以识 别参数.

效果展示:find /oldboy/find/  -type f  -name '*.txt' |xargs ls -lh


[root@myvps ~]# find /oldboy/find/ -type f -name '*.txt'|xargs ls -lh
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang01.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang02.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang03.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang04.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang05.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang06.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang07.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang08.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang09.txt
-rw-r--r--. 1 root root 0 8月  17 01:20 /oldboy/find/yang10.txt
[root@myvps ~]# find /oldboy/find/ -type f -name '*.txt'|xargs ll -h
xargs: ll: 没有那个文件或目录
[root@myvps ~]# 


温馨提示: |xargs后面无法使用 ll,后面讲解别名的时候会详解.

  • 方法03 find + -exec 方法 了解
find /oldboy/find/ -type f -name '*.txt'   -exec ls -lh {} \;
-exec是find选项,表示find找出文件后要执行的命令
{} 表示前面find命令找出的文件.
\;表示命令结束,固定格式.

3.3 find与打包压缩

  • find找出/oldboy/find/ 以.txt结尾的文件放 在/tmp/find.tar.gz

  • 方法01 - find ``

tar zcf /tmp/find.tar.gz  `find /oldboy/find/ -type f -name '*.txt'`
  • 方法02 find + |xargs
find /oldboy/find/  -type f  -name '*.txt'|xargs tar zcf /tmp/etc-xargs.tar.gz

  • 方法03 find + exec
find /oldboy/find/  -type f  -name '*.txt'  -exec
tar zcf /tmp/find-exec.tar.gz {} \;
# 有坑,因为-exec \;执行方式 1个文件1个文件的压缩.


find /oldboy/find/  -type f  -name '*.txt'  -exec
tar zcf /tmp/find-exec.tar.gz {}  +

4.4 find与复制或移动

  • find找出/oldboy/find/ 以.txt结尾的文件然后复制到/tmp 下面

  • **方法01 find + **

cp 源 .......   目标
cp `find /oldboy/find/ -type f -name '*.txt'`/tmp/
  • 方法02 find + |xargs
find xxx   |xargs cp /tmp/
运行的时候
1. find找出文件 oldboy01  oldboy10 
2. 把10个文件传输给cp命令
3. cp /tmp/ oldboy01  oldboy10

cp -t /tmp/ oldboy01  oldboy10  
cp -t 目标   源 文件 目录 ..... 
如何解决
find /oldboy/find/  -type f  -name '*.txt'|xargs cp -t /tmp/


  • 方法03 find + -exec
find /oldboy/find/  -type f  -name '*.txt' -execcp {} /tmp/ \;

image

posted @ 2024-11-05 22:31  殇ベ墨~  阅读(56)  评论(0)    收藏  举报