目录
2.5.4 查找 /home/ 下所有以.txt或.pdf结尾的文件
2.5.5 查找 /home/ 下所有以a开头和以.txt结尾的文件
2.5.6 搜索/home目录下txt结尾的文件,并将输出到指定文件中(re.txt)
2.7.2. 搜索出深度距离当前目录至少2个子目录的所有文件
2.10.3. 找出/home目录用户frank拥有的所有文件
2.10.4. 找出/home目录用户组frank拥有的所有文件
1.举例
遍历当前目录下所有Makefile文件,找到内容适配CONFIG_SMP的所有Makefile
find -type f -name 'Makefile' | xargs grep "CONFIG_SMP"
2.find命令
2.1. find命令作用
find命令用来在指定目录下查找文件。
2.2. find命令选项基本格式
find 目录 -选项 动作[-print -exec -ok ...]
2.3. 常用选项
-a:and 必须满足两个条件才显示
-o:or 只要满足一个条件就显示
-name:按照文件名查找文件
-iname:按照文件名查找文件(忽略大小写)
-type:根据文件类型进行搜索
-perm:按照文件权限来查找文件
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-fprint 文件名:将匹配的文件输出到文件。
-newer file1 ! newer file2 查找更改时间比文件file1新但比文件file2旧的文件
2.4. 常用动作
-print 默认动作,将匹配的文件输出到标准输出
-exec 对匹配的文件执行该参数所给出的命令。相应命令的形式为 'command' { } \;,注意{ }和\;之间的空格。
-ok 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
-delete 将匹配到的文件删除
2.5. 根据文件名进行匹配
命令:find .
[root@host-134 ~]# find .
.
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.mysql_history
./zuoye
./zuoye/lnmp.sh
./zuoye/system.sh
./nginx-1.18.0
./nginx-1.18.0/auto
./nginx-1.18.0/auto/cc
./nginx-1.18.0/auto/cc/acc
./nginx-1.18.0/auto/cc/bcc
......
2.5.2 在/home目录下查找以.txt结尾的文件名
find /home/ -name "*.txt"
2.5.3 同上,但忽略大小写
find /home -iname "*.txt"
2.5.4 查找 /home/ 下所有以.txt或.pdf结尾的文件
find /home/ -name "*.txt" -o -name "*.pdf"
2.5.5 查找 /home/ 下所有以a开头和以.txt结尾的文件
find /home/ -name "*.txt" -a -name "a*"
2.5.6 搜索/home目录下txt结尾的文件,并将输出到指定文件中(re.txt)
[root@localhost home]# find /home/ -type f -name "*.txt" -fprint /tmp/re.txt
[root@localhost home]# cat /tmp/re.txt
/home/a.txt
/home/b.txt
2.6. 根据文件类型进行搜索
-type 类型参数
类型参数列
f 普通文件
l 符号连接(软连接)
d 目录
b 块设备
s 套接字
2.6.1. f 普通文件
[root@host-136 ~]# find /home/ -type f
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
2.6.2. l 符号连接(软连接)
[root@host-136 ~]# find /usr/bin/ -type l
/usr/bin/bashbug
/usr/bin/lastb
/usr/bin/sh
/usr/bin/geqn
/usr/bin/ex
/usr/bin/lz4cat
/usr/bin/gneqn
/usr/bin/gnroff
2.6.3. d 目录
[root@host-136 ~]# find /usr/local/ -type d
/usr/local/
/usr/local/bin
/usr/local/etc
/usr/local/games
/usr/local/include
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
/usr/local/sbin
2.6.4. b 块设备
[root@host-134 ~]# find /dev/ -type b
/dev/dm-1
/dev/dm-0
/dev/sda2
/dev/sda1
/dev/sda
/dev/sr0
2.6.5. s 套接字
[root@localhost home]# find /var/lib/ -type s
/var/lib/mysql/mysql.sock
2.7. 基于目录深度搜索
2.7.1. 向下最大深度限制为3
[root@host-136 ~]# find /usr/local/ -maxdepth 3 -type d
/usr/local/
/usr/local/bin
/usr/local/etc
/usr/local/games
/usr/local/include
/usr/local/lib
/usr/local/lib64
/usr/local/libexec
/usr/local/sbin
/usr/local/share
/usr/local/share/applications
/usr/local/share/info
/usr/local/share/man
/usr/local/share/man/man1
/usr/local/share/man/man1x
2.7.2. 搜索出深度距离当前目录至少2个子目录的所有文件
[root@host-136 ~]# find /usr/local/ -mindepth 2 -type f
/usr/local/sbin/mail.py
2.8. 根据文件时间戳进行搜索
2.8.1. 根据文件时间戳进行搜索
访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间
变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。
2.8.2. stat 命令查看
[root@host-136 ~]# stat /etc/passwd
File: ‘/etc/passwd’
Size: 950 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 33818061 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: system_u:object_r:passwd_file_t:s0
Access: 2021-01-06 09:17:36.122732027 +0800
Modify: 2021-01-06 09:17:36.114732083 +0800
Change: 2021-01-06 09:17:36.115732076 +0800
Birth: -
2.8.3. 搜索最近七天内被访问过的所有文件
[root@host-136 ~]# find /etc/ -type f -atime -7
/etc/fstab
/etc/crypttab
/etc/resolv.conf
/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
/etc/pki/ca-trust/ca-legacy.conf
/etc/pki/ca-trust/extracted/java/cacerts
/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/email-ca-bundle.pem
/etc/pki/ca-trust/extracted/pem/objsign-ca-bundle.pem
2.8.4. 搜索超过七天内(7天外)被访问过的所有文件
[root@host-136 ~]# find /etc -type f -atime +7
/etc/sasl2/smtpd.conf
/etc/ethertypes
/etc/makedumpfile.conf.sample
/etc/postfix/access
/etc/postfix/canonical
/etc/postfix/generic
/etc/postfix/header_checks
/etc/postfix/relocated
/etc/postfix/transport
/etc/postfix/virtual
2.9. 根据文件大小进行匹配
2.9.1. 用法
find . -type f -size 文件大小单元
2.9.2. 文件大小单元
b —— 块(512字节)
c —— 字节
w —— 字(2字节)
k —— 千字节
M —— 兆字节
G —— 吉字节
2.9.3. 搜索大于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size +10k
/etc/ssh/moduli
/etc/postfix/access
/etc/postfix/canonical
/etc/postfix/header_checks
/etc/postfix/main.cf
/etc/postfix/transport
/etc/postfix/virtual
2.9.4. 搜索小于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size -10k
/etc/man_db.conf
/etc/sudo-ldap.conf
/etc/sudo.conf
/etc/sudoers
/etc/e2fsck.conf
/etc/mke2fs.conf
/etc/vconsole.conf
/etc/locale.conf
2.9.5. 搜索等于10KB的文件
[root@host-136 ~]# find /etc/ -type f -size 10k
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf
/etc/selinux/targeted/active/modules/100/accountsd/hll
/etc/selinux/targeted/active/modules/100/acct/hll
/etc/selinux/targeted/active/modules/100/aiccu/hll
/etc/selinux/targeted/active/modules/100/alsa/hll
/etc/selinux/targeted/active/modules/100/arpwatch/hll
2.9.6. 搜索大于10G的日志文件,并删除
[root@host-136 ~]# find /var/log -type f -name "*.log" -size +10G -delete
2.10. 根据文件权限/所有权进行匹配
2.10.1. 指定目录下搜索出权限为644的文件
[root@host-136 ~]# find / -type f -perm 644
/usr/libexec/sudo/libsudo_util.so.0.0.0
/usr/libexec/sudo/sudo_noexec.so
/usr/libexec/sudo/sudoers.so
/usr/libexec/sudo/system_group.so
2.10.2. 找出指定目录下权限不是644的txt文件
[root@host-136 ~]# find / -type f -name "*.txt" ! -perm 644
/usr/lib/firmware/ivtv-firmware-license-end-user.txt
/usr/lib/firmware/ivtv-firmware-license-oemihvisv.txt
/usr/share/licenses/shadow-utils-4.6/gpl-2.0.txt
/usr/share/licenses/shadow-utils-4.6/shadow-bsd.txt
2.10.3. 找出/home目录用户frank拥有的所有文件
[root@host-136 ~]# find /home/ -type f -user frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
2.10.4. 找出/home目录用户组frank拥有的所有文件
[root@host-136 ~]# find /home/ -type f -group frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc
3.xargs命令
3.1. 命令由来
xargs (英文全拼: eXtended ARGuments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。之所以能用到这个命令,主要是由于很多命令不支持管道符号 | 来传递参数,而日常工作中经常有这个必要,所以就有了 xargs 命令,例如:
如果想要通过一条命令,先使用 find 命令获取 /etc 目录下所有的 passwd 文件,最后使用 ls -l 获取文件的详细信息,你可能一开始会使用类似下列格式的命令:
[root@centos ~]# find /etc -name passwd
/etc/pam.d/passwd
/etc/passwd
[root@centos ~]# find /etc -name passwd | ls -l
total 0
可以看到,此时并没有按照我们的期望输出结果,此时使用 xargs
命令可以解决该问题
[root@centos ~]# find /etc -name passwd | xargs ls -l
-rw-r--r--. 1 root root 168 Apr 7 2020 /etc/pam.d/passwd
-rw-r--r-- 1 root root 1541 May 21 11:32 /etc/passwd
实际上,在 Linux 的 Shell 命令中,诸如 mkdir
、 cat
、 rm
等命令都不支持直接将管道符的输出当做输入进而作为他们的操作对象来使用。
3.2.格式
xargs [ option ] ... [ command [ initial-arguments ] ... ]
关于如何阅读 Linux 的 man
手册,请见【Linux入门学习笔记】Linux命令查阅方法及文件操作命令简介。