locate/find文件查找工具使用技巧

locate,find

文件查找工具:
文件:根据文件的种属性去找到相应文件
文件查找工具:locate,find

locate :模糊查找工具,非实时查找
locate:根据此前updatedb命令生成的数据库来完成文件查找
注意:对rhel或centos的系统一般而言,每天空闲时间有一个工具程序会自动的被启动起来而运行,把当前系统的每一个文件的文件名,路径都收集起来放在一个数据库中,而后能供locate在查找时使用,
locate不会实时的更新这个数据库,当一些文件发生改变时,locate是不能做到精确查找的

特点:查找速度很快
缺点:非实时查找,结果不精确;模糊查找;
注意:必要时,可手动执行updatedb命令;这个命令作用是重新生成数据库,构建索引,它的速度是相当缓慢,漫长的
find命令:

find命令:通过遍历指定的目标目录,实时查找符合指定属性的文件;

优点:精确匹配
缺点:速度略慢;

find [OPTIONS] [查找路径] [查找条件] [处理动作]
查找路径:默认为当前路径;
查找条件:默认为指定路径下的所有文件;
处理动作:默认为打印至屏幕;

查找条件:
-name "文件名":文件名支持使用globbing
*, ?, [], [^]
-iname "文件名"

[root@localhost ~]# find /etc -name "passwd"
/etc/passwd
/etc/pam.d/passwd
[root@localhost ~]#

[root@localhost ~]# find /etc -name "passwd*"
/etc/passwd
/etc/passwd-
/etc/passwd.OLD
/etc/pam.d/passwd
[root@localhost ~]#

[root@localhost ~]#find /etc -name "*passwd*"
/etc/passwd
/etc/passwd-
/etc/passwd.OLD
/etc/pam.d/passwd
/etc/security/opasswd
[root@localhost ~]#
-user USERNAME: 根据属主查找
[root@localhost ~]# find /tmp -user mark
/tmp/.esd-500
/tmp/.esd-500/socket
/tmp/virtual-mark.E7iX8u
/tmp/pulse-VA9IVBowRMXL
/tmp/pulse-VA9IVBowRMXL/native
/tmp/pulse-VA9IVBowRMXL/pid
/tmp/vmware-mark
/tmp/vmware-mark/vmware-apploader-12873.log
[root@localhost ~]#

[root@localhost ~]#find /tmp -user mark -ls
4980779 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/.esd-500
4980780 0 srwxrwxrwx 1 mark mark 0 Apr 4 01:19 /tmp/.esd-500/socket
4980767 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/virtual-mark.E7iX8u
4980775 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL
4980781 0 srwxrwxrwx 1 mark mark 0 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/native
4980778 4 -rw------- 1 mark mark 6 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/pid
4980788 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/vmware-mark
4980789 8 -rw-r----- 1 mark mark 4549 Apr 4 01:19 /tmp/vmware-mark/vmware-apploader-12873.log
[root@localhost ~]#
-group USERNAME: 根据属组查找
[root@localhost ~]# find /tmp -group mark -ls
4980779 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/.esd-500
4980780 0 srwxrwxrwx 1 mark mark 0 Apr 4 01:19 /tmp/.esd-500/socket
4980767 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/virtual-mark.E7iX8u
4980775 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL
4980781 0 srwxrwxrwx 1 mark mark 0 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/native
4980778 4 -rw------- 1 mark mark 6 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/pid
4980788 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/vmware-mark
4980789 8 -rw-r----- 1 mark mark 4549 Apr 4 01:19 /tmp/vmware-mark/vmware-apploader-12873.log
[root@localhost ~]#

-uid UID: 根据指定UID查找;
[root@localhost ~]#find /tmp -uid 500 -ls
4980818 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.2
4980779 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/.esd-500
4980780 0 srwxrwxrwx 1 500 500 0 Apr 4 01:19 /tmp/.esd-500/socket
4980767 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/virtual-mark.E7iX8u
4980817 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.1
4980775 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL
4980781 0 srwxrwxrwx 1 500 500 0 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/native
4980778 4 -rw------- 1 500 500 6 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/pid
4980788 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/vmware-mark
4980789 8 -rw-r----- 1 500 500 4549 Apr 4 01:19 /tmp/vmware-mark/vmware-apploader-12873.log
4980819 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.
[root@localhost ~]#

-gid GID: 根据指定GID查找;
[root@localhost ~]# find /tmp -gid 500 -ls
4980818 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.2
4980779 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/.esd-500
4980780 0 srwxrwxrwx 1 500 500 0 Apr 4 01:19 /tmp/.esd-500/socket
4980767 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/virtual-mark.E7iX8u
4980817 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.1
4980775 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL
4980781 0 srwxrwxrwx 1 500 500 0 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/native
4980778 4 -rw------- 1 500 500 6 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL/pid
4980788 4 drwx------ 2 500 500 4096 Apr 4 01:19 /tmp/vmware-mark
4980789 8 -rw-r----- 1 500 500 4549 Apr 4 01:19 /tmp/vmware-mark/vmware-apploader-12873.log
4980819 0 -rw-rw-r-- 1 500 500 0 Apr 5 06:01 /tmp/mark.
[root@localhost ~]#
-nouser: 查找没有属主的文件;
[root@localhost ~]# find /tmp -nouser -ls

-nogroup:查找没有属组的文件;
[root@localhost ~]# find /tmp -nogroup -ls
组合查找条件:
与:-a, 查找条件1 -a 查找条件2 -a ...
所有条件必须同时满足
或:-o, 查找条件1 -o 查找条件2 -o ...
满足其中一个条件即可
非:-not, !
-not 查找条件
[root@localhost ~]# find /tmp name "*mark*" -o -uid 500

找不是root
[root@localhost ~]# find /tmp -not -user root -ls
4980793 0 srwxrwxrwx 1 gdm gdm 0 Apr 5 00:02 /tmp/.ICE-unix/2464
4980818 0 -rw-rw-r-- 1 mark mark 0 Apr 5 06:01 /tmp/mark.2
4980765 4 drwx------ 2 gdm gdm 4096 Apr 5 00:02 /tmp/pulse-YW29q1RHDFtl
4980799 0 srwxrwxrwx 1 gdm gdm 0 Apr 5 00:02 /tmp/pulse-YW29q1RHDFtl/native
4980798 4 -rw------- 1 gdm gdm 5 Apr 5 00:02 /tmp/pulse-YW29q1RHDFtl/pid
4980779 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/.esd-500
找包含.txt
[root@localhost ~]# find /tmp -name "*.txt"
/tmp/mark.txt
找不包含.txt
[root@localhost ~]#find /tmp -not -name "*.txt"
/tmp
/tmp/VMwareDnD
/tmp/.ICE-unix
/tmp/.ICE-unix/2464
/tmp/grep2015-04-05-01-30-21
/tmp/.X11-unix
/tmp/.X11-unix/X0
/tmp/vmware-root
/tmp/vmware-root/vmware-db.pl.2067
不是txt或者not结尾
[root@localhost ~]# find /tmp -not -name "*.txt" -a -not -name "*.out"
/tmp
/tmp/VMwareDnD
/tmp/.ICE-unix
/tmp/.ICE-unix/2464
/tmp/grep2015-04-05-01-30-21
/tmp/.X11-unix
/tmp/.X11-unix/X0
/tmp/vmware-root
/tmp/vmware-root/vmware-db.pl.2067
# find /tmp -not \( -name "*.txt" -o -name "*.out" \)
# find /tmp -not -name "*.txt" -a -not -name "*.out"
查找条件(2)

-type TYPE: 根据文件类型查找
f: 普通文件
d: 目录文件
l: 符号链接文件
b: 块设备
c: 字符设备文件
p: 命令名管道文件
s: 套接字文件
find /tmp -type [OPTIONS] -ls
查找目录文件
[root@localhost ~]#find /tmp -type d -ls
4980737 4 drwxrwxrwt 16 root root 4096 Apr 5 06:41 /tmp
4984333 4 drwxrwxrwt 2 root root 4096 Apr 4 09:17 /tmp/VMwareDnD
4980741 4 drwxrwxrwt 2 root root 4096 Apr 5 00:02 /tmp/.ICE-unix
4980790 4 drwxrwxrwt 2 root root 4096 Apr 5 00:02 /tmp/.X11-unix
4980742 4 drwxr-xr-x 2 root root 4096 Apr 5 00:02 /tmp/vmware-root
4984304 4 drwx------ 2 root root 4096 Apr 5 00:02 /tmp/vmware-root-2117547395
4984322 4 drwxr-xr-x 2 root root 4096 Apr 4 09:16 /tmp/vmware-config0
2 4 drwxr-xr-x 4 root root 4096 Apr 4 20:27 /tmp/1mark
12 4 drwxr-xr-x 2 root root 4096 Apr 4 20:27 /tmp/1mark/test1
11 16 drwx------ 2 root root 16384 Apr 4 03:56 /tmp/1mark/lost+found
4980754 4 drwx------ 2 root lp 4096 Apr 5 00:02 /tmp/vmware-root-2117940668
4980765 4 drwx------ 2 gdm gdm 4096 Apr 5 00:02 /tmp/pulse-YW29q1RHDFtl
4980779 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/.esd-500
4980767 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/virtual-mark.E7iX8u
4980794 4 drwx------ 2 gdm gdm 4096 Apr 5 00:02 /tmp/orbit-gdm
4980775 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/pulse-VA9IVBowRMXL
4980788 4 drwx------ 2 mark mark 4096 Apr 4 01:19 /tmp/vmware-mark
[root@localhost ~]#

查找普通文件
[root@localhost ~]# find /tmp -type f -ls
4980815 4 -rw-r--r-- 1 root root 534 Apr 5 01:30 /tmp/grep2015-04-05-01-30-21
4980776 4 -rw-r--r-- 1 root root 90 Apr 5 00:02 /tmp/vmware-root/vmware-db.pl.2067
4980782 4 -rw-r--r-- 1 root root 90 Apr 5 00:02 /tmp/vmware-root/vmware-db.pl.2082
4980746 4 -rw-r--r-- 1 root root 90 Apr 4 02:49 /tmp/vmware-root/vmware-db.pl.1995
4984326 4 -rw-r--r-- 1 root root 90 Apr 4 09:17 /tmp/vmware-root/vmware-db.pl.12032
4980750 4 -rw-r--r-- 1 root root 90 Apr 4 02:49 /tmp/vmware-root/vmware-db.pl.2015
4980761 4 -rw-r--r-- 1 root root 90 Apr 4 20:13 /tmp/vmware-root/vmware-db.pl.2025
4980760 4 -rw-r--r-- 1 root root 90 Apr 4 20:13 /tmp/vmware-root/vmware-db.pl.2020
-size [+|-]#UNIT:
常用单位有:k, M, G

#UNIT: #-1 < 2K <= #
2k:1.8k, 1.9k, 1.1k

-#UNIT:x <= #-1
-2k: 1k, 0.9k

+#UNIT: x > #
+2k: 2.1k, 3.1k

查找2k的文件
[root@localhost ~]# find /etc -size 2k -ls
5638108 4 -rw-r--r-- 1 root root 1189 Nov 22 2013 /etc/bash_completion.d/scl.bash
5636317 4 -rw-r--r-- 1 root root 1362 Aug 22 2010 /etc/pbm2ppa.conf
5636865 4 -rwxr-xr-x 1 root root 1465 Jul 22 2014 /etc/sysconfig/network-scripts/ifdown-sit
5636866 4 -rwxr-xr-x 1 root root 1434 Jul 22 2014 /etc/sysconfig/network-scripts/ifdown-tunnel
5636862 4 -rwxr-xr-x 1 root root 1481 Jul 22 2014 /etc/sysconfig/network-scripts/ifdown-post
5636878 4 -rwxr-xr-x 1 root root 1925 Jul 22 2014 /etc/sysconfig/network-scripts/ifup-routes
5636863 4 -rwxr-xr-x 1 root root 1064 Jul 22 2014 /etc/sysconfig/network-scripts/ifdown-ppp
5636883 4 -rwxr-xr-x 1 root root 1219 Jul 22 2014 /etc/sysconfig/network-scripts/net.hotplug
5636852 4 -rw-r--r-- 1 root root 1154 Oct 16 07:53 /etc/sysconfig/init
5637349 4 -rw------- 1 root root 1988 Oct 15 07:30 /etc/sysconfig/ip6tables-config
5637937 4 -rw-r--r-- 1 root root 1212 Oct 15 10:24 /etc/sysconfig/kdump
5638035 4 -rw-r--r-- 1 root root 1459 Jul 19 2013 /etc/sysconfig/prelink
5636745 4 -rw------- 1 root root 1974 Oct 15 07:30 /etc/sysconfig/iptables-conf
根据时间戳查找:
以“天”为单位
-atime [+|-]#
#: #= < x < #+1
-#: x < #
+#: x >= #+1

-mtime [+|-]#

-ctime [+|-]#
以“分钟”为单位
-amin [+|-]#
-mmin
-cmin
100分钟以内被访问过的文件
[root@localhost ~]#find /etc -amin -100
/etc
/etc/group
/etc/shadow
/etc/yum.repos.d
/etc/passwd
/etc/passwd-
/etc/group-
/etc/gshadow
/etc/default
/etc/default/useradd
/etc/cups
/etc/pam.d/su
/etc/pam.d/system-auth-ac
/etc/gshadow-
/etc/shadow-
[root@localhost ~]#
根据权限查找:
-perm [+|-]MODE
MODE: 与MODE精确匹配
+MODE:任何一类用户的权限只要能包含对其指定的任何一位权限即可;
-MODE:每一类用户的权限都包含对其指定的所有权限;
-222,666,664
注意:0表示不检查
查找777权限的文件
[root@localhost ~]#find /etc -perm 777 -ls
5638168 0 lrwxrwxrwx 1 root root 22 Apr 4 09:12 /etc/grub.conf -> ../boot/grub/grub.conf
5636867 0 lrwxrwxrwx 1 root root 18 Apr 4 08:54 /etc/sysconfig/network-scripts/ifup -> ../../../sbin/ifup
5636873 0 lrwxrwxrwx 1 root root 9 Apr 4 08:54 /etc/sysconfig/network-scripts/ifup-isdn -> ifup-ippp
5636861 0 lrwxrwxrwx 1 root root 11 Apr 4 08:54 /etc/sysconfig/network-scripts/ifdown-isdn -> ifdown-ippp
5636856 0 lrwxrwxrwx 1 root root 20 Apr 4 08:54 /etc/sysconfig/network-scripts/ifdown -> ../../../sbin/ifdown
5637365 0 lrwxrwxrwx 1 root root 17 Apr 4 08:58 /etc/sysconfig/selinux -> ../selinux/config
5636720 0 lrwxrwxrwx 1 root root 14 Apr 4 08:54 /etc/system-release -> centos-release
5636189 0 lrwxrwxrwx 1 root root 10 Apr 4 08:54 /etc/rc3.d -> rc.d/rc3.d
5636192 0 lrwxrwxrwx 1 root root 10 Apr 4 08:54 /etc/rc6.d -> rc.d/rc6.d
5636178 0 lrwxrwxrwx 1 root root 11 Apr 4 08:47 /etc/init.d -> rc.d/init.d

查找属组或者属主或者其他,任何有一位有写权限的文件
[root@localhost ~]# find /etc -perm +222 -ls
5637475 4 -rw-r--r-- 1 root root 644 Apr 4 06:12 /etc/blkid/blkid.tab
5638179 12 -rw-r--r-- 1 root root 12288 Apr 4 09:13 /etc/aliases.db
5636191 0 lrwxrwxrwx 1 root root 10 Apr 4 08:54 /etc/rc5.d -> rc.d/rc5.d

范例:
1、查找/var目录下属主为root,且属组为mail的所有文件或目录;
# find /var -user root -a -group mail -ls

2、查找当前系统上没有属主或属组的文件;
进一步:查找当前系统上没有属主或属组,且最近3天内曾被访问过的文件或目录;
# find / \( -nouser -o -nogroup \) -a -atime -3

3、查找/etc目录下所有用户都有写权限的文件;
# find /etc -perm -222 -ls
进一步:查找/etc/目录下所有用户都没有写权限的文件;
# find /etc -not -perm +222

4、查找/etc目录下大于1M,且类型为普通文件的所有文件;
# find /etc -size +1M -a -type f

5、查找/etc/init.d/目录下,所有用户都有执行权限,且其它用户有写权限的文件;
# find /etc/init.d/ -perm -113

6、查找/usr目录下不属于root、bin或hadoop的文件;
# find /usr -not -user root -a -not -user bin -a -not -user hadoop -ls
# find /usr -not \( -user root -o -user bin -o -user hadoop \) -ls

7、查找/etc/目录下至少有一类用户没有写权限的文件;
# find /etc -not -perm -222

8、查找/etc目录下最近一周内其内容被修改过,且不属于root或hadoop的文件;
# find /etc -mtime -7 -a -not \( -user root -o -user hadoop \)
处理动作:
-print: 默认动作,打印至屏幕;
-ls: 显示找到的文件的详细属性;
-exec COMMAND {} \;
-ok COMMAND {} \;

注意:find命令会一次性找到所有符合条件的文件,并一同传递给后面的命令;但有些命令不能接受过长的参数;解决办法:

find | xargs COMMAND

find /tmp -not -user root -exec mv {} {}.notroot \;

0 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2015-04-05分类: Sys Basics

标签: linux基础

发表评论