在使用 find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。

  find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

  在有些系统中,使用-exec选项会为处理每一个匹配到的文件而发起一个相应的进程,并非将匹配到的文件全部作为参数一次执行;这样在有些情况下就会出现进程过多,系统性能下降的问题,因而效率不高; 而使用xargs命令则只有一个进程。另外,在使用xargs命令时,究竟是一次获取所有的参数,还是分批取得参数,以及每一次获取参数的数目都会根据该命令的选项及系统内核中相应的可调参数来确定。

使用实例:

实例1: 查找系统中的每一个普通文件,然后使用xargs命令来测试它们分别属于哪类文件

命令:

  find . -type f -print | xargs file

输出:

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root       - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxrwx  root root    - : test3

drwxrwxrwx  root root    - : test4

[root@localhost test]# find . -type f -print | xargs file

./log2014.log: empty

./log2013.log: empty

./log2012.log: ASCII text

[root@localhost test]#

实例2:在整个系统中查找内存信息转储文件(core dump) ,然后把结果保存到/tmp/core.log 文件中

命令:

   find / -name "core" -print | xargs echo "" >/tmp/core.log

输出:

[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log

[root@localhost test]# cd /tmp

[root@localhost tmp]# ll

总计 

-rw-r--r--  root root  - : core.log

drwx------  root root  - : ssh-TzcZDx1766

drwx------  root root  - : ssh-ykiRPk1815

drwx------  root root  - : vmware-root

实例3:在当前目录下查找所有用户具有读、写和执行权限的文件,并收回相应的写权限

命令:

  find . -perm -7 -print | xargs chmod o-w

输出:

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root       - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxrwx  root root    - : test3

drwxrwxrwx  root root    - : test4

[root@localhost test]#  -print | xargs chmod o-w

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root       - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxr-x  root root    - : test3

drwxrwxr-x  root root    - : test4

[root@localhost test]#

说明:

  执行命令后,文件夹scf、test3和test4的权限都发生改变

实例4:用grep命令在所有的普通文件中搜索hostname这个词

命令:

  find . -type f -print | xargs grep "hostname"

输出:

[root@localhost test]# find . -type f -print | xargs grep "hostname"

./log2013.log:hostnamebaidu=baidu.com

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

实例5:用grep命令在当前目录下的所有普通文件中搜索hostnames这个词

命令:

  find . -name \* -type f -print | xargs grep "hostnames"

输出:

[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"

./log2013.log:hostnamesina=sina.com

./log2013.log:hostnames=true[root@localhost test]#

说明:

  注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。

实例6:使用xargs执行mv 

命令:

  find . -name "*.log" | xargs -i mv {} test4

输出:

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxr-x  root root    - : test3

drwxrwxr-x  root root    - : test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

总计 [root@localhost test4]# cd ..

[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4

[root@localhost test]# ll

总计 12drwxr-xr-x  root root  - : scf

drwxrwxr-x  root root  - : test3

drwxrwxr-x  root root  - : test4

[root@localhost test]# cd test4/

[root@localhost test4]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

[root@localhost test4]#

实例7:find后执行xargs提示xargs: argument line too long解决方法:

命令:

  find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

输出:

[root@pd test4]#   -print0 |  -l1 -t rm -f

rm -f 

[root@pdtest4]#

说明:

  -l1是一次处理一个;-t是处理之前打印出命令

实例8:使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[] 

命令:

输出:

[root@localhost test]# ll

总计 12drwxr-xr-x  root root  - : scf

drwxrwxr-x  root root  - : test3

drwxrwxr-x  root root  - : test4

[root@localhost test]# cd test4

[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..

[root@localhost test4]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

[root@localhost test4]# cd ..

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxr-x  root root    - : test3

drwxrwxr-x  root root    - : test4

[root@localhost test]#

说明:

  使用-i参数默认的前面输出用{}代替,-I参数可以指定其他代替字符,如例子中的[]

实例9:xargs的-p参数的使用 

命令:

  find . -name "*.log" | xargs -p -i mv {} ..

输出:

[root@localhost test3]# ll

总计 

-rw-r--r--  root root  - : log2015.log

[root@localhost test3]# cd ..

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

drwxr-xr-x  root root    - : scf

drwxrwxr-x  root root    - : test3

drwxrwxr-x  root root    - : test4

[root@localhost test]# cd test3

[root@localhost test3]#  find . -name "*.log" | xargs -p -i mv {} ..

mv ./log2015.log .. ?...y

[root@localhost test3]# ll

总计 [root@localhost test3]# cd ..

[root@localhost test]# ll

总计 

-rw-r--r--  root root  - : log2012.log

-rw-r--r--  root root      - : log2013.log

-rw-r--r--  root root       - : log2014.log

-rw-r--r--  root root       - : log2015.log

drwxr-xr-x  root root    - : scf

drwxrwxr-x  root root    - : test3

drwxrwxr-x  root root    - : test4

[root@localhost test]#

说明:

  -p参数会提示让你确认是否执行后面的命令,y执行,n不执行。

linux常用命令:find命令之xargs的更多相关文章

  1. ## 本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘

    本篇文章对linux常用的一些命令做一下总结,如有需要补充以及不懂得地方,请在下方留言 适合于linux初学者,以及对命令掌握不牢的用来备忘一,磁盘管理1.显示当前目录位置 pwd2.切换目录 cd ...

  2. Linux 常用的压缩命令有 gzip 和 zip

    Linux 常用的压缩命令有 gzip 和 zip,两种压缩包的结尾不同:zip 压缩的后文件是 *.zip ,而 gzip 压缩后的文件 *.gz 相应的解压缩命令则是 gunzip 和 unzip ...

  3. LINUX常用配置及命令

    一.   Fedora系统配置 1.      [设置网卡IP] 步骤如下: 1)     用root用户登陆,打开/etc/sysconfig/network-scripts/ifcfg-eth0文 ...

  4. Linux常用的基础命令总结

    man 查看英文命令帮助   可以看作--help 拷贝目录的命令cp -a  包含所有 ls -a 显示所有文件包括隐藏文件  -ld ls -F 过滤目录文件(给不同类型文件结尾加上不同的符号) ...

  5. Java线上问题排查思路及Linux常用问题分析命令学习

    前言 之前线上有过一两次OOM的问题,但是每次定位问题都有点手足无措的感觉,刚好利用星期天,以测试环境为模版来学习一下Linux常用的几个排查问题的命令. 也可以帮助自己在以后的工作中快速的排查线上问 ...

  6. linux常用60条命令 转

    Linux必学的60个命令   Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命 ...

  7. 入门学习Linux常用必会命令实例详解

    Linux提供了大量的命令,利用它可以有效地完成大量的工作,如磁盘操作.文件存取.目录操作.进程管理.文件权限设定等.所以,在Linux系统上工作离不开使用系统提供的命令.要想真正理解Linux系统, ...

  8. 网络编程学习笔记-linux常用的网络命令

    网络参数设置命令 所有时刻如果你想要做好自己的网络参数设置,包括IP参数.路由参数和无线网络等,就得要了解下面这些相关的命令才行.其中Route及ip这两条命令是比较重要的.当然,比较早期的用法,我们 ...

  9. linux常用20条命令

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

  10. linux常用配置文件和命令总结

    常用配置文件说明: 1..设置-n永远生效:Vim的配置文件:命令模式想永久生效, ~/.vimrc,新建文件,在里面输入保存即可 2.设置别名永远生效:在~/.bashrc  修改当前用户家目录里的 ...

随机推荐

  1. 在UI自动化测试中使用flaky插件运行失败用例

    在UI自动化测试中,有时候经常会提示跑用例失败,在单步或单个用例调试时,用例却成功,这个失败的因素主要有环境.代码或前端定位等原因. 可以看这篇文章<我们是如何让UI测试变得稳定的>中有详 ...

  2. java的多态示例

    子类是父类的类型,但父类不是子类的类型. 子类的实例可以声明为父类型,但父类的实例不能声明为子类型. class Vehicle {} public class Car extends Vehicle ...

  3. vs2010版本注释

    转:http://www.cnblogs.com/chaselwang/p/3580839.html 关于Visual Studio 20**自动添加头部注释信息 作为一个万年潜水党,不关这一篇文章技 ...

  4. CVPixelBuffer

    https://www.cnblogs.com/psklf/p/7700834.html https://stackoverflow.com/questions/16475737/convert-ui ...

  5. 对position的认知观

    position :absolute 认识以前有的理解不正确,以为没有设置left,top 与设置left :0,top : 0是一样的,现在认识为,错误! 没有设置的时候,该absolute元素{由 ...

  6. 新建虚拟机_WIN7 32位系统

    准备工作:下载win7 32位纯净版镜像文件 大部分步骤与安装XP系统相似,此处只说明一下不同: 创建好虚拟机后启动有报错:CHS data ERROR,无法从CD/DVD启动 编辑虚拟机--> ...

  7. Is It A Tree?----poj1308

    http://poj.org/problem?id=1308 #include<stdio.h> #include<string.h> #include<iostream ...

  8. Heavy Transportation---poj1797

    求(Dijkstra算法,求每条路径上的最小值 的最大值)和青蛙的那题类似:   #include<iostream> #include<stdio.h> #include&l ...

  9. webstorm添加调试nodejs

    打开run菜单选择Edit Configurations 展开defaults菜单,选择nodejs 点击+按钮,选择Node.js,出现下面弹出框. 点击ok保存

  10. android switch控件

    <Switch android:layout_width="wrap_content" android:layout_height="@dimen/minCellH ...