find&locate

先说说locate和find的区别:

1、locate是查找数据库里面的数据,速度比find快,但是数据可能不准确,因为数据库里面的内容,数据库不会实时更新,数据库在/var/lib/mlocate/mlocate.db里面,如果我们现在删除掉一个文件或者新建掉一个文件,用locate查找是不准确的,如果坚持用locate查找,应该先用updatedb,更新数据到数据库里面

2、locate操作比较少,没有find操作多,功能不够find强大。

 

一、locate

 

查找etc下以sh开头的文件

[root@localhost tmp]# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells

 

查找家目录下以a开头的文件

[root@localhost tmp]# locate ~/a
/root/a
/root/anaconda-ks.cfg

 

locate也可以使用通配符

[root@localhost tmp]# locate /etc/*a
/etc/alsa
/etc/ipa
/etc/java
/etc/qemu-ga
/etc/rdma
/etc/samba
/etc/alsa/alsactl.conf
/etc/alsa/state-daemon.conf
/etc/ipa/nssdb
/etc/java/font.properties
/etc/java/java.conf
/etc/java/security
/etc/java/security/security.d
/etc/qemu-ga/fsfreeze-hook
/etc/qemu-ga/fsfreeze-hook.d
/etc/rdma/ibacm_opts.cfg
/etc/rdma/mlx4.conf
/etc/rdma/rdma.conf
/etc/rdma/sriov-vfs
/etc/samba/lmhosts
/etc/samba/smb.conf
/etc/samba/smb.conf.example

 

-i是不区分大小写

touch之后应该更新数据库才可以看见

[root@localhost ~]# touch A
[root@localhost ~]# locate -i ~/*a*
/root/a
/root/anaconda-ks.cfg
/root/initial-setup-ks.cfg
[root@localhost ~]# locate -i ~/a
/root/a
/root/anaconda-ks.cfg
[root@localhost ~]# updatedb
[root@localhost ~]# locate -i ~/a
/root/A
/root/a
/root/anaconda-ks.cfg

 

locate 感觉是不能用来查找在tmp下的文件或者目录,可能tmp下的东西不会写进数据库里面

[root@localhost ~]# ll /tmp/
总用量 660
-rw-r–r–. 1 root root 15031 5月 12 20:01 anaconda.log
drwxr-xr-x. 2 root root 50 5月 13 12:10 back
-rw-r–r–. 1 Bob Bang 13 5月 16 09:08 f1
-rw-rwxr–+ 1 root root 12 5月 13 09:18 file
-rw-r–r–. 1 root root 1157 5月 13 10:52 group
drwxr-xr-x. 2 root root 18 5月 12 17:47 hsperfdata_root
-rw-r–r–. 1 root root 0 5月 12 19:52 ifcfg.log
drwxr-x—. 7 root nubula 4096 5月 5 15:01 log
drwsrwx—+ 4 h2 h2 4096 5月 16 08:47 p1

[root@localhost ~]# locate -i /tmp/f1

[root@localhost ~]#

 

二、find

 

find涉及 的操作就比较多,除了查找还可以在查找完之后做一系列操作,还可以多条件的筛选

 

 

1、按时间查找       -mtime

假如我们查找24小时内被修改过的文件可以用

[root@localhost ~]# find /etc/ -mtime 0
/etc/
/etc/resolv.conf

 

问题

在非root下用这个命令会出现权限问题

 

[h1@localhost ~]$ find /etc/ -mtime 0
/etc/
/etc/resolv.conf
find: ‘/etc/grub.d’: 权限不够
find: ‘/etc/pki/CA/private’: 权限不够
find: ‘/etc/pki/rsyslog’: 权限不够
find: ‘/etc/dhcp’: 权限不够
find: ‘/etc/polkit-1/rules.d’: 权限不够
find: ‘/etc/polkit-1/localauthority’: 权限不够
find: ‘/etc/lvm/archive’: 权限不够
find: ‘/etc/lvm/backup’: 权限不够
find: ‘/etc/lvm/cache’: 权限不够
find: ‘/etc/selinux/targeted/active’: 权限不够
find: ‘/etc/selinux/final’: 权限不够
find: ‘/etc/ntp/crypto’: 权限不够
find: ‘/etc/vmware-tools/GuestProxyData/trusted’: 权限不够
find: ‘/etc/firewalld’: 权限不够
find: ‘/etc/audisp’: 权限不够
find: ‘/etc/audit’: 权限不够
find: ‘/etc/sudoers.d’: 权限不够
find: ‘/etc/ipsec.d’: 权限不够
find: ‘/etc/cups/ssl’: 权限不够
find: ‘/etc/libvirt’: 权限不够
find: ‘/etc/sssd’: 权限不够

 

-mtime +n :列出在 n 天之前(不含 n 天本身)被更改的文件;

[root@localhost p1]# ll
总用量 72
-rw-r–r–+ 1 root root 1348 5月 16 08:47 abrt-console-notification.sh
-rw-r–r–+ 1 root root 381 5月 16 08:47 cert-verification.cfg
-rw-r–r–+ 1 root root 50 5月 16 08:47 cifs.idmap.conf
-rw-r–r–+ 1 root root 52 5月 16 08:47 cifs.spnego.conf
-rw-r–r–+ 1 root root 37790 5月 16 08:47 de-kurzschrift.ctb
-rw-r–r–+ 1 root root 3289 5月 16 08:47 de-vollschrift.ctb
-rw-r–r–+ 1 root root 11267 5月 16 08:47 fr-cbifs.ttb
drwxrwxr-x+ 2 h1 h1 6 5月 12 21:21 p1
drwxrwxr-x+ 2 root root 6 5月 12 21:18 p2

[root@localhost p1]# find /tmp/p1/ -mtime +1 -ls
2111185 0 drwxrwxr-x 2 root root 6 5月 12 21:18 /tmp/p1/p2
35801367 0 drwxrwxr-x 2 h1 h1 6 5月 12 21:21 /tmp/p1/p1

 

-mtime -n :列出在 n 天之內(含 n 天本身)被更改过的文件;

[root@localhost ~]# find /etc/ -mtime -3 -ls
33554497 12 drwxr-xr-x 137 root root 8192 5月 16 20:35 /etc/
35807877 4 -rw-r–r– 1 root root 73 5月 16 20:35 /etc/resolv.conf
67411938 4 -rw-r–r– 1 root root 14 5月 13 21:32 /etc/tuned/active_profile
1839240 4 drwxr-xr-x 5 root lp 4096 5月 13 21:33 /etc/cups
1839256 4 -rw-r—– 1 root lp 110 5月 13 21:33 /etc/cups/subscriptions.conf

 

2、按文件名找文件

find和locate有点不同 的是

 

[h1@localhost ~]$ locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells
[h1@localhost ~]$ find /etc/sh
find: ‘/etc/sh’: 没有那个文件或目录

 

locate可以打头字母寻找文件,而 find不可以,如果想这样只能使用通配符

[h1@localhost ~]$ find /etc/sh*
/etc/shadow
/etc/shadow-
/etc/shells

 

– iname 可以不区别大小写

[root@localhost p1]# find /tmp/p1/  -iname p1
/tmp/p1/
/tmp/p1/p1
/tmp/p1/P1

 

3、按属主属组查找

-user与-group

[root@localhost p1]# find -user h1
./p1
[root@localhost p1]# find -user root
./p2
./abrt-console-notification.sh
./cert-verification.cfg
./cifs.idmap.conf
./cifs.spnego.conf
./fr-cbifs.ttb
./de-kurzschrift.ctb
./de-vollschrift.ctb
./P1

 

如果是-group 和-user一起用,一定要都匹配上才会找到文件,是与操作,不是或操作

[root@localhost p1]# find -group root -user h1
[root@localhost p1]# find -group root -user root
./p2
./abrt-console-notification.sh
./cert-verification.cfg
./cifs.idmap.conf
./cifs.spnego.conf
./fr-cbifs.ttb
./de-kurzschrift.ctb
./de-vollschrift.ctb
./P1

 

-nouser 不属于任何人的文件

[root@localhost p1]# find / -nouser
find: ‘/proc/22180/task/22180/fd/6’: 没有那个文件或目录
find: ‘/proc/22180/task/22180/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/22180/fd/6’: 没有那个文件或目录
find: ‘/proc/22180/fdinfo/6’: 没有那个文件或目录

/var/spool/mail/nick
/var/spool/mail/cassie
/var/spool/mail/gentoo
/var/spool/mail/fedora
/var/spool/mail/ab
/var/spool/mail/aba

。。。。。

 

一些被删除的用户会遗留下属于他们的文件,但是前面find到的为什么是没有那个文件或目录?

 

4、按大小找、按文件名字找

 

-size

-name

[root@localhost p1]# find / -name passwd
/sys/fs/selinux/class/passwd
/sys/fs/selinux/class/passwd/perms/passwd
/etc/passwd
/etc/pam.d/passwd
/usr/bin/passwd
/usr/share/bash-completion/completions/passwd

如果找不到就不显示出来

[root@localhost p1]# find / -name passwdd
[root@localhost p1]#

 

-size

如果不输入单位,默认是k

 

四舍五入是4k的

 

[root@localhost p1]# find /etc/ -size 4 -ls
116 4 -rw-r–r– 1 root root 1690 8月 30 2017 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
118 4 -rw-r–r– 1 root root 1690 8月 30 2017 /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-Testing-7
33554930 4 -rw-r–r– 1 root root 1664 8月 30 2017 /etc/yum.repos.d/bak/CentOS-Base.repo
67741659 4 -rw-r–r– 1 root root 1764 8月 5 2017 /etc/sasl2/libvirt.conf
34350143 4 -rw-r–r– 1 root root 1781 8月 4 2017 /etc/rdma/sriov-vfs

四舍五入是4M的

[root@localhost p1]# find /etc/ -size 4M -ls
33949277 3668 -rw-r–r– 1 root root 3752457 8月 6 2017 /etc/selinux/targeted/active/policy.kern
67422899 3668 -rw-r–r– 1 root root 3752457 8月 6 2017 /etc/selinux/targeted/policy/policy.30

 

大于4M

[root@localhost p1]# find /etc/ -size +4M -ls
67741609 7332 -r–r–r– 1 root root 7503912 5月 12 17:45 /etc/udev/hwdb.bin

 

小于1k的

[root@localhost p1]# find /etc/ -size -1 -ls
33554499 0 -rw——- 1 root root 0 3月 31 21:31 /etc/crypttab
33554945 0 -rw-r–r– 1 root root 0 11月 6 2016 /etc/environment
33554946 0 -rw-r–r– 1 root root 0 6月 7 2013 /etc/exports
33554955 0 -rw-r–r– 1 root root 0 6月 7 2013 /etc/motd
33554964 0 -rw-r–r– 1 root root 0 11月 6 2016 /etc/subgid
33554965 0 -rw-r–r– 1 root root 0 11月 6 2016 /etc/subuid
67342104 0 -rw-r–r– 1 root root 0 6月 10 2014 /etc/sysconfig/run-parts
33628799 0 -rw——- 1 root root 0 3月 31 21:33 /etc/.pwd.lock
509682 0 -rw——- 1 root root 0 8月 6 2017 /etc/selinux/targeted/semanage.read.LOCK
300822 0 -rw——- 1 root root 0 8月 6 2017 /etc/selinux/targeted/semanage.trans.LOCK
101013546 0 -rw-r–r– 1 root root 0 8月 6 2017 /etc/selinux/targeted/contexts/files/file_contexts.subs
101583085 0 -rw-r–r– 1 root root 0 8月 5 2017 /etc/security/console.apps/xserver
67295439 0 -rw——- 1 root root 0 11月 6 2016 /etc/security/opasswd
33690203 0 -rw——- 1 root root 0 8月 3 2017 /etc/cron.deny
35679762 0 -rw-r–r– 1 root root 0 6月 10 2014 /etc/wvdial.conf
1839247 0 -rw——- 1 root lp 0 8月 4 2017 /etc/cups/classes.conf
1839248 0 -rw-r–r– 1 root lp 0 8月 4 2017 /etc/cups/client.conf
1839253 0 -rw-r–r– 1 root lp 0 8月 4 2017 /etc/cups/lpoptions
1839254 0 -rw——- 1 root lp 0 8月 4 2017 /etc/cups/printers.conf
101558243 0 -rw-r–r– 1 root root 0 8月 4 2017 /etc/ghostscript/9.07/CIDFnmap.local
101558244 0 -rw-r–r– 1 root root 0 8月 4 2017 /etc/ghostscript/9.07/Fontmap.local
101558245 0 -rw-r–r– 1 root root 0 8月 4 2017 /etc/ghostscript/9.07/cidfmap.local
34553231 0 -rw-r–r– 1 root root 0 8月 5 2017 /etc/qemu-kvm/target-x86_64.conf

 

5、按文件类型找

d目录文件

[root@localhost p1]# find /tmp/p1 -type d -ls
102507961 4 drwsrwx— 5 h2 h2 4096 5月 16 20:52 /tmp/p1
2111185 0 drwxrwxr-x 2 root root 6 5月 12 21:18 /tmp/p1/p2
35801367 0 drwxrwxr-x 2 h1 h1 6 5月 12 21:21 /tmp/p1/p1
102517144 0 drwxrwxr-x 2 root root 6 5月 16 20:52 /tmp/p1/P1

f 普通文件

[root@localhost p1]# find /tmp/p1 -type f -ls
102517134 4 -rw-r–r– 1 root root 1348 5月 16 08:47 /tmp/p1/abrt-console-notification.sh
102517135 4 -rw-r–r– 1 root root 381 5月 16 08:47 /tmp/p1/cert-verification.cfg
102517136 4 -rw-r–r– 1 root root 50 5月 16 08:47 /tmp/p1/cifs.idmap.conf
102517137 4 -rw-r–r– 1 root root 52 5月 16 08:47 /tmp/p1/cifs.spnego.conf
102517138 12 -rw-r–r– 1 root root 11267 5月 16 08:47 /tmp/p1/fr-cbifs.ttb
102517139 40 -rw-r–r– 1 root root 37790 5月 16 08:47 /tmp/p1/de-kurzschrift.ctb
102517140 4 -rw-r–r– 1 root root 3289 5月 16 08:47 /tmp/p1/de-vollschrift.ctb

b 是块设备文件

c 字符设备

s 套接字

p 管道

 

6、按文件权限

-perm

[root@localhost p1]# find /tmp/ -perm 644 -ls
102517134 4 -rw-r–r– 1 root root 1348 5月 16 08:47 /tmp/p1/abrt-console-notification.sh
102517135 4 -rw-r–r– 1 root root 381 5月 16 08:47 /tmp/p1/cert-verification.cfg
102517136 4 -rw-r–r– 1 root root 50 5月 16 08:47 /tmp/p1/cifs.idmap.conf
102517137 4 -rw-r–r– 1 root root 52 5月 16 08:47 /tmp/p1/cifs.spnego.conf
102517138 12 -rw-r–r– 1 root root 11267 5月 16 08:47 /tmp/p1/fr-cbifs.ttb
102517139 40 -rw-r–r– 1 root root 37790 5月 16 08:47 /tmp/p1/de-kurzschrift.ctb
102517140 4 -rw-r–r– 1 root root 3289 5月 16 08:47 /tmp/p1/de-vollschrift.ctb
33562202 16 -rw-r–r– 1 root root 15031 5月 12 20:01 /tmp/anaconda.log
35801349 0 -rw-r–r– 1 root root 0 5月 12 19:52 /tmp/storage.log
35801350 0 -rw-r–r– 1 root root 0 5月 12 19:52 /tmp/program.log

 

7、深度 -maxdepth

 

[root@localhost p1]# mkdir -pv /tmp/p1/P1/{a,b}/{c,d}
mkdir: 已创建目录 “/tmp/p1/P1/a”
mkdir: 已创建目录 “/tmp/p1/P1/a/c”
mkdir: 已创建目录 “/tmp/p1/P1/a/d”
mkdir: 已创建目录 “/tmp/p1/P1/b”
mkdir: 已创建目录 “/tmp/p1/P1/b/c”
mkdir: 已创建目录 “/tmp/p1/P1/b/d”

[root@localhost p1]# find /tmp/p1/ -maxdepth 0
/tmp/p1/

[root@localhost p1]# find /tmp/p1/ -maxdepth 1
/tmp/p1/
/tmp/p1/p2
/tmp/p1/p1
/tmp/p1/abrt-console-notification.sh
/tmp/p1/cert-verification.cfg
/tmp/p1/cifs.idmap.conf
/tmp/p1/cifs.spnego.conf
/tmp/p1/fr-cbifs.ttb
/tmp/p1/de-kurzschrift.ctb
/tmp/p1/de-vollschrift.ctb
/tmp/p1/P1

[root@localhost p1]# find /tmp/p1/ -maxdepth 2
/tmp/p1/
/tmp/p1/p2
/tmp/p1/p1
/tmp/p1/abrt-console-notification.sh
/tmp/p1/cert-verification.cfg
/tmp/p1/cifs.idmap.conf
/tmp/p1/cifs.spnego.conf
/tmp/p1/fr-cbifs.ttb
/tmp/p1/de-kurzschrift.ctb
/tmp/p1/de-vollschrift.ctb
/tmp/p1/P1
/tmp/p1/P1/a
/tmp/p1/P1/b

[root@localhost p1]# find /tmp/p1/ -maxdepth 3
/tmp/p1/
/tmp/p1/p2
/tmp/p1/p1
/tmp/p1/abrt-console-notification.sh
/tmp/p1/cert-verification.cfg
/tmp/p1/cifs.idmap.conf
/tmp/p1/cifs.spnego.conf
/tmp/p1/fr-cbifs.ttb
/tmp/p1/de-kurzschrift.ctb
/tmp/p1/de-vollschrift.ctb
/tmp/p1/P1
/tmp/p1/P1/a
/tmp/p1/P1/a/c
/tmp/p1/P1/a/d
/tmp/p1/P1/b
/tmp/p1/P1/b/c
/tmp/p1/P1/b/d

 

0 是目前那个目录文件,1是文件下一个深度,2 是可以再看多一个深度,以此类推

 

7、find后面可以加的操作

-print   -ls   -delete    (-exec -ok)   后面接shell命令

 

[root@localhost p1]# ll
总用量 72
-rw-r–r–+ 1 root root 1348 5月 16 08:47 abrt-console-notification.sh
-rw-r–r–+ 1 root root 381 5月 16 08:47 cert-verification.cfg
-rw-r–r–+ 1 root root 50 5月 16 08:47 cifs.idmap.conf
-rw-r–r–+ 1 root root 52 5月 16 08:47 cifs.spnego.conf
-rw-r–r–+ 1 root root 37790 5月 16 08:47 de-kurzschrift.ctb
-rw-r–r–+ 1 root root 3289 5月 16 08:47 de-vollschrift.ctb
-rw-r–r–+ 1 root root 11267 5月 16 08:47 fr-cbifs.ttb
drwxrwxr-x+ 2 h1 h1 6 5月 12 21:21 p1
drwxrwxr-x+ 4 root root 24 5月 16 21:21 P1
drwxrwxr-x+ 2 root root 6 5月 12 21:18 p2
[root@localhost p1]# ll /tmp/p1/p1/
总用量 0
[root@localhost p1]# find /tmp/p1/ -maxdepth 1 -exec cp -rf {} /tmp/p1/p1 \;
cp: 无法将目录”/tmp/p1/” 复制到自己”/tmp/p1/p1/p1″
cp: 无法将目录”/tmp/p1/p1″ 复制到自己”/tmp/p1/p1/p1″
[root@localhost p1]# ll /tmp/p1/p1/
总用量 72
-rw-r–r–+ 1 root root 1348 5月 16 21:29 abrt-console-notification.sh
-rw-r–r–+ 1 root root 381 5月 16 21:29 cert-verification.cfg
-rw-r–r–+ 1 root root 50 5月 16 21:29 cifs.idmap.conf
-rw-r–r–+ 1 root root 52 5月 16 21:29 cifs.spnego.conf
-rw-r–r–+ 1 root root 37790 5月 16 21:29 de-kurzschrift.ctb
-rw-r–r–+ 1 root root 3289 5月 16 21:29 de-vollschrift.ctb
-rw-r–r–+ 1 root root 11267 5月 16 21:29 fr-cbifs.ttb
drwxr-x—+ 4 root root 26 5月 16 21:29 p1
drwxr-xr-x+ 4 root root 24 5月 16 21:29 P1
drwxr-xr-x+ 2 root root 6 5月 16 21:29 p2

 

-ok和-exec的区别是有没有交互

 

[root@localhost p1]# find /tmp/p1/ -maxdepth 1 -ok cp -rf {} /tmp/p1/p1 \;
< cp … /tmp/p1/ > ? y
cp: 无法将目录”/tmp/p1/” 复制到自己”/tmp/p1/p1/p1″
< cp … /tmp/p1/p2 > ? y
< cp … /tmp/p1/p1 > ? y
cp: 无法将目录”/tmp/p1/p1″ 复制到自己”/tmp/p1/p1/p1″
< cp … /tmp/p1/abrt-console-notification.sh > ? y
< cp … /tmp/p1/cert-verification.cfg > ? y
< cp … /tmp/p1/cifs.idmap.conf > ? y
< cp … /tmp/p1/cifs.spnego.conf > ? y
< cp … /tmp/p1/fr-cbifs.ttb > ? y
< cp … /tmp/p1/de-kurzschrift.ctb > ? y
< cp … /tmp/p1/de-vollschrift.ctb > ? y
< cp … /tmp/p1/P1 > ? y

 

-ok每一个都要自己去确定

 

查找非 目录文件

[root@localhost p1]# find /tmp/p1/p1/ ! -type d -ls
35801390 4 -rw-r–r– 1 root root 1348 5月 16 21:30 /tmp/p1/p1/abrt-console-notification.sh
35801393 4 -rw-r–r– 1 root root 381 5月 16 21:30 /tmp/p1/p1/cert-verification.cfg
35801399 4 -rw-r–r– 1 root root 50 5月 16 21:30 /tmp/p1/p1/cifs.idmap.conf
35807885 4 -rw-r–r– 1 root root 52 5月 16 21:30 /tmp/p1/p1/cifs.spnego.conf
35807886 12 -rw-r–r– 1 root root 11267 5月 16 21:30 /tmp/p1/p1/fr-cbifs.ttb
35807887 40 -rw-r–r– 1 root root 37790 5月 16 21:30 /tmp/p1/p1/de-kurzschrift.ctb
35807888 4 -rw-r–r– 1 root root 3289 5月 16 21:30 /tmp/p1/p1/de-vollschrift.ctb
[root@localhost p1]# ll /tmp/p1/p1
总用量 72
-rw-r–r–+ 1 root root 1348 5月 16 21:30 abrt-console-notification.sh
-rw-r–r–+ 1 root root 381 5月 16 21:30 cert-verification.cfg
-rw-r–r–+ 1 root root 50 5月 16 21:30 cifs.idmap.conf
-rw-r–r–+ 1 root root 52 5月 16 21:30 cifs.spnego.conf
-rw-r–r–+ 1 root root 37790 5月 16 21:30 de-kurzschrift.ctb
-rw-r–r–+ 1 root root 3289 5月 16 21:30 de-vollschrift.ctb
-rw-r–r–+ 1 root root 11267 5月 16 21:30 fr-cbifs.ttb
drwxr-x—+ 4 root root 26 5月 16 21:29 p1
drwxr-xr-x+ 4 root root 24 5月 16 21:29 P1
drwxr-xr-x+ 2 root root 6 5月 16 21:29 p2

! -type d 表示非目录    ! -type f表示非普通文件 等等

直接执行命令比用find再用命令要快

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值