欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

linux下使用locate命令快速查找文件

shiping1 的头像

updatedb: can not open a temporary file for `/var/lib/ml

 

 (2013-01-23 12:01:24)
标签:

linux

 

 

updatedb

 

 

locate

 

 

it

 

 分类: Linux学习
locate查找命令不能查找不我们最新创建的文件,因为系统的数据库还没有更新,若果我们手动更新数据库便可以查到。
在使用updatedb时有时会出现下面的错误updatedb: can not open a temporary file for `/var/lib/mlocate/mlocate.db'。这是

因为权限不够,我们要切换到root 用户下,su - root ,然后再updatedb就可以了。updatedb: <wbr>can <wbr>not <wbr>open <wbr>a <wbr>temporary <wbr>file <wbr>for <wbr>`/var/lib/ml


来自 http://blog.sina.com.cn/s/blog_87f166cf010175yo.html


linux下使用locate命令快速查找文件 (2011-04-15 22:39:46)

 

linux下使用locate命令快速查找文件:

先来看一下updatedb命令:

[root@localhost huage]# which updatedb

/usr/bin/updatedb

[root@localhost huage]# man updatedb

updatedb(8)                                                        updatedb(8)

 

NAME

       updatedb update database for mlocate

 

DESCRIPTION

       updatedb  creates  or  updates  database used by locate(1).  If the

       database already exists, its data is reused to avoid rereading direc-

       tories that have not changed.

 

       updatedb  is  usually  run  daily  by  cron(8)  to update the default

       Database.

  

FILES

       /etc/updatedb.conf

              configuration file.  See updatedb.conf(5).

 

       /var/lib/mlocate/mlocate.db

              The database updated by default.

 

[root@localhost huage]# locate mlocate |grep etc

/etc/cron.daily/mlocate.cron

[root@localhost huage]# 

 

通过man page可以看到updatedb是一个管理工具,它用来更新索引数据库mlocate.db,所以普通用户无法使用updatedb命令。

 

[huage@localhost ~]$ updatedb

updatedb: can not open temporary file for `/var/lib/mlocate/mlocate.db'

[huage@localhost ~]$ su

Password: 

[root@localhost huage]# updatedb

[root@localhost huage]# pwd

/home/huage

[root@localhost huage]# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

[root@localhost huage]# touch sina-huage

[root@localhost huage]# ls

Desktop    Downloads  Pictures  sina-huage  Videos

Documents  Music      Public    Templates

[root@localhost huage]# locate sina-huage

因为sina-huage文件是刚才才建立的,而mlocate数据库一天更新一次,所以索引数据库中自然没有建立刚才所建立的文件的索引,因此我们需要用updatedb命令进行即时更新。

 

[root@localhost huage]# updatedb

[root@localhost huage]# locate sina-huage

/home/huage/sina-huage

[root@localhost huage]# rm -rf sina-huage 

[root@localhost huage]# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

[root@localhost huage]# locate sina-huage

/home/huage/sina-huage

locate命令默认情况下,不去检查索引数据库中的文件在查询时是否仍然存在

 

       By  default,  locate  does not check whether files found in database still

       exist.  locate can never report files created after the most recent update

       of the relevant database.

 

[root@localhost huage]# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

[root@localhost huage]# locate -e sina-huage

[root@localhost huage]# ls

Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos

locate命令的-e参数就是在输出结果前先确认文件是否仍然存在,只有存在才会将查找结果输出

 

       -e, --existing

              Print  only  entries  that refer to files existing at the time

              locate is run.

 

 

[root@localhost huage]# cat /etc/issue

Red Hat Enterprise Linux Server release 6.0 (Santiago)

Kernel \r on an \m

查看系统的版本

 

[root@localhost huage]# exit

exit

[huage@localhost ~]$ locate *locate.db

普通用户使用locate命令查找文件时,有些结果是不会输出的,因为某些文件普通用户没有权限访问

[huage@localhost ~]$ su

Password: 

[root@localhost huage]# locate *locate.db

/var/lib/mlocate/mlocate.db

 

[root@localhost huage]# locate *locate.db*

/usr/share/man/man5/mlocate.db.5.gz

/var/lib/mlocate/mlocate.db

/var/lib/mlocate/mlocate.db.eHxit1

从上面可以看出,如果查找文件时,文件的前面加了星号,那最好在末尾也加上吧,不然有些结果可能不会输出

 

[root@localhost huage]# locate mlocate.db

/usr/share/man/man5/mlocate.db.5.gz

/var/lib/mlocate/mlocate.db

/var/lib/mlocate/mlocate.db.eHxit1

[root@localhost huage]# locate -e mlocate.db

/usr/share/man/man5/mlocate.db.5.gz

/var/lib/mlocate/mlocate.db

从这里可以看出mlocate.db.eHxit1是不存在的

 

[root@localhost huage]# locate mlocate.db |xargs ls -al

ls: cannot access /var/lib/mlocate/mlocate.db.eHxit1: No such file or directory

-rw-r--r--. root root       1626 Jul 20  2009 /usr/share/man/man5/mlocate.db.5.gz

-rw-r-----. root slocate 2516026 Apr 15 23:04 /var/lib/mlocate/mlocate.db

 

从输出的第一行也可以看到mlocate.db.eHxit1是不存在的,第三行中可以看到一个名为slocate的组

 

locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的,locate命令在搜寻数据库时比由整个由硬盘资料来搜寻资料来得快,但较差劲的是locate所找到的档案若是最近才建立或刚更名的,可能会找不到,在内定值中,updatedb每天会跑一次,可以由修改crontab来更新设定值。(etc/crontab)

 

  locate指定用在搜寻符合条件的档案,它会去储存档案与目录名称的数据库内,寻找合乎范本样式条件的档案或目录,可以使用特殊字元(如”*””?”等)来指定范本样式,如指定范本为hua*sina, locate会找出所有起始字串为hua且结尾为sina的档案或目录,如名称为huagesina的档案,目录名称为hua_sina则会列出该目录下包括子目录在内的所有档案;如果范本为mlocate,则会列出所有文件名中包含mlocate的档案或目录;如果范本为*locate,则会列出所有文件名中包含locate且以locate结尾的档案或目录;如果范本为*locate*,则会列出所有文件名中包含locate的档案或目录。

 

 

  locate指令和find找寻档案的功能类似,但locate是透过updatedb程序将硬盘中的所有档案和目录资料先建立一个索引数据库,在执行loacte时直接找该索引,查询速度会较快,索引数据库一般是由操作系统管理,但也可以直接使用updatedb命令来更新索引数据库。

 

 

       updatedb:根据/etc/updatedb.conf 的设定去搜寻系统硬盘内的档案,并更新/var/lib/mlocate/mlocate.db内的资料库档案;

       locate:依据/var/lib/mlocate/mlocate.db内的资料库记载,找出包含使用者输入的关键字的档案。

来自 http://blog.sina.com.cn/s/blog_716844910100r3sf.html


普通分类: