Linux常用命令之文件搜索命令详解(find、locate、which、whereis、grep、wc)

Linux常用命令——文件搜索命令

一、find命令:在目录中查找指定文件

find命令是直接在硬盘中进行搜索的,如果指定的搜索范围过大,find命令就会消耗较大的系统资源,导致服务压力过大。所以,在使用find命令搜索时,尽量更精准的查找,这样消耗系统资源少,查找的速度快。

find 命令的基本信息如下:

  • 命令名称:find
  • 所在路径:/bin/find
  • 格式:find 搜索路径 搜索内容
  • 执行权限:所有用户
  • 功能描述:在目录中查找文件
(1)按照文件名搜索

-name:按照文件名搜索;在Windows搜索中,只要文件名包含init就会被搜索出来,但在Linux中,find命令是完全匹配的,必须和搜索关键字一模一样才会列出,是一种精确搜索;

[root@root 桌面]# find /etc -name init
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/init
/etc/sysconfig/init

通配符的使用:* 表示匹配所有,?表示匹配单个字符;

[root@root 桌面]# find /etc -name *init*
/etc/festival/siteinit.scm
/etc/X11/xinit
/etc/X11/xinit/xinitrc-common
/etc/X11/xinit/xinitrc.d
/etc/X11/xinit/xinitrc
/etc/pam.d/run_init
/etc/kdump-adv-conf/kdump_initscripts
......
[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# find /etc -name init??
/etc/init.d
/etc/rc.d/init.d

在Linux中,严格区分大小写,如果想要不区分大小写,就可以使用 -iname:按照文件名搜索,但不区分文件名;

[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# touch /etc/INITTAB
[root@root 桌面]# find /etc -name init???
/etc/inittab
[root@root 桌面]# find /etc -iname init???
/etc/INITTAB
/etc/inittab
(2)按照文件大小搜索:-size 大小:+n大于 -n小于 n等于

1块=512k ,2048块=10M;

[root@root 桌面]# find /etc -size +2048
/etc/selinux/targeted/policy/policy.24
/etc/selinux/targeted/modules/active/policy.kern
/etc/gconf/gconf.xml.defaults/%gconf-tree.xml
(3)根据所有者进行查询:-user 所有者

注:所有者,指创建者;所属者,指创建者所在的组。

[root@redhat 桌面]# find /home -user root
/home
/home/zhangsan/zhang
/home/vftpusers/guest
(4)根据所属者进行查询
[root@redhat 桌面]# find /home -user root
/home
/home/zhangsan/zhang
/home/vftpusers/guest
(5)根据时间属性来查询:
  • -amin:访问时间
  • -cmin:改变文件属性(使用ls -l 看到的信息)
  • -mmin:改变文件内容
[root@root 桌面]# find /etc -mmin -30
/etc
/etc/vmware-tools
/etc/gconf/gconf.xml.defaults
/etc/gconf/gconf.xml.mandatory
/etc/gconf/gconf.xml.system
/etc/resolv.conf
/etc/mtab
/etc/tpvmlp.conf
(6)-a:两个条件同时满足;-o:两个条件满足任意一个即可;-type:文件类型。f——>文件,d——>目录,l——>软连接文件
[root@root 桌面]# find /etc -name init* -a -type f
/etc/kdump-adv-conf/kdump_initscripts/init
/etc/inittab
/etc/selinux/targeted/contexts/initrc_context
/etc/init/init-system-dbus.conf
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/sysconfig/init

在这里插入图片描述

(7)-exec/-ok:把find 命令的结果交给由 -exec/-ok调用的命令2来处理。“{}”就代表 find 命令的查询结果。
[root@root 桌面]# find /etc -name inittab
/etc/inittab
[root@root 桌面]# find /etc -name inittab -exec ls -l {} \;
-rw-r--r--. 1 root root 884 6月  22 19:26 /etc/inittab
[root@root 桌面]# find /etc -name inittab -ok ls -l {} \;
< ls ... /etc/inittab > ? y
-rw-r--r--. 1 root root 884 6月  22 19:26 /etc/inittab

二、locate命令:在文件资料库查找文件

locate 命令相比find命令而言,那就是搜索速度非常快,而且耗费系统资源非常小。这是因为 locate命令不会直接搜索硬盘空间,而会先建立 locate 数据库,然后在数据库中按照文件名进行搜索,是快速查找的搜索命令。

locate 命令的基本信息如下:

  • 命令名称:locate
  • 所在路径:/usr/bin/locate
  • 执行权限:所有用户
  • 功能描述:按照文件名搜索文件

1、locate 文件名

注:该实验没有成功!

[root@redhat 桌面]# locate inittab
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/vim/vim72/syntax/inittab.vim

2、locate 查找有一个缺点,就是如果我们新建立一个文件,那么locate命令找不到这个文件;因为locate不能实时更新资料库;但我们可以 updatedb 进行手动查询。

[root@redhat 桌面]# locate inittab
/etc/inittab
/usr/share/man/man5/inittab.5.gz
/usr/share/vim/vim72/syntax/inittab.vim

3、locate命令的文件资料库不收录/tmp这个临时文件的内容;

[root@root ~]# touch /tmp/zhaoliying
[root@root ~]# updatedb
[root@root ~]# locate zhaoliying
[root@root ~]# 

4、使用 locate 命令查找文件时,如果想要不区分大小写,就可以加 -i 选项;

[root@root ~]# touch TANGYAN
[root@root ~]# updatedb
[root@root ~]# locate tangyan
[root@root ~]# locate -i tangyan
/root/TANGYAN
[root@root ~]# 

三、which命令:列出命令的所在路径

which 命令的基本信息如下:

  • 命令名称:which
  • 所在路径:/usr/bin/which
  • 执行权限:所有用户
  • 功能描述:列出命令的所有路径

which 命令在查询到二进制的同时,如果这个命令有别名,则还可以找到命令的别名;

[root@root ~]# which cp
alias cp='cp -i'
	/bin/cp
[root@root ~]# which useradd
/usr/sbin/useradd
[root@root ~]# which rm
alias rm='rm -i'
	/bin/rm
[root@root ~]# which ifconfig
/sbin/ifconfig
[root@root ~]# 

alias 字段后面就是命令的别名,例如:我们在执行 rm 命令时,本身是没有提示信息的,但是Linux系统中,rm 是rm -i的别名,所以才会有提示信息,是否需要删除文件?

注意:定义别名

格式:alias 别名=‘命令所在路径’
例如:
alias dir=’/usr/bin/ls’
alias rm=’/usr/bin/rm -i’

[root@root ~]# cd /tmp
[root@root tmp]# touch tangyan
[root@root tmp]# rm tangyan
rm:是否删除普通空文件 "tangyan"?

四、whereis命令:搜索命令所在的目录及帮助文档路径

whereis 是搜索系统命令的命令,也就是说,whereis命令不能搜索普通文件,而只能搜索系统命令。whereis命令的基本信息如下:

  • 命令名称:whereis
  • 所在路径:/usr/bin/whereis
  • 执行权限:所有用户
  • 功能描述:查找二进制命令、源文件和帮助文档的命令

whereis命令不仅搜索命令所在的目录,还可以搜索帮助文档的路径,针对搜索命令的路径而言,whereis命令和which命令没有本质区别;

[root@root tmp]# whereis useradd
useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz
[root@root tmp]# whereis rm
rm: /bin/rm /usr/share/man/man1p/rm.1p.gz /usr/share/man/man1/rm.1.gz

五、grep命令:在文件中搜索匹配的行输出

grep命令的基本信息如下:

  • 命令名称:grep
  • 所在路径:/bin/grep
  • 格式:grep 搜索内容 搜索路径
  • 执行权限:所有用户
  • 功能描述:在文档中搜寻匹配的行并输出

1、选项:-i(忽略大、小写) -v(反转我查找)

[root@root tmp]# grep multiuser /etc/inittab
#   3 - Full multiuser mode
[root@root tmp]# grep -i multiuser /etc/inittab
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
[root@root tmp]# grep -v "#"  /etc/inittab
id:5:initdefault:

2、查找条件:

查找以某字符串为开头:“^某字符串” 例如:grep “^#” /etc/inittab

[root@root tmp]# grep "^#" /etc/inittab
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#.......

查找以某字符串为结尾:“ 某字符串$ ” 例如:grep “ )$ ” /etc/inittab

[root@root tmp]# grep ")$" /etc/inittab
#   0 - halt (Do NOT set initdefault to this)
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   6 - reboot (Do NOT set initdefault to this)

查找空行:“ ^$ ” 例如:grep “ )$ ” /etc/inittab

[root@root tmp]# grep "^$" /etc/yum.conf


[root@root tmp]# 

六、wc 命令:统计命令

wc 命令的基本信息如下:

  • 命令名称:wc
  • 所在路径:/usr/bin/bin
  • 执行权限:所有用户
  • 功能描述:统计文件中的信息

wc命令的参数如下:

  • -l:统计行数 例如:wc -l 需要统计的文件
  • -w:统计单词数量 例如:wc -w 需要统计的文件
  • -c:统计字节数 例如:wc -c 需要统计的文件
[root@root tmp]# wc -l /etc/services
10774 /etc/services
[root@root tmp]# wc -w /etc/services
58108 /etc/services
[root@root tmp]# wc -c /etc/services
641020 /etc/services
[root@root tmp]# 

猜你喜欢

转载自blog.csdn.net/weixin_45116657/article/details/94434716