转
一个强大的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
相关热词: 一个和 一个 一个标题一个框 一个圈里一个c 一个端口一个ip
▼查看关于本篇文章更多信息
æ³å¯¹ä½è说ç¹ä»ä¹
添加代码片
- HTML/XML
- objective-c
- Ruby
- PHP
- C
- C++
- JavaScript
- Python
- Java
- CSS
- SQL
- 其它
还能输入1000个字符
find . -name "*.txt" -exec rm {} \; find . -name "*.txt" | xargs rm {} -exec 1.参数是一个一个传递...
Linux命令 find和mv的结合使用:查找文件,移动到某个目录 - CSDN博客
6-18
6-6
Linux下用find查找并复制指定文件到指定目录下 - CSDN博客
6-15
8-1
不足:用ctrl+r搜索历史命令找到grep之后,如果想改变查询词,还需要从末尾移动光标...在使用linux时,经常需要进行文件查找。其中查找的命令主要有find和grep。两个命令...
exec选项后面跟随着所要执行的命令或脚本,然后是一对儿 {},一个空格和一个\,最后是一个分号。 cmd {} \; 看例子: zoer@ubuntu:~$ awk 'BEGIN{cou...
linux环境下find的所有用法以及粘滞位的说明 - CSDN博客
6-30
Linux下用find查找并复制指定文件到指定目录下 - CSDN博客
8-10
linux find查找多个指定目录并复制到指定文件夹 ...shell脚本:cp某个目录下筛选(find)后的某些文件到另...移动类 37篇 金融类 31篇 信息保存 56篇 Web...
分类: LINUX 原文地址:linux exec用法总结 作者:taosk Linux中exec的用法总结 先总结一个表: exec命令 ...
8-4
1.方法一 ll -lrt | grep May\ 25 | awk '{print $9}' | xargs tar -zcvf /home/DexYang/userser0525.tar.gz
8-2
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。 exec解释: -exec 参数后面跟的是comm...
8-7
linux怎么将一个文件移动到另一个目录下 - CSDN博客
8-6
(3)把当前目录的一个子目录里的文件移动到另一个...参数备份目标文件后,备份文件的字尾会被加上一个...linux 将find的文件copy到另一个文件夹 yangguang...
find命令主要用来进行文件或目录的查找。 -print指明打印出匹配的文件名 -print0指明使用'\0'定界符来打印每一个匹配的文件名 -name:按文件名查找 -iname忽略...
没有更多推荐了,返回首页
个人资料
粉丝
14
喜欢
8
评论
6
等级:
访问:
3万+
积分:
736
排名:
7万+
勋章:
持之以恒
授予每个自然月内发布4篇或4篇以上原创或翻译IT博文的用户。不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
最新文章
个人分类
- 学习笔记 20篇
- 考试 1篇
- hadoop 7篇
- zookeeper 3篇
- Java 3篇
- 设计模式 1篇
- linux 23篇
- 面试攻略 12篇
- go 2篇
- 大数据 3篇
- python 2篇
- 服务器 1篇
- 安全 1篇
展开
归档
- 2018年3月 10篇
- 2018年1月 2篇
- 2017年10月 2篇
- 2017年9月 5篇
- 2017年3月 1篇
- 2017年2月 3篇
- 2017年1月 6篇
- 2016年12月 15篇
- 2016年11月 6篇
- 2016年5月 2篇
- 2016年4月 2篇
- 2016年3月 1篇
展开
热门文章
- 穿透内网--实现从外网远程桌面或ssh到内网x
阅读量:9490
- Java面试之垃圾回收总结
阅读量:7433
- 一个强大的linux命令——find之exec
阅读量:7418
- Python Excel数据查找
阅读量:1995
- Go语言用GoPacket抓包分析
阅读量:1339
最新评论
- 穿透内网--实现从外网远程桌面或s...
yichang99:用N2N,简单有效。
- 穿透内网--实现从外网远程桌面或s...
u014762921:[reply]weixin_38879056[/reply] 同时存在两个服务端的,如果跳板机的N...
- 穿透内网--实现从外网远程桌面或s...
weixin_38879056:vps链接不上去 这是报错的信息: 几个在同一 IP 地址的 VPN Server。您可以指定目...
- LintCode之最长公共子串(L...
DCX_abc:感谢分享
- 穿透内网--实现从外网远程桌面或s...
u012348345:很好的文章
联系我们
请扫描二维码联系客服
400-660-0108
QQ客服 客服论坛
©2018 CSDN版权所有 京ICP证09002463号
-
2
写评论
目录
收藏
微信
微博
QQ
关闭
关闭
关闭
不良信息举报
举报内容:
一个强大的linux命令——find之exec
举报原因:
色情 政治 抄袭 广告 招聘 骂人 其他
原文地址:
原因补充:
最多只允许输入30个字