服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Linux|Centos|Ubuntu|系统进程|Fedora|注册表|Bios|Solaris|Windows7|Windows10|Windows11|windows server|

服务器之家 - 服务器系统 - Linux - find 命令全集

find 命令全集

2023-03-06 13:47Linux运维-Friend Linux

find命令用来在指定目录下查找文件,本文给大家整理了关于find命令的相关知识,感兴趣的朋友跟随小编一起看看吧

find命令详解

1. find命令作用

find命令用来在指定目录下查找文件。

2. find命令选项基本格式

find 目录 -选项 动作[-print -exec -ok ...]

3. 常用选项:

?
1
2
3
4
5
6
7
8
9
10
-a:and 必须满足两个条件才显示
-o:or 只要满足一个条件就显示
-name:按照文件名查找文件
-iname:按照文件名查找文件(忽略大小写)
-type:根据文件类型进行搜索
-perm:按照文件权限来查找文件
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-fprint 文件名:将匹配的文件输出到文件。
-newer file1 ! newer file2 查找更改时间比文件file1新但比文件file2旧的文件

4. 常用动作:

?
1
2
3
4
5
-print 默认动作,将匹配的文件输出到标准输出
-exec 对匹配的文件执行该参数所给出的命令。相应命令的形式为 'command' { } \;,注意{ }和\;之间的空格。
-ok 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
-delete 将匹配到的文件删除
|xargs  将匹配到的文件删除   |xargs rm -rf

5. 根据文件名进行匹配

5.1 列出当前目录及子目录下所有文件和文件夹

命令:find .

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
命令: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

5.2 在/home目录下查找以.txt结尾的文件名

?
1
find /home/ -name "*.txt"

5.3 在/home目录下查找以.txt结尾的文件名,但忽略大小写

?
1
find /home -iname "*.txt"

5.4 查找 /home/ 下所有以.txt或.pdf结尾的文件

?
1
find /home/ -name "*.txt" -o -name "*.pdf"

5.5 查找 /home/ 下所有以a开头和以.txt结尾的文件

?
1
find /home/ -name "*.txt" -a -name "a*"

5.6 搜索/home目录下txt结尾的文件,并将输出到指定文件中(re.txt)

?
1
2
3
4
[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

6. 根据文件类型进行搜索

?
1
2
3
4
5
6
7
8
-type 类型参数
 
类型参数列
f 普通文件
l 符号连接(软连接)
d 目录
b 块设备
s 套接字

6.1

?
1
2
3
4
[root@host-136 ~]# find /home/ -type f
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc

6.2

?
1
2
3
4
5
6
7
8
9
[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

6.3

?
1
2
3
4
5
6
7
8
9
10
[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

6.4

?
1
2
3
4
5
6
7
[root@host-134 ~]# find /dev/ -type b
/dev/dm-1
/dev/dm-0
/dev/sda2
/dev/sda1
/dev/sda
/dev/sr0

6.5

?
1
2
[root@localhost home]# find /var/lib/ -type s
/var/lib/mysql/mysql.sock

7. 基于目录深度搜索

7.1 向下最大深度限制为3

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[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

7.2 搜索出深度距离当前目录至少2个子目录的所有文件

?
1
2
[root@host-136 ~]# find /usr/local/ -mindepth 2 -type f
/usr/local/sbin/mail.py

8. 根据文件时间戳进行搜索

8.1 UNIX/Linux文件系统每个文件都有三种时间戳:

?
1
2
3
访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间
变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。

8.2 stat 命令查看:

?
1
2
3
4
5
6
7
8
9
10
[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: -

8.3 搜索最近七天内被访问过的所有文件

?
1
2
3
4
5
6
7
8
9
10
11
[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

8.4 搜索超过七天内(7天外)被访问过的所有文件

?
1
2
3
4
5
6
7
8
9
10
11
[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

9. 根据文件大小进行匹配

?
1
2
3
4
5
6
7
8
9
10
用法
find . -type f -size 文件大小单元
文件大小单元:
 
b —— 块(512字节)
c —— 字节
w —— 字(2字节)
k —— 千字节
M —— 兆字节
G —— 吉字节

9.1 搜索大于10KB的文件

?
1
2
3
4
5
6
7
8
[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

9.2 搜索小于10KB的文件

?
1
2
3
4
5
6
7
8
9
[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

9.3 搜索等于10KB的文件

?
1
2
3
4
5
6
7
8
[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

9.4 搜索大于10G的日志文件,并删除

?
1
[root@host-136 ~]# find /var/log -type f -name "*.log" -size +10G -delete

10. 根据文件权限/所有权进行匹配

找出指定目录下权限不是644的txt文件

?
1
2
3
4
5
[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

找出/home目录用户frank拥有的所有文件

?
1
2
3
4
[root@host-136 ~]# find /home/ -type f -user frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc

找出/home目录用户组frank拥有的所有文件

?
1
2
3
4
[root@host-136 ~]# find /home/ -type f -group frank
/home/frank/.bash_logout
/home/frank/.bash_profile
/home/frank/.bashrc

11. 借助-exec选项与其他命令结合使用

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
找出/tmp目录下所有root的文件,并把所有权更改为用户frank
 
find /tmp/ -type f -user root -exec chown frank {} \;
使用占位符{}来表示find到的文件名
 
找出家目录下所有的.sh文件并删除
 
[root@localhost home]# find $HOME -name "*.sh" -ok rm {} \;find #HOME -name "*.sh" -exec rm {} \;
< rm ... /root/install_lnmp.sh > ? y
-ok和-exec行为一样,不过它会给出提示,是否执行相应的操作。
 
查找/home目录下所有.txt文件并把他们拼接到all.txt文件中
 
find /home/ -type f -name "*.txt" -exec cat {} \;>all.txt
 
查找/home目录下所有.txt文件并把他们复制到/opt/backup文件中
 
find /home/ -type f -name "*.txt" -exec cp {} /opt/backup/ \;
 
/var/log目录中查找更改时间在5日以前的文件并删除它们:
 
find /var/log -type f -mtime +5 -exec rm {} \;find /var/log -type f -mtime +5 |xargs rm -rf

到此这篇关于find 命令全集的文章就介绍到这了,更多相关find 命令详解内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/linuxmysql/p/16398664.html

延伸 · 阅读

精彩推荐