您的位置:首页 > 运维架构 > Linux

linux文件查找命令locate和find

2017-06-28 22:13 337 查看
1.locate命令

非实时地,直接在系统文件数据库中查找,速度比较快,但查找结果不准确。linux在运行过程中会将文件系统数据写入一个数据库(每隔一段时间(比如一个小时)才会写入数据库),locate命令就是从该数据库中查找的。

jie$ touch hello.txt
jie$ ls
code     Documents  examples.desktop  gitstudy1  Music      Pictures  q.save     Videos
Desktop  Downloads  gitstudy          hello.txt  operncode  Public    Templates
jie$ pwd
/home/jie
jie$ locate hello.txt
/usr/share/doc/syslinux-common/asciidoc/hello.txt
jie$ su
Password:
root$ updatedb #手动更新数据库数据耗时比较长
root$ exit
jie$ locate hello.txt
/home/jie/hello.txt
/usr/share/doc/syslinux-common/asciidoc/hello.txt


上述过程中我们先新创建一个新文件hello.txt,然后使用locate查找,结果没有找到/home/jie/hello.txt这条记录,然后我们切换为root用户,使用updatedb命令手动更新数据库,更新完成之后切回jie用户再使用locate命令进行查找,这次就能看到/home/jie/hello.txt这条记录了。

locate命令还有一个问题就是只进行模糊匹配

jie$ locate passwd
/etc/passwd
/etc/passwd-
/etc/cron.daily/passwd
/etc/init/passwd.conf
/etc/pam.d/chpasswd
/etc/pam.d/passwd
/etc/security/opasswd
/home/jie/code/Python-3.6.1/Lib/test/keycert.passwd.pem
/home/jie/code/Python-3.6.1/Lib/test/ssl_key.passwd.pem
/usr/bin/gpasswd
.............


可以看到,路径中含有passwd的文件也被显示出来了,这个结果可能不是我们想要的,locate命令平时使用较少,就不多作介绍。

2.find命令

实时查找文件命令,通过遍历指定目录所有文件进行查找,速度较慢,但可以根据多种标准进行查找,功能强大。

使用方法:

find 查找路径 查找标准 查到以后进行的操作

查找路径 默认为当前路径

查找标准 默认标准为查找路径下的所有文件

查到以后进行的操作 默认为输出到屏幕上

jie$ find # 查找路径默认 查找标准默认 找到以后的操作默认
.
./.bash_history
./.q.swp
./.xsession-errors
./.bashrc
./.bash_logout
./.sudo_as_admin_successful
./.cache
./.cache/ibus
./.cache/ibus/bus
./.cache/ibus/bus/registry
./.cache/compizconfig-1
./.cache/compizconfig-1/ezoom.pb
.......
jie$ find code/  # 查找路径code/ 查找标准默认 找到以后的操作默认
code/
code/shellcode
code/shellcode/test_let.sh
code/shellcode/testfile
code/shellcode/testout
code/shellcode/sed.txt
code/shellcode/test3file
code/shellcode/stdout.txt
code/shellcode/test.txt
code/shellcode/stdout
code/shellcode/testerr
code/shellcode/stderr.txt
code/shellcode/Makefile
code/makestudy
code/makestudy/add_test.c
code/makestudy/add
code/makestudy/add/add.o
.......


查找路径这个没什么好说的,主要研究一下查找标准:

选项参数含义例子
-name “filename”查到路径下文件名为filename的文件(夹),注意filename支持通配符* ? []find . -name “*.txt” 查找当前文件夹下所有文件名以.txt结尾的文件(夹)
-iname “filename”不区分文件名大小写find . -iname a 查找当前文件夹下所有文件名为A或a的文件(夹)
-user usrname查找路径下属主为usrname的文件(夹)find /etc -user root 查找/etc下属主为root的文件(夹)
-group groupname查找路径下属组为groupname的文件(夹)find /etc -group root 查找/etc下属组为root的文件(夹)
-uid uid根据 uid查找find /etc -uid 1000 查找/etc目录下用户id为1000的文件(夹)
-gid gid根据 gid查找find /etc -gid 1000 查找/etc目录下用户组id为1000的文件(夹)
-nouser查找路径下没有属主的文件find /home -nouser 查找/home目录下没有属主的文件(夹)
-nogroup查找路径下没有属组的文件find /home -nogroup 查找/home目录下没有属组的文件(夹)
-type filetype根据文件类型filetype(f:普通文件,d:目录,c:字符设备等)查找文件find /home -type d 查找/home目录下的文件夹
-size filesize根据文件大小查找文件find /tmp -size 10M 查找/tmp目录下大小在(9M,10M]区间的文件(夹)
-a组合条件与find /tmp -nouser -a -type d 查找/tmp目录下没有属主的文件夹
-o组合条件或find /tmp -nouser -o -type d 查找/tmp目录下没有属主的文件(夹)或者文件夹
-not组合条件非find /tmp -not -type d 查找/tmp目录下的文件
最后就是找到以后的操作,默认为-print显示在屏幕上

选项参数含义例子
-print显示在屏幕上find . -print 显示在屏幕上
-ls类似ls- l显示匹配文件的详细信息find . -ls 输出详细信息
-exec command \;可以使用{}来引用文件名 注意\;必须末尾否则会报错find ./test -exec chmod o-x {} \; 将查找到的文件的其它用户权限中的执行权限去掉
-ok command \;与-exec功能相同,只是每一次操作需要用户确认同上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: