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

Linux学习笔记(5)Linux常用命令之文件搜索命令

2015-01-12 17:47 701 查看

(1)find

find命令用于文件搜索,所在路径/bin/find,其语法格式为:

find [搜索范围] [匹配条件]

1)-name:根据文件名搜索,如搜索/etc下的init文件

[root@localhost ~]# find /etc/ -name init

/etc/sysconfig/init

/etc/kdump-adv-conf/kdump_initscripts/init

/etc/init

[root@localhost ~]#


注:-iname是不区分大小写,与Windows不同的是,按照-name搜索到的文件只有与之完全相同的才会被搜索出来。

2)-size:按照文件大小搜索,如搜索根目录下大于100M的文件:

[root@localhost ~]# find / -size +204800

find: “/proc/1628/task/1628/fd/5”: 没有那个文件或目录

find: “/proc/1628/task/1628/fdinfo/5”: 没有那个文件或目录

find: “/proc/1628/fd/5”: 没有那个文件或目录

find: “/proc/1628/fdinfo/5”: 没有那个文件或目录

[root@localhost ~]#


注:num前面的"+"表示大于,"-"表示小于,其次文件的大小需要进行换算,Linux在以数据块为单位,1个数据块即为512字节(0.5k)。

3)-user:按所有者进行查找,-group:按所属组进行查找,如在/home目录下查找所有者为username的文件:find /home –user username。

4)-amin:按访问时间进行搜索(access),-cmin:按文件属性修改的时间进行搜索(change),-mmin:按文件内容修改的时间进行搜索(modify)。如在/etc下查找5分钟内被修改过属性的文件:

find /etc –cmin -5

注:+5表示超过5分钟,-5表示在5分钟内。

5)连接选项:-a和-o

-a表示与(and)的关系,-o表示或(or)的关系,如在/etc下查找大于80M,小于100M的文件:find /etc –size +163840 –a –size -204800。

6)连接选项:-exec和-ok

格式为:-exec/-ok [命令] {} \;

其中"{}"包含搜索的结果,"\"表示转移字符,分号";"表示命令结束。-exec和-ok用于搜索后文件后执行特定命令。二者的区别是前者直接执行,后者需询问确认。如在/etc下查找inittab文件并显示其详细信息:

[root@localhost ~]# find /etc/ -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 884 1月   7 22:27 /etc/inittab
[root@localhost ~]#


7)-type和-inum

-type选项是根据文件类型查找,其中"f"表示文件,"d"表示目录,"l"表示软链接文件。如查找/etc下以init开头的文件:

[root@localhost ~]# find /etc/ -name init* -a -type f

/etc/sysconfig/init

/etc/sysconfig/network-scripts/init.ipv6-global

/etc/kdump-adv-conf/kdump_initscripts/init

/etc/init/init-system-dbus.conf

/etc/selinux/targeted/contexts/initrc_context

/etc/inittab

[root@localhost ~]#


-inum选项是根据i节点查找,Linux中每个文件都有i节点号,通过ls –l命令可以显示,i节点号的作用是:当对一个文件名很复杂的文件进行操作时,可以通过i节点号进行操作,同时,i节点号可以查找硬链接信息。

(2)locate

locate命令用于在文件资料库中查找文件,是一种快速查找工具,其语法格式为:

locate [文件名称]

加入-i选项表示不区分大小写进行查找。

注:新创建的文件无法使用locate进行查找,因为该文件尚未更新至文件资料库中,若要成功查找,则需使用updatedb命令进行更新。另外,创建在/tmp下的文件并不在文件资料库的收录范围之中,因此也无法查找该文件夹下的文件。

查找inittab文件

[root@localhost ~]# locate inittab

/etc/inittab

/usr/share/man/man5/inittab.5.gz

/usr/share/vim/vim72/syntax/inittab.vim

[root@localhost ~]#


在/root目录下创建一个新文件,使用updatedb进行更新,然后进行查找:

[root@localhost ~]# touch text.txt

[root@localhost ~]# locate text.txt

[root@localhost ~]# updatedb

[root@localhost ~]# locate text.txt

/root/text.txt


(3)which

which命令用于搜索命令所在目录及别名信息,所在路径为/usr/bin/which,语法格式为:

which [命令名称]

如查找rm命令的信息:

[root@localhost ~]# which rm

alias rm='rm -i'

/bin/rm


其中alias是定义的别名。

(4)whereis

whereis命令用于搜索命令所在目录及帮助文档路径,所在路径为/usr/bin/whereis,其语法格式为:

whereis [命令名称]

如查找ls的命令信息:

[root@localhost ~]# whereis ls

ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz


(5)grep

grep命令用于在文件中搜索字串匹配的行并输出,所在路径为/bin/grep,其语法格式为:

grep –iv [指定字串] [文件]

其中-i选项表示不区分大小写,-v选项表示排除指定字串进行显示。

例:搜索/root下install.log中的mysql所在行:

[root@localhost ~]# grep mysql install.log

安装 mysql-libs-5.1.71-1.el6.i686


例:查看/etc下inittab文件除#开头的行

[root@localhost ~]# grep -v ^# /etc/inittab

id:3:initdefault:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: