位置: 编程技术 - 正文

Linux find命令中-exec参数的作用介绍(linux命令find用法)

编辑:rootadmin

推荐整理分享Linux find命令中-exec参数的作用介绍(linux命令find用法),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:在linux中find命令,在linux中find命令,linux find -cmin,linux find -o,linux find -o,linux find -cmin,linux find -cmin,linux find命令详解xargs,内容如对您有帮助,希望把文章链接给更多的朋友!

  我们都知道,Linux命令加上不同的参数其效果也不同,下面小编将针对Linux fing命令中的-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.log

  lrwxrwxrwx 1 root root 7 - : log_link.log -》 log.log

  -rw-r--r-- 1 root root - : log.log

  -rw-r--r-- 1 root root - : log.txt

  drwxr-xr-x 6 root root - : scf

  drwxrwxrwx 2 root root - : test3

  drwxrwxrwx 2 root root - : test4

  [root@localhost test]# find 。 -type f -mtime + -exec rm {} ;

  [root@localhost test]# ll

  总计

  -rw-r--r-- 1 root root - : log.log

  lrwxrwxrwx 1 root root 7 - : log_link.log -》 log.log

  drwxr-xr-x 6 root root - : scf

  drwxrwxrwx 2 root root - : test3

  drwxrwxrwx 2 root root - : test4

  [root@localhost test]#

  说明:

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

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

  命令:

  find 。 -name “*.log” -mtime +5 -ok rm {} ;

  输出:

  代码如下:

  [root@localhost test]# ll

  总计

  -rw-r--r-- 1 root root - : log.log

  lrwxrwxrwx 1 root root 7 - : log_link.log -》 log.log

  drwxr-xr-x 6 root root - : scf

  drwxrwxrwx 2 root root - : test3

  drwxrwxrwx 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.log

Linux find命令中-exec参数的作用介绍(linux命令find用法)

  drwxr-xr-x 6 root root - : scf

  drwxrwxrwx 2 root root - : test3

  drwxrwxrwx 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/bash

  root:x:0:0:root:/root:/bin/bash

  [root@localhost test]#

  说明:

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

上一页下一页共3页

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

  命令:

  find 。 -name “*.log” -exec mv {} 。。 ;

  输出:

  代码如下:

  [root@localhost test]# ll

  总计 drwxr-xr-x 6 root root - : scf

  drwxrwxr-x 2 root root - : test3

  drwxrwxr-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.log

  drwxr-xr-x 6 root root - : scf

  drwxrwxr-x 2 root root - : test3

  drwxrwxr-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.log

  drwxr-xr-x 6 root root - : scf

  drwxrwxr-x 2 root root - : test3

  drwxrwxr-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命令中-exec参数的用法介绍了,find命令的参数还有很多,如果你还想了解其他参数的使用,详见Linux find命令中-path -prune参数的作用介绍。

Linux find命令中-path -prune参数作用详细介绍 Linux下find命令拥有多种查找方式,那么find命令中加上-path-prune参数会怎么样呢?下面小编就给大家介绍下find命令中-path-prune的用法。假如在当前目录下

在Linux下如何安装配置fcitx输入法 Linux输入法的切换和Windows一样,也可使用快捷切换,fcitx输入法是众多输入法中的一种,有些朋友习惯使用该输入法,下面小编就给大家介绍下Linux如何

Linux如何使用locate命令查找数据查找指定文件 locate命令是Linux查找命令中的一种,可用于查找数据,与find命令有些相似,但find命令更加消耗资源,下面小编就给大家详细介绍下locate命令的用法。loca

标签: linux命令find用法

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

上一篇:Linux学习笔记(三):磁盘和文件系统管理(linux learn)

下一篇:Linux find命令中-path -prune参数作用详细介绍(linux中find命令用法)

  • 在报税显示缺少发票信息
  • 企业对公账户钱怎么取出来
  • 普通发票被作废的话财务会发现吗
  • 建筑行业增值税税负率计算公式
  • 企业城建税教育费附加和地教费的税率是多少
  • 可供出售金融资产是指什么
  • 负债类科目有借方余额吗
  • 土地转让缴纳增值税政策
  • 商品损失 进项税 确定
  • 税收返还要交税吗
  • 写字楼出租流程
  • 收到子公司非货币资产分配账务处理
  • 事业单位对外投资涉及的主要科目有
  • 以公司名义买货车可以抵税吗
  • 离职补偿金个税计算器2022
  • 外经证预缴税填不含税金额吗
  • 发票涉税风险有哪些
  • 防伪税控减免税款的会计分录
  • 设备器具一次性扣除会计分录
  • 进项税和销项税怎么抵扣
  • 政策性搬迁的会计处理
  • 销售商品售后回购
  • php数组函数题目
  • 借条丢了怎么办打电话给家长
  • 穹顶高度
  • php判断字符串是否存在
  • php获取北京时间
  • 行政单位捐赠收入属于什么收入
  • js遇到的问题
  • 固定资产被替换的账面价值怎么算
  • 如何修改php网页内容
  • 织梦系统网站搭建教程
  • chrome网上应用商店在哪
  • 购买税控设备的分录怎么做
  • 股权转让个人转个人要交什么税
  • MySQL创建数据库语句
  • 以前年度损益调整会计分录
  • 企业所得税的税收筹划
  • 电梯折旧费计算方法
  • 房租费待摊分录怎么做
  • 建筑企业需要缴纳的税种
  • 个人开发者要缴税吗
  • 收取会费
  • 国有独资企业董事会成员组成规定
  • 加油的增值税发票几个点
  • 资本增值率计算公式是什么?
  • 固定资产如何管理
  • 什么是增资扩股协议
  • 记账凭证的审核与编制可以是一个人吗
  • mysql常见优化手段
  • mysql 判断
  • mysql中删除表中的数据
  • 排序mysql
  • mysql主从复制原理以及架构
  • redhat常用命令总结
  • 大白菜u盘启动按f几
  • w7系统删除所有东西
  • centos7权限
  • redhat配置
  • xp系统桌面图标不见了,点什么都没用?
  • linux,Centos5,Ubuntu关掉ipv6的方法
  • winio在win7下使用
  • centos7安装软件包命令
  • 日历显示不全
  • OpenGL Tips: 在Visual C++中调用FreeGLUT
  • 批处理替换文件中的某个内容
  • Unity3D游戏开发标准教程
  • 右键隐藏在哪
  • bootstrap要学吗
  • powershell实战指南
  • javascript中math.ceil
  • python3.9爬取网页教程
  • 欢迎使用来电提醒业务是什么意思
  • 前方高能(莞尔wr)晋江
  • javascript巧用eval函数组装表单输入项为json对象的方法
  • python 汉诺塔
  • jquery给按钮添加事件
  • 建筑企业个人所得税管理办法
  • 贵州泉源会计服务有限公司花溪店位置
  • 先进单位表彰发言稿
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设