位置: 编程技术 - 正文

linux系统中find命令之exec使用介绍(linux里find命令)

编辑:rootadmin

推荐整理分享linux系统中find命令之exec使用介绍(linux里find命令),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:linux系统find命令用法,linux find -name命令详解,linux find -name 命令,linux find -mmin,linux里find命令,linux系统find命令用法,linux系统中find命令,linux系统find命令用法,内容如对您有帮助,希望把文章链接给更多的朋友!

find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。

exec解释:

-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{} 花括号代表前面find查找出来的文件名。

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

实例1:ls -l命令放在find命令的-exec选项中

命令:

find . -type f -exec ls -l {} ;

输出:

复制代码代码如下:[root@localhost test]# find . -type f -exec ls -l {} ; -rw-r--r-- 1 root root - : ./log.log-rw-r--r-- 1 root root 0 - : ./test4/log3-2.log-rw-r--r-- 1 root root 0 - : ./test4/log3-3.log-rw-r--r-- 1 root root 0 - : ./test4/log3-1.log-rw-r--r-- 1 root root - : ./log.log-rw-r--r-- 1 root root - : ./log.log-rw-r--r-- 1 root root - : ./log.log-rw-r--r-- 1 root root - : ./log.txt-rw-r--r-- 1 root root 0 - : ./test3/log3-2.log-rw-r--r-- 1 root root 0 - : ./test3/log3-3.log-rw-r--r-- 1 root root 0 - : ./test3/log3-1.log[root@localhost test]#

说明:

上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。

实例2:在目录中查找更改时间在n日以前的文件并删除它们

命令:

find . -type f -mtime + -exec rm {} ;

输出:

复制代码代码如下:[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.loglrwxrwxrwx 1 root root 7 - : log_link.log -> log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.txtdrwxr-xr-x 6 root root - : scfdrwxrwxrwx 2 root root - : test3drwxrwxrwx 2 root root - : test4[root@localhost test]# find . -type f -mtime + -exec rm {} ;[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.loglrwxrwxrwx 1 root root 7 - : log_link.log -> log.logdrwxr-xr-x 6 root root - : scfdrwxrwxrwx 2 root root - : test3drwxrwxrwx 2 root root - : test4[root@localhost test]#

说明:

在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。

实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示

命令:

linux系统中find命令之exec使用介绍(linux里find命令)

find . -name "*.log" -mtime +5 -ok rm {} ;

输出:

复制代码代码如下:[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.loglrwxrwxrwx 1 root root 7 - : log_link.log -> log.logdrwxr-xr-x 6 root root - : scfdrwxrwxrwx 2 root root - : test3drwxrwxrwx 2 root root - : test4[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} ;< rm ... ./log_link.log > ? y< rm ... ./log.log > ? n[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.logdrwxr-xr-x 6 root root - : scfdrwxrwxrwx 2 root root - : test3drwxrwxrwx 2 root root - : test4[root@localhost test]#

说明:

在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。 按y键删除文件,按n键不删除。

实例4:-exec中使用grep命令

命令:

find /etc -name "passwd*" -exec grep "root" {} ;

输出:

复制代码代码如下:[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} ;root:x:0:0:root:/root:/bin/bashroot:x:0:0:root:/root:/bin/bash[root@localhost test]#

说明:

任何形式的命令都可以在-exec选项中使用。 在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。

实例5:查找文件移动到指定目录

命令:

find . -name "*.log" -exec mv {} .. ;

输出:

复制代码代码如下:[root@localhost test]# ll总计 drwxr-xr-x 6 root root - : scfdrwxrwxr-x 2 root root - : test3drwxrwxr-x 2 root root - : test4[root@localhost test]# cd test3/[root@localhost test3]# ll总计 -rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root 0 - : log.log[root@localhost test3]# find . -name "*.log" -exec mv {} .. ;[root@localhost test3]# ll总计 0[root@localhost test3]# cd ..[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root 0 - : log.logdrwxr-xr-x 6 root root - : scfdrwxrwxr-x 2 root root - : test3drwxrwxr-x 2 root root - : test4[root@localhost test]#

实例6:用exec选项执行cp命令

命令:

find . -name "*.log" -exec cp {} test3 ;

输出:

复制代码代码如下:[root@localhost test3]# ll总计 0[root@localhost test3]# cd ..[root@localhost test]# ll总计 -rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root 0 - : log.logdrwxr-xr-x 6 root root - : scfdrwxrwxr-x 2 root root - : test3drwxrwxr-x 2 root root - : test4[root@localhost test]# find . -name "*.log" -exec cp {} test3 ;cp: “./test3/log.log” 及 “test3/log.log” 为同一文件cp: “./test3/log.log” 及 “test3/log.log” 为同一文件cp: “./test3/log.log” 及 “test3/log.log” 为同一文件[root@localhost test]# cd test3[root@localhost test3]# ll总计 -rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root - : log.log-rw-r--r-- 1 root root 0 - : log.log[root@localhost test3]#

linux系统find命令之xargs使用实例分享 错误信息通常是参数列太长或参数列溢出。这就是xargs命令的用处所在,特别是与find命令一起使用。find命令把匹配到的文件传递给xargs命令,而xargs命令

linux命令之find命令的个常用参数详解(含具体用法和注意事项) 1.使用name选项:文件名选项是find命令最常用的选项,要么单独使用该选项,要么和其他选项一起使用。可以使用某种文件名模式来匹配文件,记住要

linux命令大全之crontab命令使用详解 at命令是针对仅运行一次的任务,循环运行的例行性计划任务,linux系统则是由cron(crond)这个系统服务来控制的。Linux系统上面原本就有非常多的计划性工

标签: linux里find命令

本文链接地址:https://www.jiuchutong.com/biancheng/356932.html 转载请保留说明!

上一篇:linux中locate命令使用介绍(快速搜寻档案)(linux中locate命令的作用)

下一篇:linux系统find命令之xargs使用实例分享(linux 中find)

  • 生育津贴是否需要申报免税所得
  • 增值税发票开票软件金税盘
  • 进项税转出包括什么
  • 税款抵扣会计分录
  • 用友财务软件怎么设置二级科目
  • 计提利息收入分录怎么写
  • 汇算清缴退回的所得税
  • 社保缴费回单怎么查
  • 多余的实收资本可以转到其他应付款吗
  • 业务人员差旅费为什么不计入投资性房地产成本
  • 发票上盖财务章吗
  • 存货盘亏损失可以扣除吗
  • 现金流量表上的期末现金余额等于___
  • 出口不报关账务处理
  • 进项税额转出后续处理
  • 招待费的进项
  • 预收账款转为主营业务收入
  • 购进商品返点的钱用于抵扣货款怎么入账?
  • 什么是法?法的本质特征是什么
  • 一般纳税人申请流程
  • 预收房款如何申报缴纳增值税
  • 个人承包是什么意思
  • 一般纳税人可以开1%的发票吗
  • 银行异地汇款抽奖需要扣缴个人所得税吗?
  • 房地产预收房款怎么开票
  • 小规模纳税人出售使用过的汽车
  • 税务公司属于什么性质
  • 季报现金流量表怎么填
  • 企业所得税季报是填累计数吗
  • 礼品发票怎么入账
  • 结转电费成本会计分录
  • 购货方跨月销项负数发票如何做账?
  • 应收账款零头没有收到如何账务处理
  • 鸿蒙os程序
  • 礼品应该计入会计分录
  • 几种方法解决一个问题的架构图怎么画
  • 交际应酬费可以抵扣吗
  • php://input用法
  • vue登录页面点击登录进入首页
  • PHP:imagepalettecopy()的用法_GD库图像处理函数
  • 保险赔款的会计分录
  • zendframework3
  • php字符串在另一个字符串出现
  • ubuntu20.04.1安装
  • 专票认证期限多长时间
  • dede织梦怎么转成zblog
  • 销售自己使用过的物品
  • 怎么修改申报数据
  • 哪些进项税不允许从销项税额中抵扣
  • 其他应付款科目贷方登记发生的各种应付
  • 银行代发的工资公司要求返还然后发一半
  • 溢价发行可转换债券 利息调整在贷方吗
  • 工程款的税费怎么计算
  • 事业单位的服务期
  • 递延收益属于哪个报表项目
  • 办公室租金发票可以扺增值税吗
  • 如何做好现金流
  • sqlserver数据库事物日志已满
  • sqlserver全文索引
  • win10预览版选哪个
  • ubuntu笔记软件
  • win sth
  • xp系统如何获取ip地址
  • win10系统如何屏蔽弹窗广告
  • linux系统管理命令有哪些
  • window10添加虚拟网卡
  • 批处理命令是什么语言
  • 求婚表白怎么说怎么写
  • html中<
  • nodejs基础知识
  • unity打开c#
  • unity api compatibility level
  • TFC360冯燃:手游市场细分领域新秀崛起
  • 河南地税网上个税怎么交
  • 深圳市税务审批中心电话
  • 江苏省教师增量绩效多少
  • 张劲松身高体重出生年
  • 淘宝店铺过户后身份证是谁的
  • 地税注销需要什么资料
  • 房屋租赁税房东不承担怎么办理
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设