位置: 编程技术 - 正文

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)

  • 增值税为什么不重复征税
  • 国税和地税的区别
  • 销售佣金 个税
  • 车购税怎么计算
  • 个人社保缴费多少钱一个月
  • 墨盒应该怎么买
  • 增值税发票丢了怎么办?
  • 企业亏损法定盈亏是什么
  • 未分配利润转增股本需要缴纳企业所得税吗
  • 行政事业单位结算起点
  • 城建税免征怎么记账
  • 民办幼儿园需要给老师交五险一金吗
  • 待清算直销银行
  • 利润率的计算举例
  • 股东借款可以转为认缴出资
  • 简述房地产开发企业设立的特殊规定
  • 无票收入后附什么凭证
  • 会计师事务所执业许可和监督管理办法
  • 小规模纳税人按季收入不达30万
  • 递延所得税资产借贷方向
  • 负债清偿损益明细表可以0报么
  • 一次性支付一年租金怎么做账
  • php mysql_real_escape_string函数用法与实例教程
  • 土地出让金土地使用税
  • 哪些人可以享受二级医保
  • cookie验证和token验证的区别
  • thinkphp5数据库操作
  • php添加扩展
  • 天国拯救晕倒的人多久起来
  • 今日除夕吉时
  • vue @hook
  • 来料加工成品如何入库
  • 公司租赁个人车辆账务处理
  • 资产减值损失冲减
  • 帝国cms视频教程
  • dedecms使用教程
  • 开一般户需要开户许可证吗
  • db2数据库安装服务器的环境
  • 出口退税款计入营业外收入
  • 费用没有发票先开什么
  • 社保和公积金个人和单位缴纳比例
  • 运费发票如何做分录
  • 哪些进项发票不能认证
  • 软件续费应记什么科目
  • 会计物料超损费怎么算
  • 微信提现手续费多少?
  • 发工资扣的个人社保计入哪个科目
  • 冲销凭证如何做分录
  • 应付账款可以用现金支付吗
  • 企业被收购会产生哪些税
  • 工程款开票数量和单价要填吗
  • 对公转账需要填写开户行吗
  • 注册表被锁定后的处理方法
  • 一句命令
  • vcspawn.exe
  • crowd1登录界面
  • ubuntu系统查看mac地址命令
  • linux中cat命令的用法含义
  • win8系统找不到wifi
  • xp系统如何共享文件夹怎么弄
  • win8系统特点
  • win7系统点击图标没反应
  • win7系统注册表损坏无法启动怎么修复
  • ghost参数设置
  • 基于专业性的家校双向互动,需要家长的学校教育参与
  • github 最火的项目
  • nodejs webp
  • 新版unity
  • 飞机大战剧情102
  • 安卓手机管家怎么关闭
  • Android4.4 wpa_supplicant深入分析之wpa_supplicant初始化流程
  • python获取xhr
  • python访问oracle
  • 河北电子税务局社保缴费流程
  • 惠州市国家税务局张新华简历
  • 个人所得税预扣预缴办法
  • 江苏联通客服电话人工服务号码
  • 上海买房各种税
  • 烟台税务查询系统网址
  • 刚成立小公司的流程
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设