find命令的exec参数,用于find查找命令完成以后的后续操作。

    (1)用法:

用法:  [find命令]  [-exec  其他命令 {} \;]

    (2)功能:

功能:-exec find命令对匹配的文件执行该参数所给出的其他linux命令。

    (3)-exec的解释:

-exec参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的 考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。

{}   花括号代表前面find查找出来的文件名。

    (4)实例:

1)[root@localhost Documents]# find -type f -exec ls -l {} \;              将查找出来的结果用ls -l命令列出

[root@localhost sunjimeng]# cd Documents
[root@localhost Documents]# ll                    //ll命令等价于 ls -l
总用量
drwxr-xr-x. root root 5月 : findDir
-rw-r--r--. root root 5月 : head_text
-rw-r--r--. root root 5月 : less1
-rw-r--r--. root root 5月 : less2
-rw-r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : tail_text
-rw-r--r--. root root 5月 : tempory
-rw-r--r--. root root 5月 : uText
[root@localhost Documents]# find -type f -exec ls -l {} \;   //这里与直接执行ls -l命令不同的是,find命令会递归地将所有当前要查询的文件的子目录进行遍历,将每个后代文件均输出。
-rw-r--r--. root root 5月 : ./less1
-rw-r--r--. root root 5月 : ./less2
-rw-r--r--. root root 5月 : ./head_text
-rw-r--r--. root root 5月 : ./tail_text
-rw-r--r--. root root 5月 : ./tempory
-rw-r--r--. root root 5月 : ./newlocate
-rw-r--r--. root root 5月 : ./uText   //只输出文件,却没有输出文件夹
-rw-r--r--. root root 5月 : ./findDir/t1.txt
-rw-r--r--. root root 5月 : ./findDir/T1.txt
-rw-r--r--. root root 5月 : ./findDir/T2.txt
-rw-r--r--. root root 5月 : ./findDir/p1.pdf
-rw-r--r--. root root 5月 : ./findDir/p2.pdf
[root@localhost Documents]#

-type d与-type f的区别:

[root@localhost Documents]# find -type d -exec ls -l {} \;     //这里没有显示相对的路径
总用量
drwxr-xr-x. root root 5月 : findDir
-rw-r--r--. root root 5月 : head_text
-rw-r--r--. root root 5月 : less1
-rw-r--r--. root root 5月 : less2
-rw-r--r--. root root 5月 : newlocate
-rw-r--r--. root root 5月 : tail_text
-rw-r--r--. root root 5月 : tempory
-rw-r--r--. root root 5月 : uText
总用量
-rw-r--r--. root root 5月 : p1.pdf
-rw-r--r--. root root 5月 : p2.pdf
-rw-r--r--. root root 5月 : t1.txt
-rw-r--r--. root root 5月 : T1.txt
-rw-r--r--. root root 5月 : T2.txt

2)[root@localhost Document]# find . -type f -mtime +10 -exec rm -i {} \;        删除10天以外修改过的文件,删除时给出提醒

[root@localhost Document]# find . -type f -mtime + -exec rm -i {} \;      由+14给出后没有反应,而给参数+10之后有反应,可知这些文件是10到14天以前建立或修改过的
[root@localhost Document]# find . -type f -mtime + -exec rm -i {} \;
rm:是否删除普通空文件 "./newDir/text1"?n
rm:是否删除普通空文件 "./newDir/text2"?n
rm:是否删除普通空文件 "./text1/newDir/text1"?n
rm:是否删除普通空文件 "./text1/newDir/text2"?n
rm:是否删除普通空文件 "./text2/newDir/text1"?n
rm:是否删除普通空文件 "./text2/newDir/text2"?n
rm:是否删除普通空文件 "./text3/text1"?n
rm:是否删除普通空文件 "./text3/text2"?n
rm:是否删除普通空文件 "./text4/text1"?n
rm:是否删除普通空文件 "./text4/text2"?n
rm:是否删除普通空文件 "./mytext"?n
rm:是否删除普通空文件 "./mytext.txt"?n

3)[root@localhost Document]# find . -type f -mtime +10 -ok rm {} \;            另一种方法实现删除时的交互

[root@localhost Document]# find . -type f -mtime + -ok rm {} \;
< rm ... ./newDir/text1 > ? n
< rm ... ./newDir/text2 > ? n
< rm ... ./text1/newDir/text1 > ? n
< rm ... ./text1/newDir/text2 > ? n
< rm ... ./text2/newDir/text1 > ? n
< rm ... ./text2/newDir/text2 > ? n
< rm ... ./text3/text1 > ? n
< rm ... ./text3/text2 > ? n
< rm ... ./text4/text1 > ? n
< rm ... ./text4/text2 > ? n
< rm ... ./mytext > ? n
< rm ... ./mytext.txt > ? n

4)[root@localhost Document]# find /etc -name "passwd*" -exec grep "root" {} \;      查看查询出来的文件中有没有root用户

[root@localhost Document]# ll                          //此目录下有用户文件,也有root文件
总用量
-rw-rw-r--. sunjimeng sunjimeng 5月 : grepTest
-rw-r--r--. root root 5月 : mytext
-rw-r--r--. root root 5月 : mytext.txt
drwxr-xr-x. root root 5月 : newDir
-rw-rw-r--. sunjimeng sunjimeng 5月 : userfile
[root@localhost Document]# find /etc -name "passwd*" -exec grep "root" {} \; //用grep命令查看在这些文件中是否存在一个root用户。(不太清楚什么意思)
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
root:x:::root:/root:/bin/bash
operator:x:::operator:/root:/sbin/nologin
[root@localhost Document]# find . -type f -exec grep "root" {} \;            //然而用这个命令,一点反应没有!

5)[root@localhost Document]# find . -name "mv*" -exec mv {} newDir \;          将查询到的内容移动到newDir目录下

[root@localhost Document]# ls
newDir
[root@localhost Document]# touch {mvt1.txt,mvt2.txt,mvt3.txt}
[root@localhost Document]# find . -name "mv*" -exec ls -l {} \;
-rw-r--r--. root root 5月 : ./mvt1.txt
-rw-r--r--. root root 5月 : ./mvt2.txt
-rw-r--r--. root root 5月 : ./mvt3.txt
[root@localhost Document]# find . -name "mv*" -exec mv newDir {} \;  //注意顺序不要弄错
mv: 无法以目录"newDir" 来覆盖非目录"./mvt1.txt"
mv: 无法以目录"newDir" 来覆盖非目录"./mvt2.txt"
mv: 无法以目录"newDir" 来覆盖非目录"./mvt3.txt"
[root@localhost Document]# find . -name "mv*" -exec mv {} newDir \; //mv命令将前面的文件集移动到后面的文件夹中
[root@localhost Document]# ll
总用量
drwxr-xr-x. root root 5月 : newDir
[root@localhost Document]# ls -l ./newDir
总用量
-rw-r--r--. root root 5月 : mvt1.txt
-rw-r--r--. root root 5月 : mvt2.txt
-rw-r--r--. root root 5月 : mvt3.txt

6)[root@localhost sunjimeng]# find . -name "t*.txt" -mtime -1 -exec cat {} \; > ./Document/all.txt   合并查询到的多个文件的文本内容到一个新的文件

[root@localhost Document]# ll            //查看当前目录
总用量
drwxr-xr-x. root root 5月 : newDir
[root@localhost Document]# cat >t1.txt <<EOF //新建t1.txt
> this is t1.txt!
> I'm testing -exec option!
> EOF
[root@localhost Document]# cat >t2.txt <<EOF //新建t2.txt
> this is t2.txt!
> I'm testing -exec optioin!
> EOF
[root@localhost Document]# cd ../
[root@localhost sunjimeng]# find -name "t*.txt" -exec ls -l {} \; //查出了很多,前几项红的不是我们要的
-rw-rw-r--. 1 sunjimeng sunjimeng 58 5月 4 21:45 ./.local/share/Trash/files/test1.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月 4 21:46 ./.local/share/Trash/files/test2.txt
-rw-rw-r--. 1 sunjimeng sunjimeng 61 5月 4 21:47 ./.local/share/Trash/files/test3.txt-rw-r--r--. root root 5月 : ./Document/t1.txt
-rw-r--r--. root root 5月 : ./Document/t2.txt
[root@localhost sunjimeng]# find -name "t*.txt" -mtime - -exec ls -l {} \; //加上日期限制为一天以内修改过的之后,显示正确的文件-rw-r--r--. root root 5月 : ./Document/t1.txt
-rw-r--r--. root root 5月 : ./Document/t2.txt
[root@localhost sunjimeng]# find . -name "t*.txt" -mtime - -exec cat {} \; > ./Document/all.txt //将文件输出到当前子目录Document下的all.txt文件中,如果不存在则创建
[root@localhost sunjimeng]# cat ./Document/all.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!

7)[root@localhost sunjimeng]# find ./Documents -type f -name "*.txt" -exec printf "File: %s\n" {} \;    将查询到的文件当作字符数组,用类似c语言的printf的形式控制格式输出

[root@localhost sunjimeng]# find ./Documents -type f -name "*.txt" -exec printf "File: %s\n" {} \;
File: ./Documents/findDir/t1.txt
File: ./Documents/findDir/T1.txt
File: ./Documents/findDir/T2.txt
[root@localhost sunjimeng]# find ./Documents -type f -name "*.txt" -exec printf "File: %s\n\n" {} \; //也可以多加一个\n
File: ./Documents/findDir/t1.txt File: ./Documents/findDir/T1.txt File: ./Documents/findDir/T2.txt [root@localhost sunjimeng]#

(4)其他:

Linux中exec命令相关:

  bash shell的命令分为两类:外部命令和内部命令。外部命令是通过系统调用或独立的程序实现的,如sed、awk等等。内部命令是由特殊的文件格式(.def)所实现,如cd、history、exec等等。

1. 系统调用exec是以新的进程去代替原来的进程,但进程的PID保持不变。因此,可以这样认为,exec系统调用并没有创建新的进程,只是替换了原来进程上下文的内容。原进程的代码段,数据段,堆栈段被新的进程所代替。

fork的概念

fork是linux的系统调用,用来创建子进程(child process)。子进程是父进程(parent process)的一个副本,从父进程那里获得一定的资源分配以及继承父进程的环境。子进程与父进程唯一不同的地方在于pid(process id)。

一个进程主要包括以下几个方面的内容:

(1)一个可以执行的程序

(2) 与进程相关联的全部数据(包括变量,内存,缓冲区)

(3)程序上下文(程序计数器PC,保存程序执行的位置)

2. exec是一个函数簇,由6个函数组成,分别是以excl和execv打头的。 执行exec系统调用,一般都是这样,用fork()函数新建立一个进程,然后让进程去执行exec调用。我们知道,在fork()建立 新进程之后,父进各与子进程共享代码段,但数据空间是分开的,但父进程会把自己数据空间的内容copy到子进程中去,还有上 下文也会copy到子进程中去。而为了提高效率,采用一种写时copy的策略,即创建子进程的时候,并不copy父进程的地址空间, 父子进程拥有共同的地址空间,只有当子进程需要写入数据时(如向缓冲区写入数据),这时候会复制地址空间,复制缓冲区到子 进程中去。从而父子进程拥有独立的地址空间。而对于fork()之后执行exec后,这种策略能够很好的提高效率,如果一开始就 copy,那么exec之后,子进程的数据会被放弃,被新的进程所代替。

每天一个Linux命令(20)find命令_exec参数的更多相关文章

  1. linux常用20条命令

    1.cd命令 这是一个非常基本,也是大家经常需要使用的命令,它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径.如: cd /root/Docements # 切换到 ...

  2. 每天一个Linux命令:find(20)

    find find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间 ...

  3. 每天一个 Linux 命令(20):find命令之exec

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

  4. 每天一个linux命令(20):find命令之exec

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

  5. 每天一个Linux命令(20)--find命令之exec

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

  6. 【转】每天一个linux命令(20):find命令之exec

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html find是我们很常用的一个Linux命令,但是我们一般查找出来的并不 ...

  7. 每天一个 Linux 命令(21):find命令之xargs

    在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行.但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出 ...

  8. 每天一个linux命令(46):vmstat命令

    vmstat是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体情况进行统计,不足之处是无法对某个进程进行深 ...

  9. 每天一个linux命令目录

    出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...

随机推荐

  1. Oracle case when then else end的两种用法

    查询表结构 SELECT T.COLUMN_ID, T.COLUMN_NAME, (CASE WHEN (T.DATA_TYPE = 'VARCHAR2' OR T.DATA_TYPE = 'RAW' ...

  2. 奥巴马(Obama)获胜演讲全文[中英对照]+高清视频下载

    http://www.amznz.com/obama-speech/如果还有人对美国是否凡事都有可能存疑,还有人怀疑美国奠基者的梦想在我们所处的时代是否依然鲜活,还有人质疑我们的民主制度的力量,那么今 ...

  3. 正则表达式匹配 href 和text内容

    string pattern = @"<a[^>]*href=(""(?<href>[^""]*)""|' ...

  4. IBM Rational AppScan使用详细说明

    转自:http://www.nxadmin.com/tools/675.html 本文将详细介绍Appscan功能选项设置的细节,适合E文一般,初次接触Appscan的童鞋参考阅读. Appscan是 ...

  5. Linux的文件传输命令总结

    由于工作原因,须要常常在不同的server见进行文件传输,特别是大文件的传输,因此对linux下不同server间传输数据命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp ...

  6. 在SDL中显示GBK点阵汉字

    大家注意到没有,RA2的中文版本使用的是GBK点阵字库,这样做有一个好处:不管玩家是用的简体还是繁体都能识别显示的文字. GBK的意思大概是“国家标准汉字扩展字符集”吧,记不清了.但它的确是个好东东, ...

  7. httpd在嵌入式中应用

    在启动脚本合适位置添加: httpd -h /usr/app/www/ 即开始httpd服务,并定位到/usr/app/www/ 注:busybox已支持httpd命令,所以直接用即可. busybo ...

  8. go的url解析

    对于解析url,是一个常见的场景,下面就来说这个,直接见代码: package main import ( "fmt" "net/url" "stri ...

  9. Unity5 怎样做资源管理和增量更新

    工具 Unity 中的资源来源有三个途径:一个是Unity自己主动打包资源.一个是Resources.一个是AssetBundle. Unity自己主动打包资源是指在Unity场景中直接使用到的资源会 ...

  10. Linux tar包安装Nginx

    1.首先安装依赖包(依赖包有点多.我们採用yum的方式来安装) yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel ...