云计算学习:文件查找

云计算学习:文件查找

====================================================================================
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的区别

-exec和xargs的区别

2010-11-27 星期六 晴朗当你在命令行执行:
f i n d . − n a m e ′ c o r e ′ − t y p e f − e x e c r m / ; 时 , f i n d − e x e c 命 令 会 对 每 个 匹 配 的 文 件 执 行 一 个 单 独 的 r m 操 作 ( e x e c u t e a s e p a r a t e r m f o r e a c h o n e ) , 正 如 你 手 动 敲 入 下 面 命 令 : r m . / b i n / c o r e r m . / s o u r c e / s h o p p i n g c a r t / c o r e r m . / b a c k u p s / c o r e . . . 但 是 使 用 这 种 方 式 , 如 果 有 100 个 文 件 匹 配 了 , 那 么 就 需 要 启 100 个 进 程 , 一 个 进 程 处 理 一 个 r m 命 令 。 一 般 来 说 , 其 越 多 进 程 , 意 味 着 越 耗 性 能 。 我 们 可 以 换 个 思 路 , 我 们 将 要 删 除 文 件 当 作 参 数 传 递 给 r m 不 就 可 以 了 吗 ? 也 就 是 说 : r m . / b i n / c o r e r m . / s o u r c e / s h o p p i n g c a r t / c o r e r m . / b a c k u p s / c o r e . . . 改 成 : r m . / b i n / c o r e . / s o u r c e / s h o p p i n g c a r t / c o r e . / b a c k u p s / c o r e 但 是 前 提 是 后 面 的 命 令 必 须 支 持 多 参 数 。 相 有 些 命 令 ,

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值