在使用 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]# find . -perm - -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]#  find . -type f -atime + -print0 | xargs - -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. Unix/Linux常用文件操作

    Unix/Linux常用文件操作 秘籍:man命令是Unix/Linux中最常用的命令,因为命令行命令过多,我相信每个人都会经常忘记某些命令的用法,man命令就可以显示一个命令的所有选项,参数和说明, ...

  2. qt ui程序使用Linux的文件操作open、close (转)

    原文地址:qt ui程序使用Linux的文件操作open.close 作者:kjpioo 提出这个问题是因为在qt的QWidget类型的对象中,close()函数会和QWidget::close()冲 ...

  3. Linux常用命令--文件操作

    常用Linux命令笔记(1) 1. 创建文件/文件夹 参考博客:https://www.cnblogs.com/lclq/p/5741852.html. 使用cat命令创建新文件: 输入命令 # ca ...

  4. Go连接到Linux服务器进行操作-执行shell命令&&上传文件

    Go连接到Linux服务器进行操作 使用密码连接Linux服务器 package main import ( "fmt" "golang.org/x/crypto/ssh ...

  5. 『学了就忘』Linux基础命令 — 20、文件操作的相关命令

    目录 1.touch 命令 2.stat命令 3.cat命令 4.more命令 5.less命令 6.head命令 7.tail命令 1.touch 命令 touch命令用于创建空文件或修改文件时间, ...

  6. Linux之基础命令——文件操作

    ls(显示指定工作目录下的内容) -a 显示所有文件及目录 包括隐藏文件 -l 除文件名称外,还会将文件类型.权限.拥有者.文件大小等信息详细列出[可以ll简写] -r 将文件以相反次序显示(默认是a ...

  7. Linux&c 文件操作,线程进程控制,网络编程,简单知识点梳理

    一:文件操作 在linux下,一切皆文件,目录是文件,称为目录文件,内容是该目录的目录项(但是目录只有内核可以编辑,超级用户也不可以编辑),设备也是设备文件,在/dev存放的就是一些设备文件,linu ...

  8. Linux设备文件简介(转载)

    Linux 中的设备有2种类型:字符设备(无缓冲且只能顺序存取).块设备(有缓冲且可以随机存取).每个字符设备和块设备都必须有主.次设备号,主设备号相同的设 备是同类设备(使用同一个驱动程序).这些设 ...

  9. Linux C 文件操作,系统调用 -- open()、read() 和 标准I/O库 -- fopen()、fread()

    函数汇总: open().write().read().close() fopen().fwrite().fread().fclose() 一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件 ...

  10. linux文件管理 文件操作

    文件操作 pwd 命令 该命令的英文解释为print working direction(打印工作目录).输入pwd命令,Linux输出当前目录. cd 命令 用来改变所在目录 cd / 转到根目录 ...

随机推荐

  1. Android -- ContentObserver 内容观察者

    1. 实现原理图 2. 示例代码 (暂时有个问题,短信观察者 收到一条短信时 onchange方法会执行两次, 解决方法为:每次监听到变化的时候就去取最新短信的id,跟上次取的比较,如果一样的就不做处 ...

  2. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  3. docker教程目录

    为什么要用 Docker 什么是 Docker Docker 镜像 Docker容器的运用 Docker仓库 Docker如何获取镜像 CentOS 安装Docker Docker 列出镜像 Dock ...

  4. sass的多种用法

    sass的多种用法 主要归纳总结sass的常见用法,作为个人笔记使用,部分知识点并不仔细讲解.具体可参考文档:sass官网 一.嵌套 .svg{ position: absolute; left: 0 ...

  5. js数组与字符串的相互转换

    一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b,c; a = new Array(a,b,c,d,e); b = a.join('-'); //a-b-c-d ...

  6. 三、nginx 编译参数

    命令 --prefix=/usr/share/nginx # nginx 帮助目录 --sbin-path=/usr/sbin/nginx # nginx 执行命令 --modules-path=/u ...

  7. 学习opencv(持续更新)

    redhat安装,报错解决方法 1 升级GCC,http://mirrors.kernel.org/gnu/gcc/ 2 更换稳定版本 #!/bin/bash yum -y install gcc g ...

  8. hduacm集训单人排位赛1002

    自适应simpson积分公式 通过二分区间递归求simpson积分 #include<map> #include<set> #include<cmath> #inc ...

  9. AtCoder Regular Contest 078D

    两边bfs,先一边找到从1到n的路径并记录下来,然后挨个标记,最后一边bfs找1能到达的点,比较一下就行了 #include<map> #include<set> #inclu ...

  10. 【IIS转】:解决IIS下localhost访问需要输入用户名和密码的问题

    [摘要]安装完IIS后,我们可能会发现浏览器输入localhost访问时提示我们输入用户名和密码,本文介绍了这种问题的解决方法,方便站长们调试自己的程序. 在我们的WIN XP系统中安装了IIS,但是 ...