Find命令

find命令在Linux中搜索效果非常不错,不仅可以按照文件名搜索文件,还可以按照权限、时间、容量大小等来搜索文件,但是find命令它是通过递归式在硬盘中进行搜索的,如果指定的搜索范围过大,find命令就会消耗较大的系统资源,导致服务器压力过大。所以find命令搜索范围,不要指定太大。不过可以使用 locate


linux 查找redis config linux 查找目录_运维


find精确查找

  • 根据预设的条件递归查找对应的文件
  • find [目录] [条件1] [条件2] [条件3]……
  • 使用的条件类型:
  • 文件类型搜索
  • 文件名搜索
  • 文件容量大小搜索
  • 文件变更时间搜索
  • 文件权限搜索
  • 文件所有者和所属组搜索
  • 逻辑运算符
  • 其他选项

文件类型搜索

  • 选项 -typed表示查找目录 、 f 表示查找文件包括隐藏文件 、 l 表示查找软连接文件、b 表示块设备文件、c表示字符设备文件、p表示管道文件
  • 使用格式: find [搜索目录] [条件] 搜索类型
#: 查找/opt/下的目录
[root@localhost ~]# mkdir   /opt/cho
[root@localhost ~]# ll  /opt/
总用量 0
drwxr-xr-x. 2 root root 6 10月  5 04:12 cho
[root@localhost ~]# find  /opt  -type  d  
/opt
/opt/cho




#:查找/opt/下的文件
[root@localhost ~]# touch  /opt/file.txt
[root@localhost ~]# ll  /opt/
总用量 0
-rw-r--r--. 1 root root 0 10月  5 04:15 file.txt
[root@localhost ~]# find   /opt   -type  f
/opt/file.txt




#:查找/opt/下的软连接
[root@localhost ~]# mkdir  /opt/Directory
[root@localhost ~]# touch  /opt/file.txt
[root@localhost ~]# ln  -s  /etc/passwd  /opt/link
[root@localhost ~]# ll  /opt/
总用量 0
drwxr-xr-x. 2 root root  6 10月  5 04:20 Directory
-rw-r--r--. 1 root root  0 10月  5 04:20 file.txt
lrwxrwxrwx. 1 root root 11 10月  5 04:21 link -> /etc/passwd
[root@localhost ~]# find  /opt   -type  l
/opt/link

文件名搜索

  • 选项:-name:文件名搜索、-iname:文件名搜索,不区分大小写、-inum:通过inode号码搜索
  • 使用格式:find [搜索目录] [条件] 搜索内容
#: 搜索带im开头的文件
[root@localhost ~]# find   /etc/  -name  "im*"
/etc/xdg/autostart/imsettings-start.desktop



#:在Linux系统中,文件名是区分大小写的,要是文件名有大写英文字符,就需要用到 -iname来搜索
[root@localhost ~]# find  /opt  -iname  "Dire*"
/opt/Directory
/opt/directory
/opt/Dire


#:在Linux系统中,文件都会有inode号码,而硬链接的inode号跟源文件的号码是一样的,这个时候我们就可以通过,-inum来区分它们
[root@localhost ~]# ll  -i  /mnt/mnt.txt   /opt/mnt.txt 
18120196 -rw-r--r--. 2 root root 0 10月  5 04:43 /mnt/mnt.txt
18120196 -rw-r--r--. 2 root root 0 10月  5 04:43 /opt/mnt.txt
[root@localhost ~]# 


# 硬链接不是我们创建的,就可以通过这种方式区分硬链接
[root@localhost ~]# find  /  -inum  18120196
/mnt/mnt.txt
/opt/mnt.txt

根据文件容量大小搜索

  • 选项:-size [ + - ] : 小写的c:表示字节单位、小写的k:表示KB、大写M:表示MB、大写G:表示GB
  • 指定大小搜索文件,+的意思是搜索比指定的值大的文件,-的意思是搜索比指定的值小的文件
#:通过-size  搜索282字节的文件 ,在搜索字节的时候一定要带上c
[root@localhost ~]# ls  -lh  /opt/lipo.txt 
-rw-r--r--. 1 root root 282 10月  5 05:07 /opt/lipo.txt
[root@localhost ~]# find  /opt/  -size  282c
/opt/lipo.txt


#:搜索大于3k的文件
[root@localhost ~]# ls   -lh  /opt/file.txt 
-rw-r--r--. 1 root root 3.7K 10月  5 05:06 /opt/file.txt
[root@localhost ~]# find  /opt/  -size  +3k  
/opt/file.txt



#:搜索小于10MB的文件
[root@localhost ~]# ls  -lh  /opt/   
总用量 8.0K
drwxr-xr-x. 2 root root    6 10月  5 04:33 Dire
drwxr-xr-x. 2 root root    6 10月  5 04:32 directory
drwxr-xr-x. 2 root root    6 10月  5 04:20 Directory
-rw-r--r--. 1 root root 3.7K 10月  5 05:06 file.txt
lrwxrwxrwx. 1 root root   11 10月  5 04:21 link -> /etc/passwd
-rw-r--r--. 1 root root  282 10月  5 05:07 lipo.txt
-rw-r--r--. 2 root root    0 10月  5 04:43 mnt.txt
[root@localhost ~]# find   /opt/   -size  -10M
/opt/
/opt/Directory
/opt/link
/opt/directory
/opt/Dire
/opt/mnt.txt
/opt/file.txt
/opt/lipo.txt

文件修改时间搜索

  • -atime [+ -]:文件访问时间搜索
  • -mtime``[+ - ]:文件修改时间搜索
  • -ctime``[+ - ]:文件修改时间搜索
  • aminmmincmin,所有 time 选项的默认单位是天,而 min 选项的默认单位是分钟。
  • 使用格式: find [搜索目录] [条件] 搜索内容
#: 查找一天内修改过的文件跟目录
[root@localhost ~]# ll  -ld  /opt
drwxr-xr-x. 5 root root 111 10月  5 05:07 /opt
[root@localhost ~]# ll  -ld  /opt/*
drwxr-xr-x. 2 root root    6 10月  5 04:33 /opt/Dire
drwxr-xr-x. 2 root root    6 10月  5 04:32 /opt/directory
drwxr-xr-x. 2 root root    6 10月  5 04:20 /opt/Directory
-rw-r--r--. 1 root root 3713 10月  5 05:06 /opt/file.txt
lrwxrwxrwx. 1 root root   11 10月  5 04:21 /opt/link -> /etc/passwd
-rw-r--r--. 1 root root  282 10月  5 05:07 /opt/lipo.txt
-rw-r--r--. 2 root root    0 10月  5 04:43 /opt/mnt.txt
[root@localhost ~]# find  /opt/  -mtime  -1
/opt/
/opt/Directory
/opt/link
/opt/directory
/opt/Dire
/opt/mnt.txt
/opt/file.txt
/opt/lipo.txt


#:ctime跟mtime功能是一样的
[root@localhost ~]# ll  -ld  /opt/*
drwxr-xr-x. 2 root root    6 10月  5 04:33 /opt/Dire
drwxr-xr-x. 2 root root    6 10月  5 04:32 /opt/directory
drwxr-xr-x. 2 root root    6 10月  5 04:20 /opt/Directory
-rw-r--r--. 1 root root 3713 10月  5 05:06 /opt/file.txt
lrwxrwxrwx. 1 root root   11 10月  5 04:21 /opt/link -> /etc/passwd
-rw-r--r--. 1 root root  282 10月  5 05:07 /opt/lipo.txt
-rw-r--r--. 2 root root    0 10月  5 04:43 /opt/mnt.txt
[root@localhost ~]# find  /opt/  -ctime  -1
/opt/
/opt/Directory
/opt/link
/opt/directory
/opt/Dire
/opt/mnt.txt
/opt/file.txt
/opt/lipo.txt

文件权限搜索

  • find中,支持使用权限来搜索文件,权限也支持[+/-]
  • 选项:-perm :指定权限必须一致、-perm- : 指定的权限全部必须包含、-perm +: 指定的权限需要包含一项。
  • 使用格式:find [搜索目录] [条件] 搜索内容
[root@localhost ~]# mkdir  -m  0444  /opt/dire1

[root@localhost ~]# mkdir  -m  0200  /opt/dire2

[root@localhost ~]# mkdir  -m  0600  /opt/dire3

[root@localhost ~]# mkdir  -m  0755  /opt/dire4
[root@localhost ~]# ll -ld  /opt/*
dr--r--r--. 2 root root 6 10月  5 05:55 /opt/dire1
d-w-------. 2 root root 6 10月  5 05:55 /opt/dire2
drw-------. 2 root root 6 10月  5 05:55 /opt/dire3
drwxr-xr-x. 2 root root 6 10月  5 05:56 /opt/dire4

#: 查找必须是755的指定搜索
[root@localhost ~]# find   /opt/  -perm  755
/opt/
/opt/dire4


#:查找包含400的搜索,可以看到没有dire2,因为它的权限是200,没有r权限
[root@localhost ~]# find   /opt/  -perm  -400
/opt/
/opt/dire1
/opt/dire3
/opt/dire4

通过文件所有者和所属组搜索

在 Linux 系统中,绝大多数文件都是使用 root 用户身份建立的,所以在默认情况下,绝大多数系统文件的所有者都是 root,而所有的文件都有所有者,只有一种情况例外,那就是外来文件。比如光盘和 U 盘中的文件如果是由 Windows 复制的,在 Linux 中査看就是没有所有者的文件,再比如手工源码包安装的文件,也有可能没有所有者,这个时候就可以通过-nouser来搜索这些文件。

使用选项

  • -uid 用户 : 指定uid值的文件
  • -gid 组 ID: 指定gid值的文件
  • -user 用户名:指定用户的文件
  • -group 组名:指定所属组的文件
  • -nouser:查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在
  • -nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
  • 使用格式:find [搜索目录] [条件] 搜索内容
[root@localhost ~]# ls  -ld  /opt/
drwxr-xrwx. 2 root root 6 10月  5 06:19 /opt/
[root@localhost ~]# su  - xh 
上一次登录:三 10月  5 06:19:32 CST 2022pts/0 上
[xh@localhost ~]$ mkdir  /opt/dir
[xh@localhost ~]$ 登出
[root@localhost ~]# id   xh
uid=1000(xh) gid=1000(xh) 组=1000(xh)
[root@localhost ~]# find   /opt/  -uid  1000   #: 通过uid来查找文件
/opt/dir
[root@localhost ~]# find   /opt/  -gid  1000    #:通过gid来查找文件
/opt/dir
[root@localhost ~]# find   /opt/  -user  xh     #:通过用户名来查找文件
/opt/dir
[root@localhost ~]# find   /opt/  -group  xh      #:通过所属组来查找文件
/opt/dir

逻辑运算符

  • 选项:-a:and逻辑等于并且(不选逻辑运算符系统默认带-a)、-o:or逻辑或者、-not:not逻辑取反
  • 使用格式:find [搜索目录] [条件] 搜索内容
#: 查找大于4MB并且是文件,一般不带逻辑运算符,find是默认-a的
[root@localhost ~]# find  /  -size  +4M  -a   -type f  
/boot/vmlinuz-3.10.0-1160.el7.x86_64
/boot/initramfs-0-rescue-1b510e4cf76b4d8aa9aae5132f10471e.img
/boot/vmlinuz-0-rescue-1b510e4cf76b4d8aa9aae5132f10471e
/boot/initramfs-3.10.0-1160.el7.x86_64.img
/boot/initramfs-3.10.0-1160.el7.x86_64kdump.img
/proc/kcore
……下面内容省略……


#:查找文件名pass带头的,或者大于10MB的文件
[root@localhost ~]# find  /etc/  -name  "pass*"   -o    -size +10M  -type  f
/etc/pam.d/passwd
/etc/pam.d/password-auth-ac
/etc/pam.d/password-auth
/etc/passwd
/etc/openldap/certs/password
/etc/selinux/targeted/active/modules/100/passenger
/etc/passwd-

#:取反查找除了passwd的文件
[root@localhost ~]# find   /opt/  -not  -name "passwd"   -type f

find命令额外操作

-exec选项相当于额外操作,首先{}\;是标准格式,只要执行-exec选项,这两个符号就必须完整输入。\;相当于操作结束,find命令它是通过递归搜索系统文件的,所以它每找到一个文件,就会放到{}中,{}里面的内容就是find查找的内容。查找到的内容在通过-exec额外操作。然后\;结束。

-ok选项和-exec选项的作用基本一致,区别在于-exec的命令会直接处理,而不询问-ok处理前会先提示用户是否这样处理,在得到确认命令后,才会执行。

#: 查找opt目录下小于20k的文件,每找到一份文件就删除掉
[root@localhost ~]# find  /opt/  -size  -20k  -a  -type  f  -exec  rm   -rf  {} \;
[root@localhost ~]# ls /opt

#:跟-exec一样,不同与exec直接执行,ok会提示。
[root@localhost ~]# find  /etc/  -size  -20k   -a  -type  f   -ok  cp -r  {}  /opt    \;
< cp ... /etc/fstab > ? y
< cp ... /etc/crypttab > ? y
< cp ... /etc/resolv.conf > ? y
< cp ... /etc/fonts/conf.d/25-no-bitmap-fedora.conf > ? y
< cp ... /etc/fonts/conf.d/README > ? y
< cp ... /etc/fonts/fonts.conf > ? y
< cp ... /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 > ? y
< cp ... /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Debug-7 > ? y
< cp ... /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7 > ? y
< cp ... /etc/pki/ca-trust/README > ? y
< cp ... /etc/pki/ca-trust/ca-legacy.conf > ? y
< cp ... /etc/pki/ca-trust/extracted/README > ? y
< cp ... /etc/pki/ca-trust/extracted/java/README > ? y
< cp ... /etc/pki/ca-trust/extracted/openssl/README > ? 
……下面省略…………

locate命令

locate(locate) 命令用来查找文件或目录。 locate命令要比find -name快得多,原因在于它不搜索具体目录,而是搜索一个数据库/var/lib/mlocate/mlocate.db 。这个数据库中含有本地所有文件信息。Linux系统自动创建这个数据库,并且每天自动更新一次,因此,我们在用whereislocate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。为了避免这种情况,可以在使用locate之前,先使用updatedb命令,手动更新数据库。

locate工作由四部分组成

  • /usr/bin/updatedb 主要用来更新数据库,通过crontab自动完成的
  • /usr/bin/locate 查询文件位置
  • /etc/updatedb.conf updatedb的配置文件
  • -/var/lib/mlocate/mlocate.db存放文件信息的文件

locate的选项

  • -b, --basename -- 仅匹配路径名的基本名称
  • -c, --count -- 只输出找到的数量
  • -d, --database DBPATH -- 使用 DBPATH 指定的数据库,而不是默认数据库 /var/lib/mlocate/mlocate.db
  • -e, --existing -- 仅打印当前现有文件的条目
  • -1 -- 如果 是 1.则启动安全模式。在安全模式下,使用者不会看到权限无法看到 的档案。这会始速度减慢,因为 locate 必须至实际的档案系统中取得档案的 权限资料。
  • -0,--null --在输出上带有NUL的单独条目
  • -S, --statistics -- 不搜索条目,打印有关每个数据库的统计信息
  • -q – 安静模式,不会显示任何错误讯息。
  • -P, --nofollow, -H -- 检查文件存在时不要遵循尾随的符号链接
  • -l, --limit, -n LIMIT -- 将输出(或计数)限制为LIMIT个条目
  • -n -- 至多显示 n个输出。
  • -m, --mmap -- 被忽略,为了向后兼容
  • -r, --regexp REGEXP -- 使用基本正则表达式
  • --regex -- 使用扩展正则表达式
  • -q, --quiet -- 安静模式,不会显示任何错误讯息
  • -s, --stdio -- 被忽略,为了向后兼容
  • -o -- 指定资料库存的名称。
  • -h, --help -- 显示帮助
  • -i, --ignore-case -- 忽略大小写
  • -V, --version -- 显示版本信息
# 查找shells文件,连带包含shells字符的文件也列出来了。
[root@localhost ~]# locate  shells
/etc/shells
/usr/lib64/security/pam_shells.so
/usr/share/augeas/lenses/dist/shells.aug
/usr/share/doc/pam-1.1.8/html/sag-pam_shells.html
/usr/share/doc/pam-1.1.8/txts/README.pam_shells
/usr/share/man/man5/shells.5.gz
/usr/share/man/man8/pam_shells.8.gz
/usr/share/man/zh_CN/man5/shells.5.gz
/usr/share/mime/application/x-shellscript.xml

locate 与 find 不同

find 是去硬盘找,locate 只在 /var/lib/slocate 资料库中找。

locate 的速度比find快,它并不是真的查找,而是查数据库,一般文件数据库在 /var/lib/slocate/slocate.db 中,所以locate的查找并不是实时的,而是以数据库的更新为准,一般是系统自己维护,也可以手工升级数据库 。

#:命令手动数据库,默认情况下updatedb每天执行一次

[root@localhost ~]# updatedb