linux文件查找命令locate和find
2017-06-28 22:13
337 查看
1.locate命令
非实时地,直接在系统文件数据库中查找,速度比较快,但查找结果不准确。linux在运行过程中会将文件系统数据写入一个数据库(每隔一段时间(比如一个小时)才会写入数据库),locate命令就是从该数据库中查找的。
上述过程中我们先新创建一个新文件hello.txt,然后使用locate查找,结果没有找到/home/jie/hello.txt这条记录,然后我们切换为root用户,使用updatedb命令手动更新数据库,更新完成之后切回jie用户再使用locate命令进行查找,这次就能看到/home/jie/hello.txt这条记录了。
locate命令还有一个问题就是只进行模糊匹配
可以看到,路径中含有passwd的文件也被显示出来了,这个结果可能不是我们想要的,locate命令平时使用较少,就不多作介绍。
2.find命令
实时查找文件命令,通过遍历指定目录所有文件进行查找,速度较慢,但可以根据多种标准进行查找,功能强大。
使用方法:
find 查找路径 查找标准 查到以后进行的操作
查找路径 默认为当前路径
查找标准 默认标准为查找路径下的所有文件
查到以后进行的操作 默认为输出到屏幕上
查找路径这个没什么好说的,主要研究一下查找标准:
最后就是找到以后的操作,默认为-print显示在屏幕上
非实时地,直接在系统文件数据库中查找,速度比较快,但查找结果不准确。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目录下的文件 |
选项参数 | 含义 | 例子 |
---|---|---|
显示在屏幕上 | find . -print 显示在屏幕上 | |
-ls | 类似ls- l显示匹配文件的详细信息 | find . -ls 输出详细信息 |
-exec command \; | 可以使用{}来引用文件名 注意\;必须末尾否则会报错 | find ./test -exec chmod o-x {} \; 将查找到的文件的其它用户权限中的执行权限去掉 |
-ok command \; | 与-exec功能相同,只是每一次操作需要用户确认 | 同上 |
相关文章推荐
- Linux 入门之文件查找(find、locate)命令
- Linux的五个文件查找命令:find,locate,whereis,which,type
- Linux文件查找命令详解-which whereis find locate
- 如何查找Linux文件 存放在哪(地点) locate find whereis which 命令
- Linux五个文件查找命令--which、whereis、find、locate和type
- Linux下查找文件:which、whereis、locate、find 命令的区别
- linux中Find、whereis、locate、which查找文件命令总结
- Linux之文件查找命令locate与find详解
- Linux文件查找命令具体解释-which whereis find locate
- Linux内、外部命令的使用、文件权限、文件查找(find、locate、grep)
- linux 下查找文件或者内容命令whereis,locate ,find
- linux下find和locate文件查找命令详解
- Linux文件查找命令之locate与find
- Linux下which、whereis、locate、find 命令查找文件
- Linux命令:文件查找相关 Whereis、find和locate+updatedb
- linux中Find、whereis、locate、which查找文件命令详解
- Linux下文件查找命令which、whereis、locate、find的区别
- Linux下查找文件的命令:locate 和 find
- linux下查找文件命令:locate 和 find
- Linux下which、whereis、locate、find 命令查找文件