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--. root root 5月 : ./findDir/p1.pdf
-rw-r--r--. root root 5月 : ./findDir/p2.pdf
-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月 : ./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]# 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

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
总用量
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 -print | xargs chmod a-r //回收r权限
[root@localhost Documents]# ll
总用量
drwxr-xr-x. root root 5月 : findDir
--w-------. root root 5月 : head_text
--w-------. root root 5月 : less1
--w-------. root root 5月 : less2
--w-------. root root 5月 : newlocate
--w-------. root root 5月 : tail_text
--w-------. root root 5月 : tempory
--w-------. root root 5月 : 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}'

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): 没有那个文件或目录

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

  1. linux常用命令(21)tar命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...

  2. linux 的21个命令:

    1:ls  [list] 2:输入输出重定向 3:| 管道 4:chmod 5:cd 6:mkdir 7:cp 8:rm 9:mv 10:cat 11:pwd 12:ln[link] 13:grep ...

  3. linux 20个常用命令

    一.文件和目录 1. cd命令 (它用于切换当前目录,它的参数是要切换到的目录的路径,可以是绝对路径,也可以是相对路径) cd /home    进入 '/ home' 目录 cd ..       ...

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

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

  5. 每天一个linux命令(21):tar命令

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...

  6. 2-1 Linux 操作系统及常用命令

    根据马哥linux初级视频 2-1.2-2来编辑 1. GUI与CLI GUI: Graphic User Interface CLI: Command Line Interface 注:在Windo ...

  7. 每天一个linux命令(48):watch命令

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  8. 每天一个linux命令目录

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

  9. 每天一个linux命令(51):lsof命令

    lsof(list open files)是一个列出当前系统打开文件的工具.在linux环境下,任何事物都以文件的形式存在,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件.所以如传输控制协议 ...

随机推荐

  1. Android开发之Serializable 和 Parcelable的差别(源码分享)

    android 中自己定义的对象序列化的问题有两个选择一个是Parcelable,另外一个是Serializable. 一 序列化原因: 1.永久性保存对象.保存对象的字节序列到本地文件里. 2.通过 ...

  2. oc 经常用到弹出view的方法

    #pragma mark 弹出view -(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur { CAKeyframeA ...

  3. 【hadoop之翊】——windows 7使用eclipse下hadoop应用开发环境搭建

    由于一些缘故,这节内容到如今才写.事实上弄hadoop有一段时间了,能够编写一些小程序了,今天来还是来说说环境的搭建.... 说明一下:这篇文章的步骤是接上一篇的hadoop文章的:http://bl ...

  4. Android Studio 2.0 正式版公布啦 (首次中文翻译)

    Android Studio 2.0 公布了,添加了一些新特性: 1. 更加完好的 Instant Run 2. 更快的 Android Emulator 3.GPU Debugger Preview ...

  5. Mybatis_遇到的问题汇总

    1.The setting logImpl is not known 我在参考某个网站学习mybatis时,出现这个错误,后来找到的原因是因为mybatis的版本(3.1.1)太低,换成3.3.1就没 ...

  6. FP Tree算法原理总结(转载)

    FP Tree算法原理总结 在Apriori算法原理总结中,我们对Apriori算法的原理做了总结.作为一个挖掘频繁项集的算法,Apriori算法需要多次扫描数据,I/O是很大的瓶颈.为了解决这个问题 ...

  7. u-boot README--linux support

    Linux HOWTO:============ Porting Linux to U-Boot based systems:------------------------------------- ...

  8. PHP性能之语言性能优化说明

    PHP语言性能优化优化啥? 如下图所示,PHP直接执行的是opcode,所以我们尽量减少扫描和转码解析. 这是我们第一个优化点,尽量使用PHP内置的函数代替我们的代码来实现同样的功能. 和我们自己写的 ...

  9. mac虚拟机搭建自动化环境-wda和python wda client

    尽量升级Xcode到最新版,保持iPhone的版本大于9.3 1.安装webDriverAgent到ios真机 从github上下载代码:git clone https://github.com/fa ...

  10. c#中关于compare比较的一点注意事项

    一直没有太注意,今天发现在compare比较两个字符串的时候出了点小问题 如果我设置了两个字符串 一个是“2”,一个是“12” 那么在比较的时候 第一个会大于第二个: 如果第一个是“02”,第二个是“ ...