搜索 2.0 linux 搜索普通文件 |
您所在的位置:网站首页 › linux查找数据库文件 › 搜索 2.0 linux 搜索普通文件 |
文章目录
搜索普通文件locatefind1. 根据文件名搜索文件2. 按照文件大小搜索3. 按照时间搜索4. 按照权限来搜索5. 按照所属者和所属组来搜索6. 按照文件类型7. 使用逻辑运算符8. 将 find 输出作为另一个命令输入
搜索普通文件
locate
命令名称:locate。英文原意:find files by name。所在路径:/usr/bin/locate。执行权限:所有用户。功能描述:按照文件名搜索文件。
命令格式 locate [选项] 文件名 # 选项:-i: 忽略大小写locate命令的优点也非常明显,那就是搜索速度非常快,而且耗费系统资源非常小。这是因为locate命令不会直接搜索硬盘空间,而会先建立locate数据库,然后在数据库中按照文件名进行搜索,是快速的搜索命令 # 查看 test 目录是空的 [root@VM-0-7-centos test]# ll total 0 # 创建一个文件 [root@VM-0-7-centos test]# touch myfile.txt [root@VM-0-7-centos test]# ll total 0 -rw-r--r-- 1 root root 0 Aug 19 21:16 myfile.txt # locate 并没有找到 [root@VM-0-7-centos test]# locate myfile.txt这是因为locate命令不会直接搜索硬盘空间,而会搜索locate数据库。这样做的好处是耗费系统资源小、搜索速度快;缺点是数据库不是实时更新的,而要等用户退出登录或重启系统时,locate数据库才会更新,所以我们无法查找到新建立的文件。 使用 updatedb 命令 更新 locate 数据库 [root@VM-0-7-centos ~]# locate myfile.txt [root@VM-0-7-centos ~]# updatedb [root@VM-0-7-centos ~]# locate myfile.txt /root/test/myfile.txt findfind 是 linux 中一个强大的搜索命令。 命令名称:find。英文原意:search for files in adirectory hierarchy。所在路径:/bin/find。执行权限:所有用户。功能描述:在目录中搜索文件。和 locate 正好不同,find 是在硬盘中进行搜索的,不要指定太大范围,否则会很消耗系统资源的。 命令格式: find 搜索路径 [选项] 搜索内容 1. 根据文件名搜索文件 [root@localhost ~]# find 搜索路径 [选项] 搜索内容 选项: -name: 按照文件名搜索 -iname: 按照文件名搜索,不区分文件名大小写 -inum: 按照inode号搜索 [root@VM-0-7-centos ~]# find ./ -name myfile.txt ./test/myfile.txt搜索的文件名必须和搜索内容一致才行,如果只包含搜索内容,则不会找到。 [root@VM-0-7-centos ~]# find ./ -name myfile.txt ./test/myfile.txt [root@VM-0-7-centos ~]# find ./ -name myfile [root@VM-0-7-centos ~]# 2. 按照文件大小搜索 [root@localhost ~]# find 搜索路径 [选项] 搜索内容 选项: -size [+-]大小: 按照指定大小搜索文件 [root@localhost ~]# find . -size 25k ./install.log #在当前目录下,查找大小刚好是25KB的文件,可以找到 [root@localhost ~]# find . -size -25k . ./.bashrc ./.viminfo ./.tcshrc ./.pearrc ./anaconda-ks.cfg ./test2 ./.ssh ./.bash_history ./.lesshst ./.bash_profile ./yum.conf.bak ./.bash_logout ./install.log.syslog ./.cshrc ./cangls #搜索小于25KB的文件,可以找到很多文件 [root@localhost ~]# find . -size +25k #而当前目录下没有大于25KB的文件 3. 按照时间搜索 [root@localhost ~]# find 搜索路径 [选项] 搜索内容 选项: -atime [+-]时间: 按照文件访问时间搜索 -mtime [+-]时间: 按照文件数据修改时间搜索 -ctime [+-]时间: 按照文件状态修改时间搜索 -5:代表5天内修改的文件。5:代表前5~6天那一天修改的文件。+5:代表6天前修改的文件。![]() 1)-exec选项 [root@localhost ~]# find 搜索路径 [选项] 搜索内容 -exec 命令2 {} \;首先,这里的“{}”和“; ”是标准格式,只要执行“-exec”选项,这两个符号必须完整输入。 其次,这个选项的作用其实是把 find 命令的结果交给由“-exec”调用的命令2来处理。“{}”就代表find命令的查找结果。 [root@localhost test]# find . -perm 444 -exec ls -l {} \; -r--r--r-- 1 root root 0 6月 17 11:05 ./test2 #使用“-exec”选项,把find命令的结果直接交给“ls -l”命令处理2)-ok选项 “-ok”选项和“-exec”选项的作用基本一致,区别在于:“-exec”的命令2会直接处理,而不询问;“-ok”的命令2在处理前会先询问用户是否这样处理,在得到确认命令后,才会执行。例如: [root@localhost test]# find . -perm 444 -ok rm -rf {} \; ? y |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |