Linux文件搜索命令
文件搜索命令:locate
locate 文件名
- 在后台数据库中按文件名搜索,搜索速度很快(比find命令要快得多)
- locate命令所搜索的后台数据库的位置:/var/bin/mlocate
- 支持模糊搜索
这里要说明一下:locate的数据库是每天更新一次,所以新创建的文件是无法搜索到的,但是可以通过updatedb命令来更新数据库,更新后即可查找到。
请仔细阅读以下命令和结果:
[root@localhost ~]# ls
anaconda-ks.cfg
[root@localhost ~]# locate anaconda-k
/root/anaconda-ks.cfg
[root@localhost ~]# touch abcccc.txt wwbcccctt.txt
[root@localhost ~]# ls
abcccc.txt anaconda-ks.cfg wwbcccctt.txt
[root@localhost ~]# locate bcccc
[root@localhost ~]# updatedb
[root@localhost ~]# locate bcccc
/root/abcccc.txt
/root/wwbcccctt.txt
[root@localhost ~]#
此外,locate 需要符合一个配置文件的筛选规则,该文件为:
/etc/updatedb.conf
我们看一下文件的内容(通过命令 vi [文件名] 查看)
PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfs"
PRUNENAMES = ".git .hg .svn"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/spool/cups /var/spool/squid /var/tmp"
其中,PRUNEFS、PRUNENAMES、PRUNEPATHS分别列出了三种不同类别的文件,在第一行中的PRUNE_BIND_MOUNTS如果是yes,那么符合这些条件的文件将不会被搜索,如果是no,那么即可以搜索得到。
默认为yes,所以当你想搜索 tmp/ 目录下的文件时,是无法搜索到的。
命令搜索命令:whereis、which
whereis
whereis 命令名
选项:
- -b : 只查找可执行文件(即只查找命令执行文件所在位置)
- -m : 只查找帮助文件
whereis 只能搜索系统的命令,它搜索命令文件所在的路径和帮助文档的位置
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost ~]# whereis -b ls
ls: /usr/bin/ls
[root@localhost ~]# whereis -m ls
ls: /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost ~]#
which
which 命令名
which 搜索命令所在位置和文件别名
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]# which pwd
/usr/bin/pwd
通过命令我们得知,ls是ls --color=auto的别名,这里第一行显示命令的别名,第二行显示文件所在位置。如果文件没有别名,则不显示别名,只显示路径。比如pwd。
PATH环境变量
whereis 和 which 命令都是在这个环境变量的路径下寻找的,我们可以想一下,为什么执行 ls 可以输出结果?而不用写 ls 的绝对路径?因为 ls 这个命令已经在环境变量的路径中配置,只要在环境变量下,就可以直接输入命令。
我们来看一下PATH变量的路径:
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
我们在 Linux命令基本格式及目录处理命令 有讲过:
- /bin 命令保存目录(普通用户权限)
- /sbin 命令保存目录(root权限)
这些路径就是命令所在的路径,也都保存在PATH环境变量中。
文件搜索命令:find
find [搜索范围][搜索条件][文件名]
搜索条件:
- -name : 按名字搜索
- -iname : 按名字搜索,但不区分大小写
- -user : 按所有者搜索
- -nouser : 查找没有所以者的文件
- -mtime : 按时间查找(查找在某时间范围内被修改内容的文件)后面跟时间(天为单位)
- -atime : 按时间查找(查找在某时间范围内被访问的文件)后面跟时间(天为单位)
- -ctime : 按时间查找(查找在某时间范围内被改变属性的文件)后面跟时间(天为单位)
- -size : 按照文件大小搜索,后面跟文件大小,必须给出单位,比如k、M,G
- -inum : 按照文件节点查找,后面跟着节点
注意:
- find命令是遍历搜索,会把搜索范围内的文件遍历搜索一遍,尽量要指定范围,不要全局遍历,否则很耗费资源
- find命令是全文搜索,可以使用通配符来进行匹配搜索
- * : 匹配任意内容
- [] : 匹配任意中括号内的一个字符
- ? : 匹配任意一个字符
-name
[root@localhost ~]# ls
anaconda-ks.cfg folder_a www.bat www.bbcc www.txt
[root@localhost ~]# find /root -name www.txt
/root/www.txt
[root@localhost ~]# find /root -name www.*
find: 路径必须在表达式之前: www.bbcc
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@localhost ~]# find /root -name "www.*"
/root/www.txt
/root/www.bbcc
/root/www.bat
使用通配符的时候,注意要用双引号括起来,否则会出现上述提示的错误。
-iname
[root@localhost ~]# ls
anaconda-ks.cfg folder_a www.txt WwW.txt WWW.txt
[root@localhost ~]# find /root -iname www.txt
/root/www.txt
/root/WWW.txt
/root/WwW.txt
-user
[root@localhost ~]# ll
总用量 4
-rw-------. 1 root root 2752 Nov 10 02:51 anaconda-ks.cfg
drwxr-xr-x. 2 root root 20 Dec 6 00:20 folder_a
-rw-r--r--. 1 root root 0 Dec 6 00:11 www.txt
-rw-r--r--. 1 root root 0 Dec 6 14:54 WwW.txt
-rw-r--r--. 1 root root 0 Dec 6 14:53 WWW.txt
[root@localhost ~]# find /root -user root
/root
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.config
/root/.config/abrt
/root/.bash_history
/root/folder_a
/root/folder_a/bbc.txt
/root/www.txt
/root/WWW.txt
/root/WwW.txt
[root@localhost ~]#
-nouser
[root@localhost ~]# find /root -nouser
[root@localhost ~]# find /sys -nouser
[root@localhost ~]#
一般都是有所有者,所以搜索结果为空
-mtime、-atime、-ctime
[root@localhost ~]# find /root -mtime -10
/root
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/folder_a
/root/folder_a/bbc.txt
/root/www.txt
/root/WWW.txt
/root/WwW.txt
[root@localhost ~]# find /root -mtime +10
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache
/root/.config
/root/.config/abrt
/root/.bash_history
这里:
- -10:表示在十天之内修改的
- 10:表示过去的第十天修改的
- +10:表示十天之前修改的
-size
[root@localhost ~]# ls
anaconda-ks.cfg folder_a www.txt WwW.txt WWW.txt
[root@localhost ~]# find /root -size -5k
/root
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.config
/root/.config/abrt
/root/.bash_history
/root/folder_a
/root/folder_a/bbc.txt
/root/WWW.txt
/root/WwW.txt
/root/www.txt
符合条件的隐藏文件都被搜索出来。
还可以搜索一个范围:
[root@localhost ~]# find /root -size +2k -a -size -5k
/root
/root/anaconda-ks.cfg
-a表示and,上面的命令表示在 2k 到 5k 之间的文件都搜索出来,与
-o表示or,或
此外还可以用:
-exec [命令] {} ;
来对搜索出来的结果进行命令操作:
[root@localhost ~]# find /etc -size +20k -a -size -50k -exec ls -lh {} \;
-rw-r--r--. 1 root root 26K Jan 15 2015 /etc/sysconfig/network-scripts/network-functions-ipv6
-rw-r--r--. 1 root root 24K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/apache.pp
-rw-r--r--. 1 root root 28K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/init.pp
-rw-r--r--. 1 root root 33K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/staff.pp
-rw-r--r--. 1 root root 44K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/sysadm.pp
-rw-r--r--. 1 root root 29K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/unprivuser.pp
-rw-r--r--. 1 root root 26K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/virt.pp
-rw-r--r--. 1 root root 21K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/xguest.pp
-rw-r--r--. 1 root root 28K Mar 5 2015 /etc/selinux/targeted/modules/active/modules/xserver.pp
-rw-r--r--. 1 root root 25K Jun 9 2014 /etc/dnsmasq.conf
-rw-r--r--. 1 root root 49K Mar 5 2015 /etc/brltty/fr-abrege.ctb
-rw-r--r--. 1 root root 37K Mar 5 2015 /etc/brltty/de-kurzschrift.ctb
-rw-r--r--. 1 root root 21K Mar 5 2015 /etc/brltty/en-nabcc.ttb
-rw-r--r--. 1 root root 39K Mar 5 2015 /etc/brltty/en-us-g2.ctb
-rw-r--r--. 1 root root 22K Mar 5 2015 /etc/brltty.conf
-rw-r--r--. 1 root root 21K Jun 9 2014 /etc/postfix/access
-rw-r--r--. 1 root root 22K Jun 9 2014 /etc/postfix/header_checks
-rw-r--r--. 1 root root 27K Jun 9 2014 /etc/postfix/main.cf
[root@localhost ~]#
-inum
[root@localhost ~]# ls
anaconda-ks.cfg folder_a www.txt WwW.txt WWW.txt
[root@localhost ~]# ls -i
71259104 anaconda-ks.cfg 4164211 folder_a 71274406 www.txt 71274405 WwW.txt 71274385 WWW.txt
[root@localhost ~]# find -inum 71274406
./www.txt
[root@localhost ~]#
在文件中搜索字符串的命令:grep
grep [选项] 字符串 文件名
在文件中搜索符合条件的字符串
选项:
- -v : 反选,排除制定的字符串
- -i : 忽略大小写
[root@localhost ~]# vi hello.txt
Hello World!
I love you!
~
-- INSERT --
[root@localhost ~]# grep World hello.txt
Hello World!
[root@localhost ~]# grep love hello.txt
I love you!
Email:hlwyfeng(Geek)gmail.com 请将(Geek)换成@
出处:博客园 Tab Weng的博客:http://www.cnblogs.com/hlwyfeng
声明:本文采用知识共享署名-非商业性使用-禁止演绎 3.0 未本地化版本许可协议,允许重新传播和转载分享,但必须在正文显著位置注明署名及原文来源。


浙公网安备 33010602011771号