打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
云计算学习路线分享云计算之文件查找

grep: 文件内容过滤

find: 文件查找,针对文件名

一、命令文件
# which ls //从PATH环境变量 (echo $PATH)
# whereis vim

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/htop/bin/:/root/bin

二、任意文件
A. locate (查询的数据库: /var/lib/mlocate/mlocate.db)
计划任务:每天自动更新数据库 /etc/cron.daily/mlocate.cron
手动更新数据库:updatedb
# locate ifcfg-eth0
# locate ifcfg-enp0s25
locate

updatedb后才能查找到 非常麻烦 不建议使用
如果没有 locate 使用YUM直接安装是不行的。 要查一下在哪个包里 yum provides locate -→ mlocate → 直接YUM mlocate即可
B. find 
find [options] [path...] [expression] [action]

===expression=== 熟用*通配符
按文件名:
[root@tianyun ~]# find /etc -name "ifcfg-eth0" name 名字 后面-print 已省略
[root@tianyun ~]# find /etc -iname "ifcfg-eth0" //-i忽略大小写
[root@tianyun ~]# find /etc -iname "ifcfg-eth*"

按文件大小:
[root@tianyun ~]# find /etc -size +5M //大于5M
[root@tianyun ~]# find /etc -size 5M
[root@tianyun ~]# find /etc -size -5M
[root@tianyun ~]# find /etc -size +5M -ls //-ls找到的处理动作 不是平时用的ls
ll - h 查看大小

指定查找的目录深度:
-maxdepth levels
-mindepth levels
[root@tianyun ~]# find / -maxdepth 3 -a -name "ifcfg-eth0" maxdepth 3 最大3层 a要满足2个条件 并且
时间找(atime,mtime,ctime):
[root@tianyun ~]# find /etc -mtime +5 //修改时间超过5天
[root@tianyun ~]# find /etc -mtime 5 //修改时间等于5天
[root@tianyun ~]# find /etc -mtime -5 //修改时间5天以内


按文件类型:
[root@tianyun ~]# find /dev -type f //f普通
[root@tianyun ~]# find /dev -type d //d目录
[root@tianyun ~]# find /dev -type l //l链接
[root@tianyun ~]# find /dev -type b //b块设备
[root@tianyun ~]# find /dev -type c //c字符设备
[root@tianyun ~]# find /dev -type s //s套接字
[root@tianyun ~]# find /dev -type p //p管道文件

按文件权限:
[root@tianyun ~]# find . -perm 644 -ls .是当前目录 精确查找644 *一般都是精确


[root@tianyun ~]# find . -perm -644 -ls -是包含到意思

带不带- 自己对比一下 查看。 带-表示只要6就可以
[root@tianyun ~]# find . -perm -600 -ls
[root@tianyun ~]# find . -perm -222 -ls //全局可写
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -4000 -ls //包含set uid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -2000 -ls //包含set gid
[root@tianyun ~]# find /usr/bin /usr/sbin -perm -1000 -ls //包含sticky



==找到后处理的动作 ACTIONS: (默认动作-print)==
-print
-ls
-delete
-exec 后面跟自定义的shell命令
-ok 后面跟自定义的shell命令
[root@tianyun ~]# find /etc -name "ifcfg*" 后可以加|xargs -h
[root@tianyun ~]# find /etc -name "ifcfg*" -print
[root@tianyun ~]# find /etc -name "ifcfg*" -ls

[root@tianyun ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;
[root@tianyun ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;


exec命令用于调用并执行指令的命令

查找带root带文件 复制到tmp下

find /etc -name “root*” -exec cp -rf {} /tmp \; 复制到当前文件下 /tmp换成.
[root@tianyun ~]# find /etc -name "ifcfg*" -exec rm -rf {} \; exec为执行一条shell命令 {}为前面的东西\; 格式
[root@tianyun ~]# find /etc -name "ifcfg*" -delete

扩展知识:find结合xargs
[root@tianyun ~]# find . -name "yang*.txt" |xargs rm -rf !!!!!!!!!!!!!重点 找到之后删除处理xargs 参数传递处理找出后删除
[root@tianyun ~]# find /etc -name "ifcfg-eth0" |xargs -I {} cp -rf {} /var/tmp


案例1: 分别找出file5 和除了file5的文件
[root@tianyun ~]# mkdir dir1
[root@tianyun ~]# touch dir1/file{1..20}

[root@tianyun ~]# find /root/dir1 -name "file5"
[root@tianyun ~]# find /root/dir1 ! -name "file5" !为取反

[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" 即是file5又是file9
/root/dir1/file5
/root/dir1/file9

扩展 不常用

[root@tianyun ~]# find /root/dir1 -name "file5" -o -name "file9" -ls
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@tianyun ~]# find /root/dir1 -name "file5" -ls -o -name "file9" -ls
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@tianyun ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -ls \为转译 不加会报错
1466499 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file5
1466515 0 -rw-r--r-- 1 root root 0 6月 5 11:15 /root/dir1/file9

[root@localhost ~]# find /root/dir1 \( -name "file5" -o -name "file9" \) -exec rm -rvf {} \;
removed ‘/root/dir1/file5’
removed ‘/root/dir1/file9’

-exec和xargs的区别
 


2010-11-27 星期六 晴朗当你在命令行执行:
$find . -name 'core' -type f -exec rm {} /;
时,find -exec 命令会对每个匹配的文件执行一个单独的rm操作(execute a separate rm for each one), 正如你手动敲入下面命令:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...但是使用这种方式,如果有100个文件匹配了,那么就需要启100个进程,一个进程处理一个rm命令。一般来说,其越多进程,意味着越耗性能。我们可以换个思路,我们将要删除文件当作参数传递给rm不就可以了吗?也就是说:
rm ./bin/core
rm ./source/shopping_cart/core
rm ./backups/core
...改成:
rm ./bin/core ./source/shopping_cart/core ./backups/core但是前提是后面的命令必须支持多参数。相有些命令,比如unzip,就不支持输入多个jar包,所以必须用-exec。
xargs,顾名思义,是对参数进行处理的命令。它的任务就是将输入行转换成下一个命令的参数列表。因此上面的find -exec命令可以改写成:
find . -name 'core' -type f -print | xargs rmWith this approach, xargs bundles together as many filename arguments as possible for submission to each invocation of rm that's needed, in compliance with the OS's maximum allowed size for an argument list. This means xargs is guaranteed not only to handle all the arguments, but also to use the smallest possible number of processes in doing so. For example, if each command can handle 100 arguments, and there are 110 filenames to process, there will be two invocations of the command, respectively handling 100 and 10 arguments.
其中操作系统允许的最大参数长度由如下命令得到:
forrest@ubuntu:~$ getconf ARG_MAX
2097152这意味着xargs保证不会因为参数过多而挂掉。所以目前看来唯一需要保证的就是后面的命令支持多参数。比如前面说过的unzip,就不支持多参数,如果你使用xargs xxx.jar

相比之下,也不难看出各自的缺点
1、exec 每处理一个文件或者目录,它都需要启动一次命令,效率不好; 
2、exec 格式麻烦,必须用 {} 做文件的代位符,必须用 \; 作为命令的结束符,书写不便。
3、xargs 不能操作文件名有空格的文件;

综上,如果要使用的命令支持一次处理多个文件,并且也知道这些文件里没有带空格的文件,
那么使用 xargs比较方便; 否则,就要用 exec了。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
云计算面试常见问题,怎么理解shell?
软件测试:快速学会操作目录和文件的Linux命令(必读一)
Linux find 强大的文件查找工具
命令2
云计算学习路线教程大纲课件:文件链接
[cygwin]cygwin常用命令及find命令说明
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服