位置: 编程技术 - 正文

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)

  • 消费税和所得税的关系
  • 城建税的征税范围不包括农村对不对
  • 车船使用税2021
  • 如何做好存货管理,从哪方面去做
  • 向境外支付赔偿金需要发票么
  • 外地施工如何开发票
  • 会计核算体系的中心环节是什么
  • 销售费用的进项税额转出会计分录
  • 装修费用如何摊销成本
  • 工程发票是不是都要异地预缴
  • 出售旧机器设备,发生净收益会计分录
  • 车辆进项税怎么算
  • 国税申报填错了怎么办
  • 发票金额与支票数量不符
  • 请问高人们旧房子要装修应怎样装
  • 现金折扣若缴纳增值税,怎么进行会计处理?
  • 其他应收款无法收回的会计分录
  • 审计费的会计科目
  • 公司租赁房屋的物业费怎么入账
  • 取得带息应收票据时,企业应于期末计算票据利息
  • 建材销售公司需要交哪些税
  • 国外发票可以报销吗
  • 企业风险报酬转移怎么理解
  • 华为鸿蒙系统如何关闭hd通话
  • 如何免费获取Win11企业版
  • 土地增值税清算方法与技巧
  • 软件远程调用失败是什么意思
  • 电脑系统权限管理在哪里
  • windows更新暂停怎么恢复
  • win10版本1909和22h2
  • 房地产土地使用税征收标准
  • php curl命令详解
  • 邮政电信卡
  • token过期处理
  • 若依系统怎么样
  • 什么情况下增长率是0
  • 认缴没有实缴怎么做账
  • 各种汇票
  • SpringBoot+Vue实现文件上传下载功能
  • 微信小程序开发者工具
  • mksquashfs命令
  • 深入了解工作优势怎么回答
  • 不动产进项税额抵扣新政策2021
  • 出租车发票可以改时间吗
  • 所得税费用是属于费用类吗
  • 股东每年怎么分红
  • 税款差0.05是怎么处理
  • 股东收到投资收益会计科目
  • mysql密码忘了怎么办?
  • 存货报废进项税转出吗
  • 宾馆购买的床财务怎么做账
  • 成本发票未到怎么入账
  • 建筑业异地预交税款最新规定
  • 流动资产周转率和总资产周转率
  • 股权支付会计处理
  • 增值税附加税有哪些
  • 辅导期一般纳税人标准
  • 给员工租的房子水电费谁出
  • 年底需要结转的会计科目
  • 企业有生产单没发票
  • 建筑企业材料费能否加计扣除
  • 重装win7旗舰版系统教程
  • WinXP创建一键静音快捷键(在喇叭消失的情况下实用)
  • linux安装.gz
  • windows取消隐藏文件夹
  • linux中内存是2GB,虚拟内存应该是多少
  • win7网速很慢
  • win10输入法设置快捷键
  • bootstrap 组件库
  • excel 级联
  • css使用教程
  • Android Studio 1.1.0 配置androidannotations框架
  • unity strangeioc
  • jquery validator
  • [置顶]电影名字《收件人不详》
  • 安卓手机安装推特
  • 山东国地税合并6月挂牌
  • 医保漏缴如何补交
  • 百旺税控盘口令是多少
  • 监察室主任岗位职责
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设