exec用法

 

一个强大的linux命令——find之exec

2017年01月09日 11:23:20

阅读数:7422

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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

说明:
上面的例子中,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]# 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

说明:
在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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

说明:
在上面的例子中, 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]#
  • 1
  • 2
  • 3
  • 4

说明:
任何形式的命令都可以在-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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

实例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]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

文章标签: linux command

个人分类: linux

相关热词: 一个和 一个 一个标题一个框 一个圈里一个c 一个端口一个ip

▼查看关于本篇文章更多信息

上一篇一个功能强大的linux命令——find

下一篇一个强大的linux命令——find之xargs

 

 

 

æ³å¯¹ä½è说ç¹ä»ä¹

 

发表评论

添加代码片

  • HTML/XML
  • objective-c
  • Ruby
  • PHP
  • C
  • C++
  • JavaScript
  • Python
  • Java
  • CSS
  • SQL
  • 其它

还能输入1000个字符

find -exec 与xargs 区别

hittata hittata

09-26 2.1万

find . -name "*.txt" -exec rm {} \; find . -name "*.txt" | xargs rm {}  -exec     1.参数是一个一个传递...

Linux中exec系列函数的应用

tycoon1988 tycoon1988

09-05 1098

这段时间在研究linux中用户登陆和shell执行程序的原理。我们知道,shell命令分为内部命令和外部命令,内部命令有诸如cd,history,exit,echo等,常见的外部命令有ls,ping,...

Linux命令 find和mv的结合使用:查找文件,移动到某个目录 - CSDN博客

6-18

-exec mv --查找完毕后执行移动操作; 3.按照时间移动到指定目录里 # find ....文章标签: linux date file 个人分类: linux 想对作者说点什么? 我来说一句...

按时间查找并移动文件 - CSDN博客

6-6

linux查看目录的四种方法(ls只显示目录 ) 1, ls -d * 2, find . -type ...-exec mv --查找完毕后执行移动操作; 3.按照时间移动到指定目录里 # find ....

 

 

Linux系统exec命令

psvoldemort psvoldemort

01-15 3758

shell的内建命令exec将并不启动新的shell,而是用要被执行命令替换当前的shell进程,并且将老进程的环境清理掉,而且exec命令后的其它命令将不再执行。  因此,如果你在一个shell里面...

Linux下用find查找并复制指定文件到指定目录下 - CSDN博客

6-15

关键字: linux find 将用户目录ffmpeg目录下所有的*.a文件复制到当前目录ffmpeg/lib下, find ~/ffmpeg -name "*.a" -type f -exec cp {} ./ffmpeg/lib ...

find和grep结合更加方便的查找 - CSDN博客

8-1

不足:用ctrl+r搜索历史命令找到grep之后,如果想改变查询词,还需要从末尾移动光标...在使用linux时,经常需要进行文件查找。其中查找的命令主要有find和grep。两个命令...

Linux命令findexec结合使用

imzoer imzoer

03-31 9296

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿 {},一个空格和一个\,最后是一个分号。     cmd {} \; 看例子: zoer@ubuntu:~$ awk 'BEGIN{cou...

linux下exec系列(一)

JoeBlackzqq JoeBlackzqq

11-02 3817

fork()是用于建立进程的手段之一,但是fork()只能建立相同程序的副本。幸运的是,Linux系统还提供了系统调用exec系列。它可用于新程序的运行。 如果exec调用成功,调用进程将被覆盖,然...

linux环境下find的所有用法以及粘滞位的说明 - CSDN博客

6-30

1.Linux中find常见用法示例 find path -option [ -print ] [ -exec -ok command ] {} \; find命令的参数; pathname: find命令所查找的目录路径。例如用....

Linux下用find查找并复制指定文件到指定目录下 - CSDN博客

8-10

linux find查找多个指定目录并复制到指定文件夹 ...shell脚本:cp某个目录下筛选(find)后的某些文件到另...移动类 37篇 金融类 31篇 信息保存 56篇 Web...

linux exec用法总结

p656456564545 p656456564545

04-21 306

分类: LINUX 原文地址:linux exec用法总结 作者:taosk Linux中exec的用法总结 先总结一个表: exec命令 ...

Linux 查找某天的文件并打包 - CSDN博客

8-4

1.方法一 ll -lrt | grep May\ 25 | awk '{print $9}' | xargs tar -zcvf /home/DexYang/userser0525.tar.gz

Linux下find命令使用总结 - CSDN博客

8-2

-type f -name test.py -delete 将2天前的.sh文件移动到old目录中 find ....1.Linux下find命令在目录结构中搜索文件,并执行指定的操作。 Linux下find命令提供...

LINUX exec函数的使用

qq_15695787 qq_15695787

07-02 167

(1)exec函数说明 fork函数是用于创建一个子进程,该子进程几乎是父进程的副本,而有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中启动另一个程序执行的方法。它可以根据指定的...

Linux中find命令,与exec合用,按修改时间查询等

lkforce lkforce

10-18 1242

find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。  exec解释: -exec  参数后面跟的是comm...

linux find用法整理 - CSDN博客

8-7

linux find 一个不错的网站http://www.linuxsir....NUM matchinglines.在一个文件中匹配到NUM后就退出....目录下(递归子目录)的所有的*.sh文件并移动到...

linux怎么将一个文件移动到另一个目录下 - CSDN博客

8-6

(3)把当前目录的一个子目录里的文件移动到另一个...参数备份目标文件后,备份文件的字尾会被加上一个...linux 将find的文件copy到另一个文件夹 yangguang...

【Linux】shell命令学习之find

xiajun07061225 xiajun07061225

09-22 4939

find命令主要用来进行文件或目录的查找。 -print指明打印出匹配的文件名 -print0指明使用'\0'定界符来打印每一个匹配的文件名  -name:按文件名查找 -iname忽略...

linux命令---cp 命令

u011630575 u011630575

11-23 1027

cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一。一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数。但是如果是...

没有更多推荐了,返回首页

个人资料

Vtamins

关注

原创

32

粉丝

14

喜欢

8

评论

6

等级:

访问:

3万+

积分:

736

排名:

7万+

勋章:

持之以恒

授予每个自然月内发布4篇或4篇以上原创或翻译IT博文的用户。不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!

 

最新文章

个人分类

展开

归档

展开

热门文章

最新评论

 

联系我们

客服

请扫描二维码联系客服

webmaster@csdn.net

400-660-0108

QQ客服 客服论坛

关于招聘广告服务 网站地图

©2018 CSDN版权所有 京ICP证09002463号

百度提供搜索支持

经营性网站备案信息

网络110报警服务

中国互联网举报中心

北京互联网违法和不良信息举报中心

 

关闭

 

 

关闭

 

关闭

 

 

不良信息举报

举报内容:

一个强大的linux命令——find之exec

举报原因:

色情 政治 抄袭 广告 招聘 骂人 其他

原文地址:

 

原因补充:

最多只允许输入30个字

 

  • 6
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值