文件搜索命令: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"

其中,PRUNEFSPRUNENAMESPRUNEPATHS分别列出了三种不同类别的文件,在第一行中的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

通过命令我们得知,lsls --color=auto的别名,这里第一行显示命令的别名,第二行显示文件所在位置。如果文件没有别名,则不显示别名,只显示路径。比如pwd。

PATH环境变量

whereiswhich 命令都是在这个环境变量的路径下寻找的,我们可以想一下,为什么执行 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!

Linux文件搜索命令的更多相关文章

  1. Linux系列教程(六)——Linux文件搜索命令

    前一篇博客我们讲解了Linux链接命令和权限管理命令, 通过 ln -s  链接名 表示创建软链接,不加-s表示创建硬链接:还有三个更改权限的命令,chmod命令可以更改文件或目录权限,chown命令 ...

  2. Linux 文件搜索命令:find、which、whereis 和 locate

    Linux 提供了许多用于文件搜索的命令,这些命令都很强大,但是也有一些不同之处,这里分别介绍一下. 一.find 命令 find 是最常见和最强大的一个文件搜索命令.使用 find 命令可以在指定目 ...

  3. Linux —— 文件搜索命令

    文件搜索命令(配置文件/etc/updatedb.conf) locate 文件名 搜索速度非常快 在后台数据库中按照文件名搜索 updatedb 更新数据库 只可以按照文件名搜索 whereis 命 ...

  4. linux 文件搜索命令find、-name、-iname、通配符*?、-size、-user、-group、-amin、-cmin、-mmin、-a、-o、-exec/-ok、-inum

    尽可能规划好目录结构,少用文件搜索操作,因为它会占用大量的系统资源 find /bin/find find [搜索范围] [匹配条件] find /etc -name initfind /etc -n ...

  5. Linux文件搜索命令find

    命令find可以根据文件的不同属性在指定的范围内搜索文件,例如: 根据文件名进行查找,在目录/etc下搜索文件名为init( -iname 可以实现不区分大小写进行查找)的文件,实现精准查找,只查找文 ...

  6. 【find】linux文件搜索命令find/locate

    参考链接:https://mp.weixin.qq.com/s/y8LeZ0-9D56TWsD-ivPaHQ 一.按文件名称查找 按照文件名称查找是 find 最常见的用法,需要注意的是,搜索的文件名 ...

  7. Linux——文件搜索命令简单笔记

    一: 命令名称:which 命令所在路径:/usr/bin/which 执行权限:所有用户 功能描述:显示系统命令所在目录 范例:$ which ls 还有一个whereeis ls 命令 二: 命令 ...

  8. linux 文件搜索命令locate及updatedb

    windows 搜索工具Everything是根据NTFS日志来搜索的,所以速度特别快 locate 类似于windows的Everything,搜索速度比较快 如果没有locate命令,可安装 yu ...

  9. Linux文件搜索命令locate、which、grep详解

    命令locate详解 命令locate,其基本功能是在文件资料库中可以快速的搜索系统文件,占用系统资源很少,例如:locate my.cnf 还可以使用locate -i [文件名],不区分大小写进行 ...

随机推荐

  1. Spring原理解析-利用反射和注解模拟IoC的自动装配

  2. Let’s Encrypt 配置

    刚配置了下Let's Encrypt,chrome浏览器里有绿条出来,看官网其它平台问题应该也不大.我还没有研究这个工作原理,关键是刚花了几千块给公司买了个收费的证书认证.这里写下配置过程(https ...

  3. 树状sql--采用递归方式获取节点

    创建数据库 create table City(id varchar(3) primary key , pid varchar(3) , name varchar(10)) 插入数据 insert i ...

  4. (转)对比MS Test与NUnit Test框架

    前言: 项目中进行Unit Test时,肯定会用到框架,因为这样能够更快捷.方便的进行测试. .Net环境下的测试框架非常多,在这里只是对MS Test和NUnit Test进行一下比较, 因为这两个 ...

  5. 基于Java Mina框架的部标808服务器设计和开发

    在开发部标GPS平台中,部标808GPS服务器是系统的核心关键,决定了部标平台的稳定性和行那个.Linux服务器是首选,为了跨平台,开发语言选择Java自不待言. 我们为客户开发的部标服务器基于Min ...

  6. Leetcode: Convex Polygon

    Given a list of points that form a polygon when joined sequentially, find if this polygon is convex ...

  7. html5,表单的综合案例

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  8. Node.js中的Session,不要觉得简单哦。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博客地址为http://www.cnblogs.com/jasonnode/ .学习网站上有对应 ...

  9. IOS响应式编程框架ReactiveCocoa(RAC)使用示例

    ReactiveCocoa是响应式编程(FRP)在iOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...

  10. IOS 截取图片 部分 并生成新图片

    /** * 从图片中按指定的位置大小截取图片的一部分 * * @param image UIImage image 原始的图片 * @param rect CGRect rect 要截取的区域 * * ...