四剑客find及命令补充

四剑客find及命令补充

1. 四剑客-grep

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

补充:四剑客 awk、sed、grep、find

1.1 概述与选项

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

1.2 案例与应用

1)基本用法

## 在/etc/passwd中过滤出包含root的行
grep '你要找的内容' /etc/passwd
grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

[root@oldboy-lnb-king-v3 ~]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  • grep 也可以对接管道
过滤出叫crond的进程
ps -ef |grep 'crond'

[root@oldboy-lnb-king-v3 ~]# ps -ef | grep crond
root      1013     1  0 11月29 ?      00:00:00 /usr/sbin/crond -n
root     63709 62724  0 09:07 pts/1    00:00:00 grep --color=auto crond

其他过滤案例:

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

2)显示内容和行号

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

##在/etc/services中过滤出包含ssh的行并显示行号
[root@oldboy83-prod oldboy]# grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

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

过滤的时候不清楚内容是大写还是小写。
## 案例 02 过滤出secure文件中failed password的行不区分大小写
grep -i 'failed password' /var/log/secure


[root@oldboy83-prod oldboy]# grep -i 'failed password' /var/log/secure
20 15:31:46 oldboy83-prod sshd[17003]: Failed password for root from 10.0.0.1 port 53920 ssh2
20 15:31:46 oldboy83-prod sshd[17003]: Failed password for root from 10.0.0.1 port 53920 ssh2

温馨提示:如果没有Failed password的日志,可以使用ssh root@10.0.0.200进行连接测试,输错密码即可生成相关日志。

4)排除

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

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

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

2. 四剑客-find

四剑客四号选择,擅长查找文件。在指定的目录中查找你要的文件,文件名。

2.1 概述与选项

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

2.2 基础案例

find 目录 指定类型 指定名字

1)案例01-02 精确查找与模糊查找

#案例01 在/etc/目录中找出文件名叫hostname文件 ,精确查找,指定文件名。
find /etc/   -type f   -name 'hostname' /etc/hostname

#案例02 找出/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)案例03 根据大小查找

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

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

3)案例04 根据时间查找

一般是根据文件的修改时间进行查找.
未来主要用于查找系统日志文件,旧的日志文件。7天之前的文件。

-mtime选项:
• +7表示 找出7天之前的文件(修改时间是7天之前)
• -7表示 最近7天内的文件

#案例04  根据修改时间找出文件
根据相对的时间进行查找,比如
修改时间最近7天的文件
修改时间是大于30天

#找出/etc/目录下以.conf结尾的,7天之前的文件。
find /etc/ -type f -name "*.conf" -mtime +7

find /oldboy/ -type f -mtime -7 #最近7天(包含第 7 天)
find /oldboy/ -type f -mtime +7 #7天之前的(不包含第 7 天)

4)案例05 综合案例

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

[root@oldboy83-prod ~]# ls -lh  /etc/lvm/lvm.conf
-rw-r--r--. 1 root root 94K 10月  1 2020 /etc/lvm/lvm.conf

5)案例06-07: 进阶选项

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

#案例07  查找的时候不区分文件名的大小写
find /    -type f -iname "*.conf"
#ignore  case

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

2.3 find命令与其他命令配合(难点)

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

1)案例08-找出 /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 /oldboy/find/  -type f -name '*.txt' | ls -lh
##执行失败:管道传递的是字符串
##管道 | 的作用是把前一个命令的标准输出(字符串)传递给后一个命令的标准输入。但 ls 命令是通过命令行参数来接收文件路径的,它不会读取标准输入里的字符串,因此 find 输出的文件路径会被直接忽略。

##如何解决:
##通过|xargs把前面命令传递过来的 字符串 转换为 后面命令可以识别的 参数.
find /oldboy/find/  -type f -name '*.txt' |xargs ls -lh

注:温馨提示: |xargs后面无法使用 ll,

xargs 默认会调用系统原生的命令(如 /bin/ls),而不是你在 Shell 中定义的别名(比如你把 ll 设为 ls -l 的别名)。

因此,直接写 ... | xargs ll 会报错,因为 xargs 找不到名为 ll 的系统命令。

#如果想在 xargs 中使用别名,需要显式调用 Shell 来解析:
# 正确写法
find /oldboy/find/ -type f -name '*.txt' | xargs bash -c 'll "$@"' _

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

2)案例09-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 \:找到一个文件,就执行一次压缩的命令

#-exec {} + :把所有找到的文件一次性传给后面的命令,只执行一次 tar
find /oldboy/find/ -type f -name '*.txt' -exec tar zcf /tmp/find-exec.tar.gz {} +

##注意:-exec 命令 {} \; ,必须加 \;(分号需要转义)
##-exec 命令 {} + , 不加 ;

3)案例10-find与复制或移动

  • find找出/oldboy/find/ 以.txt结尾的文件然后复制到/tmp下面
##方法01 find + ``
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 源文件 目标目录,顺序反了
#xargs 的默认行为:把管道传来的参数(文件列表)加到命令最后。

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' -exec cp {} /tmp/ \;
posted @ 2026-01-20 21:47  gzjwo  阅读(2)  评论(0)    收藏  举报