iis服务器助手广告广告
返回顶部
首页 > 资讯 > 操作系统 >Linux 中find命令exec参数的作用是什么
  • 802
分享到

Linux 中find命令exec参数的作用是什么

2023-06-13 02:06:48 802人浏览 泡泡鱼
摘要

本篇文章为大家展示了linux 中find命令exec参数的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。  exec解释:  -exec 参数后面跟的是command命令,它的终止是以;

本篇文章为大家展示了linux 中find命令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 127 10-28 16:51 。/log2014.log

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-2.log

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-3.log

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test4/log3-1.log

  -rw-r--r-- 1 root root 33 10-28 16:54 。/log2013.log

  -rw-r--r-- 1 root root 302108 11-03 06:19 。/log2012.log

  -rw-r--r-- 1 root root 25 10-28 17:02 。/log.log

  -rw-r--r-- 1 root root 37 10-28 17:07 。/log.txt

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-2.log

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-3.log

  -rw-r--r-- 1 root root 0 10-28 14:47 。/test3/log3-1.log

  [root@localhost test]#

  说明:

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

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

  命令:

  find 。 -type f -mtime +14 -exec rm {} \;

  输出: 

  代码如下:

  [root@localhost test]# ll

  总计 328

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  -rw-r--r-- 1 root root 33 10-28 16:54 log2013.log

  -rw-r--r-- 1 root root 127 10-28 16:51 log2014.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  -rw-r--r-- 1 root root 25 10-28 17:02 log.log

  -rw-r--r-- 1 root root 37 10-28 17:07 log.txt

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 10-28 14:47 test3

  drwxrwxrwx 2 root root 4096 10-28 14:47 test4

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

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 test4

  [root@localhost test]#

  说明:

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

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

  命令:

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

  输出:

  代码如下:

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log -》 log.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 test4

  [root@localhost test]# find 。 -name “*.log” -mtime +5 -ok rm {} \;

  《 rm 。。。 。/log_link.log 》 ? y

  《 rm 。。。 。/log2012.log 》 ? n

  [root@localhost test]# ll

  总计 312

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxrwx 2 root root 4096 11-12 19:32 test3

  drwxrwxrwx 2 root root 4096 11-12 19:32 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用户。

上一页123下一页共3页

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

  命令:

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

  输出:

  代码如下:

  [root@localhost test]# ll

  总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxr-x 2 root root 4096 11-12 22:49 test3

  drwxrwxr-x 2 root root 4096 11-12 19:32 test4

  [root@localhost test]# cd test3/

  [root@localhost test3]# ll

  总计 304

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log

  -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log

  [root@localhost test3]# find 。 -name “*.log” -exec mv {} 。。 \;

  [root@localhost test3]# ll

  总计 0[root@localhost test3]# cd 。。

  [root@localhost test]# ll

  总计 316

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log

  -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxr-x 2 root root 4096 11-12 22:50 test3

  drwxrwxr-x 2 root root 4096 11-12 19:32 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

  总计 316

  -rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

  -rw-r--r-- 1 root root 61 11-12 22:44 log2013.log

  -rw-r--r-- 1 root root 0 11-12 22:25 log2014.log

  drwxr-xr-x 6 root root 4096 10-27 01:58 scf

  drwxrwxr-x 2 root root 4096 11-12 22:50 test3

  drwxrwxr-x 2 root root 4096 11-12 19:32 test4

  [root@localhost test]# find 。 -name “*.log” -exec cp {} test3 \;

  cp: “。/test3/log2014.log” 及 “test3/log2014.log” 为同一文件

  cp: “。/test3/log2013.log” 及 “test3/log2013.log” 为同一文件

  cp: “。/test3/log2012.log” 及 “test3/log2012.log” 为同一文件

  [root@localhost test]# cd test3

  [root@localhost test3]# ll

  总计 304

  -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

  -rw-r--r-- 1 root root 61 11-12 22:54 log2013.log

  -rw-r--r-- 1 root root 0 11-12 22:54 log2014.log

  [root@localhost test3]#

上述内容就是Linux 中find命令exec参数的作用是什么,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网操作系统频道。

--结束END--

本文标题: Linux 中find命令exec参数的作用是什么

本文链接: https://www.lsjlt.com/news/264370.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Linux 中find命令exec参数的作用是什么
    本篇文章为大家展示了Linux 中find命令exec参数的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。  exec解释:  -exec 参数后面跟的是command命令,它的终止是以;...
    99+
    2023-06-13
  • linux中find命令之exec参数的示例分析
    这篇文章将为大家详细讲解有关linux中find命令之exec参数的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已...
    99+
    2023-06-09
  • linux系统中find命令的exec怎么用
    这篇文章主要介绍linux系统中find命令的exec怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时...
    99+
    2023-06-13
  • Linux命令中的find命令是什么
    Linux命令中的find命令是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。find 是 Linux 中强大的搜索命令,不仅可以按照文件名搜索文件,还可以按照权限、大小、...
    99+
    2023-06-28
  • sql exec命令的作用是什么
    SQL EXEC命令用于执行存储过程、函数或动态SQL语句。它可以将参数传递给存储过程或函数,并返回执行结果。通过使用EXEC命令,...
    99+
    2024-04-09
    sql
  • CentOS中find命令的作用是什么
    这期内容当中小编将会给大家带来有关CentOS中find命令的作用是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1.在某目录下查找名为“elm.cc”的文件find /home/lijiajia/ ...
    99+
    2023-06-10
  • linux中find命令有什么用
    这篇文章主要介绍了linux中find命令有什么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下f...
    99+
    2023-06-09
  • Linux中的exec命令怎么用
    这篇文章主要介绍Linux中的exec命令怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!在Linux系统中使用 exec 命令可以并不启动新的 Shell,而是使用执行命令替换当前的 Shell 进程,并且将老...
    99+
    2023-06-28
  • linux中find命令的12个常用参数详解
    这篇文章主要介绍“linux中find命令的12个常用参数详解”,在日常操作中,相信很多人在linux中find命令的12个常用参数详解问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”linux中find命令的...
    99+
    2023-06-13
  • linux中apt命令参数是什么
    linux中apt命令参数是根据运行不同的参数使用不同的功能,例如常用的apt命令参数有:apt-cache search package #搜索包apt-cache show package #获取包的相关信息,如说明、大小、版本等sud...
    99+
    2024-04-02
  • Linux/Unix下find命令的用法是什么
    这期内容当中小编将会给大家带来有关Linux/Unix下find命令的用法是什么,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。find命令的主要功能就是沿着文件层次以此向下遍历,找到匹配条件的文件。主要功...
    99+
    2023-06-28
  • linux的find命令格式及find命令怎么用
    这篇文章主要为大家展示了“linux的find命令格式及find命令怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux的find命令格式及find命令怎么用”这篇文章吧。find命令...
    99+
    2023-06-17
  • linux中find命令的用法
    linux中find命令的用法:在linux中find命令可以用来搜索目录;语法格式为:“find 目录 参数 文件名称”,例如在linux终端中输入“find /usr/tmp -name 'a*'”命令则查找/usr/tmp目录下的所有...
    99+
    2024-04-02
  • ipconfig命令及各参数的作用是什么
    ipconfig命令是Windows操作系统中用于查看和管理网络配置的命令行工具。它的作用是获取和显示当前计算机的网络配置信息,包括...
    99+
    2023-10-08
    ipconfig
  • linux中cat命令的作用是什么
    这篇文章给大家介绍linux中cat命令的作用是什么 ,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。cat:查看文件的内容、连接文件、创建一个或多个文件和重定向输出到终端或文件 用法:cat [选项] [文件] $ c...
    99+
    2023-06-13
  • Linux中awk命令的作用是什么
    Linux中awk命令的作用是什么?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。什么是Linux系统Linux是一种免费使用和自由传播的类UNIX操作系统,是一...
    99+
    2023-06-09
  • Linux中touch命令的作用是什么
    本篇文章为大家展示了Linux中touch命令的作用是什么,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者...
    99+
    2023-06-13
  • Linux系统find命令的使用方法是什么
    这篇文章将为大家详细讲解有关Linux系统find命令的使用方法是什么,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。Linux系统中一切皆为文件,所以要想快速的找到某一个文件可以借助查找文件...
    99+
    2023-06-28
  • linux中chgrp命令的作用是什么
    chgrp命令是用于更改文件或目录的所属组的命令。 使用chgrp命令,可以将文件或目录的所属组更改为指定的组名或组ID。 chgr...
    99+
    2023-10-22
    linux
  • Linux中gunzip命令的作用是什么
    Linux中gunzip命令的作用是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。Linux gunzip 命令我们现在知道压缩文件可以用 gzip -d ...
    99+
    2023-06-16
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作