2017-07-23 每天2个Linux find命令_exec参数
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
总用量 20
drwxr-xr-x. 2 root root 71 5月 17 04:18 findDir
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 uText
[root@localhost Documents]# find -type f -exec ls -l {} \;
//这里与直接执行ls -l命令不同的是,find命令会递归地将所有当前要查询的文件的子目录进行遍历,将每个后代文件均输出。
-rw-r--r--. 1 root root 45 5月 9 08:15 ./less1
-rw-r--r--. 1 root root 57 5月 9 08:16 ./less2
-rw-r--r--. 1 root root 664 5月 9 07:59 ./head_text
-rw-r--r--. 1 root root 259 5月 12 21:53 ./tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 ./tempory
-rw-r--r--. 1 root root 0 5月 15 18:21 ./newlocate
-rw-r--r--. 1 root root 0 5月 15 18:34 ./uText //只输出文件,却没有输出文件夹
-rw-r--r--. 1 root root 0 5月 17 03:50 ./findDir/t1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 ./findDir/T1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 ./findDir/T2.txt
-rw-r--r--. 1 root root 0 5月 17 04:18 ./findDir/p1.pdf
-rw-r--r--. 1 root root 0 5月 17 04:18 ./findDir/p2.pdf
[root@localhost Documents]#
复制代码
-type d与-type f的区别:
复制代码
[root@localhost Documents]# find -type d -exec ls -l {} \; //这里没有显示相对的路径
总用量 20
drwxr-xr-x. 2 root root 71 5月 17 04:18 findDir
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 uText
总用量 0
-rw-r--r--. 1 root root 0 5月 17 04:18 p1.pdf
-rw-r--r--. 1 root root 0 5月 17 04:18 p2.pdf
-rw-r--r--. 1 root root 0 5月 17 03:50 t1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 T1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 T2.txt
2)[root@localhost Document]# find . -type f -mtime +10 -exec rm -i {} \;
删除10天以外修改过的文件,删除时给出提醒
复制代码
[root@localhost Document]# find . -type f -mtime +14 -exec rm -i {} \;
由+14给出后没有反应,而给参数+10之后有反应,可知这些文件是10到14天以前建立或修改过的
[root@localhost Document]# find . -type f -mtime +10 -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 +10 -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文件
总用量 0
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 18 02:24 grepTest
-rw-r--r--. 1 root root 0 5月 6 22:15 mytext
-rw-r--r--. 1 root root 0 5月 6 22:15 mytext.txt
drwxr-xr-x. 2 root root 30 5月 6 22:02 newDir
-rw-rw-r--. 1 sunjimeng sunjimeng 0 5月 18 02:34 userfile
[root@localhost Document]# find /etc -name "passwd*" -exec grep "root" {} \;
//用grep命令查看在这些文件中是否存在一个root用户。(不太清楚什么意思)
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0: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--. 1 root root 0 5月 18 02:46 ./mvt1.txt
-rw-r--r--. 1 root root 0 5月 18 02:46 ./mvt2.txt
-rw-r--r--. 1 root root 0 5月 18 02:46 ./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
总用量 0
drwxr-xr-x. 2 root root 51 5月 18 02:47 newDir
[root@localhost Document]# ls -l ./newDir
总用量 0
-rw-r--r--. 1 root root 0 5月 18 02:46 mvt1.txt
-rw-r--r--. 1 root root 0 5月 18 02:46 mvt2.txt
-rw-r--r--. 1 root root 0 5月 18 02:46 mvt3.txt
6)[root@localhost sunjimeng]# find . -name "t*.txt" -mtime -1 -exec cat {} \; > ./Document/all.txt
合并查询到的多个文件的文本内容到一个新的文件
复制代码
[root@localhost Document]# ll //查看当前目录
总用量 0
drwxr-xr-x. 2 root root 51 5月 18 02:47 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--.
1 root root 42 5月 18 02:53 ./Document/t1.txt
-rw-r--r--. 1 root root 43 5月 18 02:54 ./Document/t2.txt
[root@localhost sunjimeng]# find -name "t*.txt" -mtime -1 -exec ls -l {} \;
//加上日期限制为一天以内修改过的之后,显示正确的文件-rw-r--r--. 1 root root 42 5月 18 02:53 ./Document/t1.txt
-rw-r--r--. 1 root root 43 5月 18 02:54 ./Document/t2.txt
[root@localhost sunjimeng]# find . -name "t*.txt" -mtime -1 -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打头的。
3. 执行exec系统调用,一般都是这样,用fork()函数新建立一个进程,然后让进程去执行exec调用。
4. 我们知道,在fork()建立 新进程之后,父进各与子进程共享代码段,但数据空间是分开的,
5. 但父进程会把自己数据空间的内容copy到子进程中去,还有上 下文也会copy到子进程中去。
6. 而为了提高效率,采用一种写时copy的策略,即创建子进程的时候,并不copy父进程的地址空间,
7. 父子进程拥有共同的地址空间,只有当子进程需要写入数据时(如向缓冲区写入数据),这时候会复制地址空间,
8. 复制缓冲区到子 进程中去。从而父子进程拥有独立的地址空间。而对于fork()之后执行exec后,
9. 这种策略能够很好的提高效率,如果一开始就 copy,那么exec之后,子进程的数据会被放弃,被新的进程所代替。
2017-07-23 每天2个Linux find命令_xargs参数
xargs 与 exec 的作用类似,但是xargs与find 一起使用时,一般配合管道一起使用。
前面的输出转换为后方指令的参数输入,使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。
(1)用法:
用法: [find命令] | [xargs] [其他命令]
(2)功能:
功能: 该命令的主要功能是从输入中构建和执行shell命令。与-exec类似,将find找到的文件当作参数执行接下来的命令。
(3)xargs参数的解释
在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。
但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。
错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,
不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。
在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,
并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高;
而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,
还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。
(4)实例:
1)[root@localhost Documents]# find . -type f -print find的输出到标准输出的参数-print
复制代码
[root@localhost Documents]# find . -type f -print //等价于find . -type f 因为默认是输出到标准输出的,所以加不加标准输出-print一样
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
2)[root@localhost Documents]# find . -type f -print | xargs file
查找当前目录下的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件
复制代码
[root@localhost Documents]# find . -type f -print | xargs file
./less1: ASCII text
./less2: ASCII text
./head_text: ASCII text
./tail_text: ASCII text
./tempory: ASCII text
./newlocate: empty
./uText: empty
./findDir/t1.txt: empty
./findDir/T1.txt: empty
./findDir/T2.txt: empty
./findDir/p1.pdf: empty
./findDir/p2.pdf: empty
3)[root@localhost Documents]# find . -type f | xargs ls -l 列出每个查找到的文件的详细信息,包括相对路径
复制代码
[root@localhost Documents]# find . -type f | xargs ll //为每一个找到的文件执行ll命令显然是不行的(虽然ll是“ls -l”的缩写)
xargs: ll: 没有那个文件或目录
[root@localhost Documents]# find . -type f | xargs ls -l
-rw-r--r--. 1 root root 0 5月 17 04:18 ./findDir/p1.pdf
-rw-r--r--. 1 root root 0 5月 17 04:18 ./findDir/p2.pdf
-rw-r--r--. 1 root root 0 5月 17 03:50 ./findDir/t1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 ./findDir/T1.txt
-rw-r--r--. 1 root root 0 5月 17 04:02 ./findDir/T2.txt
-rw-r--r--. 1 root root 664 5月 9 07:59 ./head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 ./less1
-rw-r--r--. 1 root root 57 5月 9 08:16 ./less2
-rw-r--r--. 1 root root 0 5月 15 18:21 ./newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 ./tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 ./tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 ./uText
[root@localhost Documents]# ls -l
总用量 20
drwxr-xr-x. 2 root root 71 5月 17 04:18 findDir
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 uText
4)[root@localhost Documents]# find . -type f -print | xargs chmod a-r
回收文件的权限(r代表读,w代表写,r代表可执行)
复制代码
[root@localhost Documents]# find . -type f -print | xargs chmod a-x //回收x权限
[root@localhost Documents]# ll
总用量 20
drwxr-xr-x. 2 root root 71 5月 17 04:18 findDir
-rw-r--r--. 1 root root 664 5月 9 07:59 head_text
-rw-r--r--. 1 root root 45 5月 9 08:15 less1
-rw-r--r--. 1 root root 57 5月 9 08:16 less2
-rw-r--r--. 1 root root 0 5月 15 18:21 newlocate
-rw-r--r--. 1 root root 259 5月 12 21:53 tail_text
-rw-r--r--. 1 root root 216 5月 12 22:24 tempory
-rw-r--r--. 1 root root 0 5月 15 18:34 uText
[root@localhost Documents]# find . -type f -print | xargs chmod a-r //回收r权限
[root@localhost Documents]# ll
总用量 20
drwxr-xr-x. 2 root root 71 5月 17 04:18 findDir
--w-------. 1 root root 664 5月 9 07:59 head_text
--w-------. 1 root root 45 5月 9 08:15 less1
--w-------. 1 root root 57 5月 9 08:16 less2
--w-------. 1 root root 0 5月 15 18:21 newlocate
--w-------. 1 root root 259 5月 12 21:53 tail_text
--w-------. 1 root root 216 5月 12 22:24 tempory
--w-------. 1 root root 0 5月 15 18:34 uText
5)[root@localhost sunjimeng]# find ./Document -name "all.txt" -print | xargs echo "Success" >/home/sunjimeng/Documents/core.log
将找到的文件加上相对路径和自定义字符串输出到另一个文件中
复制代码
[root@localhost Document]# cat all.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
[root@localhost Document]# cd ./
[root@localhost Document]# cd ../
[root@localhost sunjimeng]# find ./Document -name "all.txt" -print | xargs echo "Success" >/home/sunjimeng/Documents/core.log
[root@localhost sunjimeng]# cat /home/sunjimeng/Documents/core.log //可以看出上个命令将什么拷进了自定义文件中
Success ./Document/all.txt
[root@localhost sunjimeng]# find ./Document -name "all.txt"
./Document/all.txt
6)[root@localhost sunjimeng]# find ./Document -name "all.txt" | xargs cat >/home/sunjimeng/Documents/t3.txt 将找到的文件内容拷贝到另一个文件中
复制代码
[root@localhost Document]# cat all.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
[root@localhost sunjimeng]# find ./Document -name "all.txt" | xargs cat >/home/sunjimeng/Documents/t3.txt //(5)输出的是自定义内容,而这个命令是把找到的文件内容拷贝一份到自定义文件中
[root@localhost sunjimeng]# cat /home/sunjimeng/Documents/t3.txt
this is t1.txt!
I'm testing -exec option!
this is t2.txt!
I'm testing -exec optioin!
复制代码
7)[root@localhost sunjimeng]# find ./ -type f | xargs ls -l | awk 'BEGIN{size=0}{size+=$5};END{print size}' 统计当前目录下所有文件的大小,含子目录,精确到字节
[root@localhost sunjimeng]# find ./ -type f | xargs ls -l | awk 'BEGIN{size=0}{size+=$5};END{print size}'
5173736
8)[root@localhost Documents]# find . -type f -print | xargs grep "Lost"
查找find找到的文件中有没有包含"Lost"字符串的
复制代码
[root@localhost Documents]# find . -type f -print | xargs grep "t3.txt" //虽然当前目录下有t3.txt,但查询的结果却为空
[root@localhost Documents]# find . -type f -print
./less1
./less2
./head_text
./tail_text
./tempory
./newlocate
./uText
./findDir/t1.txt
./findDir/T1.txt
./findDir/T2.txt
./findDir/p1.pdf
./findDir/p2.pdf
./find
./core.log
./t3.txt
复制代码
复制代码
[root@localhost Documents]# cat less1
Lost means Get!
No losing No getting!
End!
[root@localhost Documents]# find . -type f -print | xargs grep "Lost"
//表明它是根据文件内容进行匹配的,而不是根据查找到的文件的名字。
./less1:Lost means Get!
9)[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt
提醒是否执行find后面的其他命令
[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt
这里用mv命令出了错误,后面解决!
mv t2.txt ./t1.txt ?...n
[root@localhost findDir]# find -type f -name "t1.txt" -print |xargs -p mv t2.txt
mv t2.txt ./t1.txt ?...y
mv: 无法获取"t2.txt" 的文件状态(stat): 没有那个文件或目录