Content

find 命令全集(find 命令使用)全程干货

文章摘要

这篇文章详细介绍了Linux系统中`find`命令的使用方法及其相关功能,涵盖了命令的基本语法、常用选项、常用动作以及如何根据文件名、时间戳、类型、权限、所属用户、目录深度和文件大小进行匹配。文章通过多个示例命令展示了`find`的实用场景,包括匹配文件名、文件类型、文件权限、用户和组、目录深度、大小等。此外,还介绍了如何结合`find`与其他命令(如`cat`、`cp`、`rm`、`xargs`、`ls`等)进行更复杂的操作,如拼接文件到新文件、复制文件到特定目录、删除特定时间的文件等。文章还提到`find`命令的高级功能,如布尔逻辑操作、脚本结合使用(如`-ok`和`-exec`),以及提示用户执行操作的功能。通过这些内容,读者可以全面掌握`find`命令的用法及其在实际场景中的应用。


目录find命令详解1. find命令作用2. find命令选项基本格式3. 常用选项:4. 常用动作:5. 根据文件名进行匹配6. 根据文件类型进行搜索7. 基于目录深度搜索8. 根据文件时间戳进行搜索10. 根据文件权限/所有权进行匹配11. 借助-exec选项与其他命令结合使用

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

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

-a:and 必须满足两个条件才显示 -o:or 只要满足一个条件就显示 -name:按照文件名查找文件 -iname:按照文件名查找文件(忽略大小写) -type:根据文件类型进行搜索 -perm:按照文件权限来查找文件 -user 按照文件属主来查找文件。 -group 按照文件所属的组来查找文件。 -fprint 文件名:将匹配的文件输出到文件。 -newer file1 ! newer file2 查找更改时间比文件file1新但比文件file2旧的文件
-print 默认动作,将匹配的文件输出到标准输出 -exec 对匹配的文件执行该参数所给出的命令。相应命令的形式为 ‘command’ { } \;,注意{ }和\;之间的空格。 -ok 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。 -delete 将匹配到的文件删除 |xargs 将匹配到的文件删除 |xargs rm -rf

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

命令:find .

命令:find . [root@host-134 ~]# find . . https://www.jb51.net/article/.bash_logout https://www.jb51.net/article/.bash_profile https://www.jb51.net/article/.bashrc https://www.jb51.net/article/.cshrc https://www.jb51.net/article/.tcshrc https://www.jb51.net/article/anaconda-ks.cfg https://www.jb51.net/article/.bash_history https://www.jb51.net/article/.mysql_history https://www.jb51.net/article/zuoye https://www.jb51.net/article/zuoye/lnmp.sh https://www.jb51.net/article/zuoye/system.sh https://www.jb51.net/article/nginx-1.18.0 https://www.jb51.net/article/nginx-1.18.0/auto https://www.jb51.net/article/nginx-1.18.0/auto/cc https://www.jb51.net/article/nginx-1.18.0/auto/cc/acc https://www.jb51.net/article/nginx-1.18.0/auto/cc/bcc

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

find /home/ -name “*.txt”

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

find /home -iname “*.txt”

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

find /home/ -name “*.txt” -o -name “*.pdf”

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

find /home/ -name “*.txt” -a -name “a*”

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
-type 类型参数 类型参数列 f 普通文件 l 符号连接(软连接) d 目录 b 块设备 s 套接字

6.1

[root@host-136 ~]# find /home/ -type f /home/frank/.bash_logout /home/frank/.bash_profile /home/frank/.bashrc

6.2

[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

[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

[root@host-134 ~]# find /dev/ -type b /dev/dm-1 /dev/dm-0 /dev/sda2 /dev/sda1 /dev/sda /dev/sr0

6.5

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

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

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

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

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

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

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: –

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

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

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

用法 find . -type f -size 文件大小单元 文件大小单元: b —— 块(512字节) c —— 字节 w —— 字(2字节) k —— 千字节 M —— 兆字节 G —— 吉字节

9.1 搜索大于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

9.2 搜索小于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

9.3 搜索等于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

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

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

找出指定目录下权限不是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

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

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

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

[root@host-136 ~]# find /home/ -type f -group frank /home/frank/.bash_logout /home/frank/.bash_profile /home/frank/.bashrc
找出/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 命令详解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:详解Linux下find查找文件命令和grep查找文件命令详解Linux中两个查找命令locate和find教程Linux下find和grep常用命令及区别介绍Linux中find命令的用法汇总ubuntu下kvm 命令行安装64位ubuntu报”Couldn”t find hvm kernel for Ubuntu tree.”的问题分析字符串查找 cmd find命令