每天一个linux命令(文件操作):【转载】find命令之xargs
在使用 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的更多相关文章
- Unix/Linux常用文件操作
Unix/Linux常用文件操作 秘籍:man命令是Unix/Linux中最常用的命令,因为命令行命令过多,我相信每个人都会经常忘记某些命令的用法,man命令就可以显示一个命令的所有选项,参数和说明, ...
- qt ui程序使用Linux的文件操作open、close (转)
原文地址:qt ui程序使用Linux的文件操作open.close 作者:kjpioo 提出这个问题是因为在qt的QWidget类型的对象中,close()函数会和QWidget::close()冲 ...
- Linux常用命令--文件操作
常用Linux命令笔记(1) 1. 创建文件/文件夹 参考博客:https://www.cnblogs.com/lclq/p/5741852.html. 使用cat命令创建新文件: 输入命令 # ca ...
- Go连接到Linux服务器进行操作-执行shell命令&&上传文件
Go连接到Linux服务器进行操作 使用密码连接Linux服务器 package main import ( "fmt" "golang.org/x/crypto/ssh ...
- 『学了就忘』Linux基础命令 — 20、文件操作的相关命令
目录 1.touch 命令 2.stat命令 3.cat命令 4.more命令 5.less命令 6.head命令 7.tail命令 1.touch 命令 touch命令用于创建空文件或修改文件时间, ...
- Linux之基础命令——文件操作
ls(显示指定工作目录下的内容) -a 显示所有文件及目录 包括隐藏文件 -l 除文件名称外,还会将文件类型.权限.拥有者.文件大小等信息详细列出[可以ll简写] -r 将文件以相反次序显示(默认是a ...
- Linux&c 文件操作,线程进程控制,网络编程,简单知识点梳理
一:文件操作 在linux下,一切皆文件,目录是文件,称为目录文件,内容是该目录的目录项(但是目录只有内核可以编辑,超级用户也不可以编辑),设备也是设备文件,在/dev存放的就是一些设备文件,linu ...
- Linux设备文件简介(转载)
Linux 中的设备有2种类型:字符设备(无缓冲且只能顺序存取).块设备(有缓冲且可以随机存取).每个字符设备和块设备都必须有主.次设备号,主设备号相同的设 备是同类设备(使用同一个驱动程序).这些设 ...
- Linux C 文件操作,系统调用 -- open()、read() 和 标准I/O库 -- fopen()、fread()
函数汇总: open().write().read().close() fopen().fwrite().fread().fclose() 一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件 ...
- linux文件管理 文件操作
文件操作 pwd 命令 该命令的英文解释为print working direction(打印工作目录).输入pwd命令,Linux输出当前目录. cd 命令 用来改变所在目录 cd / 转到根目录 ...
随机推荐
- sql server 跨数据库调用存储过程
A库存储过程: create PROCEDURE [dbo].[spAAAForTest] ( ) =null , ) =null ) AS BEGIN select N'A' AS a , N'B' ...
- python 使用getopt 获取配置参数
在工程中特别是稍微大一点的项目基本上都会用到配置,就会涉及到配置文件的读取,配置参数的读取. 常用的解析配置文件的是configParser,解析命令行参数的则为getopt. getopt的参数可以 ...
- Centos7 安装Power Shell
Centos7 安装Power Shell 1 查看版本 # cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 2 安装 # R ...
- 在win7虚拟机中装sql server---待整理
本科学数据库的时候,为了做作业,需要在自己电脑上装sql server.但是每次都装不上,总是有各种小问题通不过.最后问学长,才采用了在虚拟机里装数据库的方法,在虚拟机中可以不用担心弄乱本机系统. 为 ...
- java网络编程之图片上传
输入输出流核心代码 所有的文件传输都是靠流,其中文件复制最具代表性.输入流和输出流,从输入流中读取数据写入到输出流中. InputStream in = 输入源; OutputStream os = ...
- 使用python编写微信跳一跳的自动脚本
实现思路: 调用adb命令,截图 寻找小小人的底部中心点role(从下到上扫描,直到找到小小人相同像素的点,至于小小人像素点rgb是什么,可以使用photoshop查看) 寻找棋盘最高点top,然后寻 ...
- HDU 5694 分治+规律
http://acm.hdu.edu.cn/showproblem.php?pid=5694 此题一开始我也找到了规律,也知道是分治可是,,,想的太复杂了没写开, 我一直想的通过L,R两个参数分治,可 ...
- B-Tree和B+Tree
目前大部分数据库系统及文件系统都采用B-Tree或其变种B+Tree作为索引结构,在本文的下一节会结合存储器原理及计算机存取原理讨论为什么B-Tree和B+Tree在被如此广泛用于索引,这一节先单纯从 ...
- 006-对象—— static关键字 静态属性和方法的使用
<?php /*static()静态属性: */ //静态属性: /*class Model{ private $mysqli; static $config;//数据库连接状态 functio ...
- LeetCode OJ:Jump Game II(跳跃游戏2)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...