位置: 编程技术 - 正文

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)

  • 餐饮个体户如何开票
  • 境外培训费计入关税完税价格
  • 什么叫做供应链公司
  • 红冲以后怎么做账
  • 开票和收到的款金额不一样怎么办?
  • 抵扣进项税怎么做凭证
  • 增值税纳税义务发生时间
  • 合并报表时抵消内部交易包含的未实现损益的影响有哪些
  • 施工单位给你钱敢领吗
  • 汽车公司场地租金怎么算
  • 空调安装费税率是6%
  • 西安职业中等专业学校校长
  • 资本公积转增资本时应注意的问题主要包括哪些?
  • 集团公司内部无偿借贷增值税
  • 公司账上没钱股东又不出资发工资怎么办
  • 公司基本户没开影响报税吗
  • 年平均息税前利润是什么意思
  • 企业代收代支的非税收入需要缴税吗?
  • 标准差超过多少不合理
  • 汽车厂家金融贴息 是贴利息的还是贴车价
  • 财务报表申报错了怎么撤销
  • 计提当月电费账务处理
  • 付款给对方怎么做分录
  • 开具信息咨询费的发票
  • iphone6s掉水里怎么办
  • mac开机声音取消
  • linux sar文件
  • php数组操作
  • 房地产企业哪些成本上升了
  • 微软win11预览版
  • 财政补助收入核算内容
  • 库存现金意思
  • linux zen3
  • 阿里云onedata
  • Madonna della Corona教堂,意大利 (© Volodymyr Kalyniuk/Alamy)
  • php获取年月日
  • 销售赔偿款增值税和所得税
  • php调用微信扫描二维码
  • cmd more命令
  • ps快速选择工具抠图后怎么拉出来
  • 无法支付其他应用怎么办
  • python中strftime用法
  • 本月损益类未结转为零的一级科目6603
  • 长期待摊费用如何做分录
  • sqlserver调度
  • db2入门
  • 短期借款的账务处理例题
  • 企业贴现会计分录
  • 企业注销时实收资本怎么处理
  • 出口退税项目
  • 项目回款是什么意思
  • 确认主营业务收入分录怎么写
  • 退休职工取暖费发放标准2023年
  • 出售固定资产怎样报增值税申报表
  • 银行代扣水费分录
  • 小规模纳税人怎么申报增值税报表
  • 小微企业和小规模纳税人的区别
  • mysql安装详细步骤
  • sqlserver怎么删除字段
  • mysql的密码忘了该怎么办
  • win8链接wi-fi
  • win edge
  • centos永久修改主机名
  • windows xp sp3 vl
  • 怎样下载电子书免费的
  • mom.exe是什么程序
  • win10桌面图标显示错误
  • linux 翻译
  • 清除垃圾的系统
  • sticky memo widget
  • javascript 数字类型
  • apk文件怎么使用
  • js面向对象的三大特性
  • jquery获取outerhtml
  • Python+Pika+RabbitMQ环境部署及实现工作队列的实例教程
  • javascript中如何获取元素?
  • 江苏省地方税务局网上办税
  • 酒精是否缴纳消费税
  • 建筑业按多少税收
  • 重庆两江新区购房政策
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设