Linux中find命令

1.find命令

Linux find 命令用于在指定目录下查找文件和目录。

它可以使用不同的选项来过滤和限制查找的结果。

2.基本语法

find   path  -option  【 -print 】  【 -exec   -ok   |xargs  |grep  】 【  command  {} \;  】

find [⽂件路径] [选项 选项的值]

-name *

-type f|d

options常用的有下选项:

-name   filename               #查找名为filename的文件
-perm                                #按执行权限来查找
-user    username             #按文件属主来查找
-group groupname            #按组来查找
-mtime   -n +n                   #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n                   #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime    -n +n                  #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup                          #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                            #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-type    b/d/c/p/l/f             #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]                    #查长度为n块[或n字节]的文件
-mount                            #查文件时不跨越文件系统mount点
-follow                            #如果遇到符号链接文件,就跟踪链接所指的文件
-prune                            #忽略某个目录

案例,找到httpd.conf⽂件

[root@localhost ~]# find / -name "httpd.conf" -type f

# ⽆法找到,发现是没有安装httpd服务

[root@localhost ~]# yum install -y httpd

# 安装htppd服务

[root@localhost ~]# find / -name "httpd.conf" -type f

# 下⾯就是查找出的⽂件

/etc/httpd/conf/httpd.conf

/usr/lib/tmpfiles.d/httpd.conf

# /范围换成/etc/⽬录范围,这样查找更快,更加节省计算⾃资源

[root@localhost ~]# find /etc/ -name "httpd.conf" -type f

/etc/httpd/conf/httpd.conf

3.*通配符

linux 系统中,如果要查找的⽂件的名称不清晰,可以使⽤部分⽂件名+*搜索

案例 获取/etc/中以.conf结尾的⽂件

[root@localhost ~]# find /etc/ -name "*.conf" -type f

/etc/resolv.conf
/etc/libaudit.conf
/etc/depmod.d/dist.conf
/etc/dracut.conf
/etc/prelink.conf.d/nss-softokn-prelink.conf

/etc/prelink.conf.d/fipscheck.conf
/etc/prelink.conf.d/grub2.conf
/etc/modprobe.d/tuned.conf
/etc/modprobe.d/firewalld-sysctls.conf
/etc/modprobe.d/dccp-blacklist.conf
/etc/rsyslog.d/listen.conf
/etc/systemd/bootchart.conf
/etc/systemd/coredump.conf
/etc/systemd/journald.conf
/etc/systemd/logind.conf
/etc/systemd/system.conf
/etc/systemd/user.conf
/etc/host.conf
/etc/dbus-1/system.d/org.freedesktop.hostname1.conf
/etc/dbus-1/system.d/org.freedesktop.import1.conf
/etc/dbus-1/system.d/org.freedesktop.locale1.conf
/etc/dbus-1/system.d/org.freedesktop.login1.conf
/etc/dbus-1/system.d/org.freedesktop.machine1.conf
/etc/dbus-1/system.d/org.freedesktop.systemd1.conf
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
/etc/dbus-1/system.d/org.freedesktop.PolicyKit1.conf
/etc/dbus-1/system.d/wpa_supplicant.conf
/etc/dbus-1/system.d/nm-dispatcher.conf
/etc/dbus-1/system.d/nm-ifcfg-rh.conf
/etc/dbus-1/system.d/org.freedesktop.NetworkManager.conf
/etc/dbus-1/system.d/teamd.conf
/etc/dbus-1/system.d/com.redhat.tuned.conf
/etc/dbus-1/system.d/FirewallD.conf
/etc/dbus-1/session.conf
/etc/dbus-1/system.conf
/etc/udev/udev.conf
/etc/lvm/lvm.conf
/etc/lvm/lvmlocal.conf
/etc/NetworkManager/NetworkManager.conf
/etc/X11/xorg.conf.d/00-keyboard.conf
/etc/pki/ca-trust/ca-legacy.conf
/etc/ld.so.conf
/etc/ld.so.conf.d/mariadb-x86_64.conf
/etc/ld.so.conf.d/kernel-3.10.0-862.el7.x86_64.conf
/etc/ld.so.conf.d/bind-export-x86_64.conf
/etc/ld.so.conf.d/kernel-3.10.0-1160.119.1.el7.x86_64.conf
/etc/nsswitch.conf
/etc/sestatus.conf
/etc/yum/protected.d/systemd.conf
/etc/yum/pluginconf.d/fastestmirror.conf
/etc/yum/pluginconf.d/langpacks.conf
/etc/yum/version-groups.conf
/etc/krb5.conf
/etc/sysctl.conf
/etc/security/access.conf
/etc/security/chroot.conf
/etc/security/group.conf
/etc/security/limits.conf
/etc/security/limits.d/20-nproc.conf
/etc/security/namespace.conf
/etc/security/pam_env.conf
/etc/security/sepermit.conf

搜索以http开头的⽂件

[root@localhost ~]# find /etc/ -name "http*" -type f
/etc/sysconfig/httpd
/etc/logrotate.d/httpd
/etc/httpd/conf/httpd.conf

find命令的exec参数

案例:删除系统/var/log/ 10天之前的⽇志,格式都是.log⽂件

⽅法1 报错,rm不⽀持这种写法

⽅法2 rmls不⽀持管道

⽅法3 使⽤xargs 将查询结果交给rm

⽅法4 使⽤find执⾏ -exec

#查看⽬录中的txt⽂件
[root@localhost opt]# ls -l *.txt
#没有e.txt⽂件,在前⾯被删除了
-rw-r--r--. 1 root root 0 7⽉  14 13:54 a.txt
-rw-r--r--. 1 root root 0 7⽉  13 00:00 b.txt
-rw-r--r--. 1 root root 0 7⽉  12 00:00 c.txt
-rw-r--r--. 1 root root 0 7⽉  11 00:00 d.txt
# 创建⽂件并且指定⽂件修改⽇期
[root@localhost opt]# touch -m -d "2024-7-10 00:00" e.txt
[root@localhost opt]# ls -l 
总⽤量 0
-rw-r--r--. 1 root root 0 7⽉  14 13:54 a.txt
-rw-r--r--. 1 root root 0 7⽉  13 00:00 b.txt
-rw-r--r--. 1 root root 0 7⽉  12 00:00 c.txt
-rw-r--r--. 1 root root 0 7⽉  11 00:00 d.txt

-rw-r--r--. 1 root root 0 7⽉  10 00:00 e.txt
# 查找三天以前的⽂件
[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3
/opt/e.txt
# 使⽤-exec ⽂件调⽤rm函数 {}表示前⾯find查到的内容 \;表示标识符
# 这⾥在{}后⾯没有打空格报错了,在{}后应该打空格
[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -rf {}\;
find: 遗漏“-exec”的参数
[root@localhost opt]# find /opt/ -name "*.txt" -type f -mtime +3 -exec rm -rf {} \;
总⽤量 0
-rw-r--r--. 1 root root 0 7⽉  14 13:54 a.txt
-rw-r--r--. 1 root root 0 7⽉  13 00:00 b.txt
-rw-r--r--. 1 root root 0 7⽉  12 00:00 c.txt
-rw-r--r--. 1 root root 0 7⽉  11 00:00 d.txt

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
find命令Linux系统一个非常常用的命令,该命令可以查找文件和目录,并根据不同的条件进行过滤。其基本语法格式如下: ```shell find [path] [option] [expression] ``` 其,path表示要查找的路径,可以是相对路径或绝对路径;option表示查找时的一些选项,可以用来指定查找深度、时间、权限等条件;expression表示要查找的条件,可以是文件名、文件类型、文件大小等。 下面介绍几个常用的find命令选项和表达式: 1. -name:根据文件名进行查找。 ```shell find /home -name "test.txt" # 在/home下查找文件名为test.txt的文件 ``` 2. -type:根据文件类型进行查找。 ```shell find /home -type f # 在/home下查找普通文件 find /home -type d # 在/home下查找目录 ``` 3. -mtime:根据文件修改时间进行查找。 ```shell find /home -mtime +7 # 查找/home目录下7天前修改的文件 find /home -mtime -7 # 查找/home目录下7天内修改的文件 ``` 4. -size:根据文件大小进行查找。 ```shell find /home -size +1M # 查找/home目录下大小大于1MB的文件 find /home -size -1M # 查找/home目录下大小小于1MB的文件 ``` 5. -user:根据文件所有者进行查找。 ```shell find /home -user alice # 查找/home目录下所有者为alice的文件 ``` 6. -perm:根据文件权限进行查找。 ```shell find /home -perm 644 # 查找/home目录下权限为644的文件 ``` 7. -exec:对查找到的文件进行操作。 ```shell find /home -name "test.txt" -exec rm {} \; # 查找/home目录下名为test.txt的文件并删除 ``` 以上仅是find命令的一些常用选项和表达式,实际使用还可以根据具体需求进行更多的定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值