前些天在跟大伙讨论linux查找文件速度的问题;大家都一致决定
find [path] -name "keyword"
太慢了,而且太浪费cpu了;有木有一个比较好的idea吗?当然有了。
locate 初入linux道行的,或者是接触一年半载的人很少经常用他;可能他比find多了两个字母吧;并且没有find好记;所以就被初学者遗忘在角落里面;
[10:53 @ stm32 ]$ locate -h
Usage: locate [OPTION]... [PATTERN]...
Search for entries in a mlocate database.
-b, --basename match only the base name of path names
-c, --count only print number of found entries
-d, --database DBPATH use DBPATH instead of default database (which is
/var/lib/mlocate/mlocate.db)
-e, --existing only print entries for currently existing files
-L, --follow follow trailing symbolic links when checking file
existence (default)
-h, --help print this help
-i, --ignore-case ignore case distinctions when matching patterns
-l, --limit, -n LIMIT limit output (or counting) to LIMIT entries
-m, --mmap ignored, for backward compatibility
-P, --nofollow, -H don't follow trailing symbolic links when checking file
existence
-0, --null separate entries with NUL on output
-S, --statistics don't search for entries, print statistics about each
used database
-q, --quiet report no error messages about reading databases
-r, --regexp REGEXP search for basic regexp REGEXP instead of patterns
--regex patterns are extended regexps
-s, --stdio ignored, for backward compatibility
-V, --version print version information
-w, --wholename match whole path name (default)
Report bugs to [email protected]
locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更 新,updatedb是由cron daemon周期性建立的,locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快,但较差劲的是locate所找到的档案若是最近才建立或 刚更名的,可能会找不到,在内定值中,updatedb每天会跑一次,可以由修改crontab来更新设定值。(etc/crontab)locate指定用在搜寻符合条件的档案,它会去储存档案与目录名称的数据库内,寻找合乎范本样式条件的档案或目录录,可以使用特殊字元(如 ”*”或”?”等)来指定范本样式,如指定范本为kcpa*ner, locate会找出所有起始字串为kcpa且结尾为ner的档案或目录,如名称为kcpartner若目录录名称为kcpa_ner则会列出该目录下包括 子目录在内的所有档案。
locate指令和find找寻档案的功能类似,但locate是透过update程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在执行 loacte时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接下达update强迫系统立即修改索引数据库。
不过第一次在执行update后再使用locate寻找档案常会失败,此时就要执行slocate ˉu该命令(也可执行updatedb指令,其效果相同)来更新slocate数据库,该命令会在/usr/sbin下产生slocate执行档,再由 locate到此数据库寻找所要找的资料。
locate:该命令在运行时需要后台索引的数据库作为支撑,在 Ubuntu 中这个数据库文件位于 /var/cache/locate/locatedb。一般来说,这个数据库文件每天是通过 cron 自动更新的。如果不幸没有得到更新,那么可以执行 sudo updatedb 来手动更新。
[10:59 @ stm32 ]$ sudo updatedb
[10:59 @ stm32 ]$ locate xxxxx
[11:00 @ stm32 ]$ locate xxxx
/home/chenzhenwei/embededproject/platform/s3c24x0/kernel/linux-3.0/arch/arm/mach-omap2/sdram-numonyx-m65kxxxxam.h
/home/chenzhenwei/embededproject/platform/s3c24x0/kernel/linux-3.0/drivers/scsi/3w-xxxx.c
/home/chenzhenwei/embededproject/platform/s3c24x0/kernel/linux-3.0/drivers/scsi/3w-xxxx.h
/home/chenzhenwei/embededproject-code/platform/s3c24x0/kernel/linux-3.0/arch/arm/mach-omap2/sdram-numonyx-m65kxxxxam.h
/home/chenzhenwei/embededproject-code/platform/s3c24x0/kernel/linux-3.0/drivers/scsi/3w-xxxx.c
/home/chenzhenwei/embededproject-code/platform/s3c24x0/kernel/linux-3.0/drivers/scsi/3w-xxxx.h
/home/chenzhenwei/fl2440/kernel/linux-3.0/arch/arm/mach-omap2/sdram-numonyx-m65kxxxxam.h
/home/chenzhenwei/fl2440/kernel/linux-3.0/drivers/scsi/3w-xxxx.c
/home/chenzhenwei/fl2440/kernel/linux-3.0/drivers/scsi/3w-xxxx.h
/home/chenzhenwei/fl2440/linux-3.0/arch/arm/mach-omap2/sdram-numonyx-m65kxxxxam.h
/home/chenzhenwei/fl2440/linux-3.0/drivers/scsi/3w-xxxx.c
/home/chenzhenwei/fl2440/linux-3.0/drivers/scsi/3w-xxxx.h
/lib/modules/2.6.32-220.el6.i686/kernel/drivers/scsi/3w-xxxx.ko
/opt/buildroot-2011.11/output/toolchain/linux-2.6.37/arch/arm/mach-omap2/sdram-numonyx-m65kxxxxam.h
/opt/buildroot-2011.11/output/toolchain/linux-2.6.37/drivers/scsi/3w-xxxx.c
/opt/buildroot-2011.11/output/toolchain/linux-2.6.37/drivers/scsi/3w-xxxx.h
/usr/share/foomatic/db/source/printer/Panasonic-KX-Pxxxx_24-pin.xml
/usr/src/kernels/2.6.32-220.el6.i686/include/config/blk/dev/3w/xxxx
/usr/src/kernels/2.6.32-220.el6.i686/include/config/blk/dev/3w/xxxx/raid.h
[11:00 @ stm32 ]$
也许这个可以给你带来很多方便;