问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

linux系统中find命令之exec使用介绍

发布网友 发布时间:2022-04-21 04:44

我来回答

1个回答

热心网友 时间:2023-10-14 22:37

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 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用户。

实例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使用介绍

在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。 实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示 命令: find . -name "*.log" -mtime +5 -ok rm {} /...

linux exec命令

基本介绍:exec命令用于调用并执行指定的命令。exec命令通常用在shell脚本程序中,调用其他的命令。在终端中使用命令,则当指定的命令执行完毕后会立即退出终端。入门测试:首先使用echo命令将文本“Welcome to use Linux!”进行输出:[root@linux ~]# echo Welcome to use Linux!Welcome to use Linux!再...

linux的find命令详解

find path -option [ -print ] [ -exec | -ok command {} \]path : find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。-print:find命令将匹配的文件输出到标准输出。-exec:find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为command { } ;,注意{ }...

findexec只获取文件名

findexec是一个Unix/Linux命令,用于搜索文件系统中的可执行文件。它以文件名作为参数,并在指定的路径中搜索可执行文件。它首先搜索指定的路径,然后搜索环境变量$PATH中指定的路径,最后搜索当前工作目录。findexec只获取文件名,而不会获取文件的全路径。它只返回文件名,而不是文件的绝对路径。它还可以...

Linux中find常见用法示例

·find path -option [ -print ] [ -exec -ok command ] {} ;find命令的参数;pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。-print: find命令将匹配的文件输出到标准输出。-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } ;...

find / -prem +7000 -exec ls -l { } \ ;这句命令中{ }\的作用什么,是...

在linux下-exec经常和find一起使用,当匹配到一些文件以后,希望对其进行某些操作,这时就可以使用-exec选项,一旦find命令匹配到了相应的文件,就可以用-exec选项中的命令对其进行操作(在有些操作系统中只允许-exec选项执行诸如ls或ls -l这样的命令),exec选项后面跟随着所要执行的命令,然后是一对儿{...

linux find命令中-exex命令语法

-exec command ;Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of `;' is encountered. The string `{}'is replaced by the current file name being processed everywhere i...

linux中find命令的使用方法有哪些

find命令的参数及使用命令:pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。-print: find命令将匹配的文件输出到标准输出。-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。-ok: 和-exec的作用相同,只...

linux文件查找-find命令

Linux文件查找工具find命令,强大的文件定位与操作手段,其核心在于在文件树中定位文件并执行相应的操作。find命令的基本结构是:find pathname -options [-print -exec -ok ...] { },其中pathname是搜索路径,-options则是控制find行为的关键参数。如想查找当前目录或系统根目录,可使用"."和"/"作为...

find命令怎么使用

-exec这个选项参数后面可以跟自定义的SHELL命令,格式如下:例2:把查找到的文件复制到一个指定的目录 [root@xuegod63~]#touch{1,2,3}.back [root@xuegod63mnt]#find/root-name*.txt-execcp{}/opt/;例3:查找多个类型文件 find命令中比较符的使用:-aand并且 -oor或者 +超过 -低于 [root@...

linux系统find命令 linux find exec linuxfind命令详解 linuxfind命令关键字 linux查找find命令 linux查找文件命令find linux exec命令 linux的exec命令 find命令exec
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
惠州市中级人民法院机构设置 小米6x黑屏无法唤醒 PPT无法打开ppt无法打开需要修复 红薯和红糖一起煮好吗 红薯能不能和红糖一起吃吗 红薯与红糖一起煮好吗 盗汗醒后感觉上半身热出汗、嘴干、但腿脚冷怎么办 ...上半身总是出汗,感觉很热是什么原因。 屁股以上一直到头部出汗特别厉害,而两条腿却冷飕飕的,不能招风怎么办... 上半身爱出汗,下半身不爱出汗,冬天手脚冰凉,有些便秘,这属于什么体质... 如何使用find命令查找文件? C语言程序设计问题 find的exec怎么一次执行多个命令 linux的find -exec命令的问题 linux find -exec 后面的{}是干什么用的 如何ping全部ip 如何ping IP地址 以及为什么要ping IP 如何PING出IP地址? 怎么用ping得到IP地址? 怎么用cmd ping自己的IP ping 本机IP地址 怎样用ping命令获得本机的IP地址 怎么ping自己电脑的 ip地址 如何ping ip地址 怎么ping ip地址 如何使用ping命令查询本机ip地址? 怎样Ping一台计算机的IP? 怎么ping自己电脑的ip 如何用ping查看本地IP地址 如何ping网络的ip地址?? 404 Not Found 怎样利用find命令查到50个文件后就返回,而不是一直查找下去 Linux中find与exec配合使用的问题 linux删除命令这样写不对吗,提示我find: 遗漏“-exec”的参数 Linux下,find命令可以常找文件内容吗? linux find命令中-exex命令语法 linux学习日记八 认识与学习bash Linux下Find命令的使用 linux如何删除目录linux如何删除目录 张姓小孩取名大全 姓张的给小孩取什么名字好? 姓张男孩大字辈怎么起名? 苹果11设置siri的自定义回答 “错误”是什么 在现代网页设计中,动效有哪些常见的用法? 错误意思是什么 在现代网页设计中,动效有哪些常见的用法 郑愁予的《错误》全文 郑愁予的《错误》赏析 分享几款UI动效设计工具,看看你知道几个?