find -exec 与xargs 区别
find . -name "*.txt" -exec rm {} \;
find . -name "*.txt" | xargs rm {}
-exec
1.参数是一个一个传递的,传递一个参数执行一次rm
2.文件名有空格等特殊字符也能处理
-xargs
1.一次将参数传给命令,可以使用-n控制参数个数
2.处理特殊文件名需要采用如下方式:
find . -name "*.txt" print0 |xargs -0 rm {}
实验结果如下,可以清楚看到参数传递过程
- [root@andes.com ~/tmp/dir]#find . -type f |xargs -t -n 2 echo
- echo ./data.txt ./env2.txt
- ./data.txt ./env2.txt
- echo ./env.txt ./export2.txt
- ./env.txt ./export2.txt
- echo ./s.txt ./d.txt
- ./s.txt ./d.txt
- echo ./export.txt ./set.txt
- ./export.txt ./set.txt
- echo ./fuck.txt
- ./fuck.txt
- [root@andes.com ~/tmp/dir]#find . -type f -exec echo begin {} \;
- begin ./data.txt
- begin ./env2.txt
- begin ./env.txt
- begin ./export2.txt
- begin ./s.txt
- begin ./d.txt
- begin ./export.txt
- begin ./set.txt
- begin ./fuck.txt
- [root@andes.com ~/tmp/dir]#
技巧: find -print0 与 xargs -0 的结合避免文件名有特殊字符如空格,引号等无法处理:
find . -name "*.txt" print0 |xargs -0 rm {}
find
-print True; print the full file name on the standard output, followed by a newline. If you are piping the
output of find into another program and there is the faintest possibility that the files which you
are searching for might contain a newline, then you should seriously consider using the -print0
option instead of -print. See the UNUSUAL FILENAMES section for information about how unusual char-
acters in filenames are handled.
-print0
True; print the full file name on the standard output, followed by a null character (instead of the
newline character that -print uses). This allows file names that contain newlines or other types of
white space to be correctly interpreted by programs that process the find output. This option corre-
sponds to the -0 option of xargs.
xargs
-0 Input items are terminated by a null character insteadof by
whitespace, and the quotes and backslash are not special (every
character is taken literally). Disables the end of file string,
which istreated like
any other argument. Useful when input
items might contain white space, quote marks, or backslashes.
The GNU find -print0 option produces input suitable for this
mode.
find -exec 与xargs 区别的更多相关文章
- exec与xargs区别
区别描述: 两者都是对符合条件的文件执行所给的Linux 命令,而不询问用户是否需要执行该命令. -exec:{}表示命令的参数即为所找到的文件,以:表示comman命令的结束.\是转义符,因为分号在 ...
- find只查当前目录 和 -exec和xargs区别
1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录: find . -maxdepth 1 -type f -name "*.php" 2. find . ...
- linux中exec和xargs命令的区别和优劣分析
find的exec及ok命令 exec命令的格式为: exec command {} \; exec后面跟着的是操作命令,然后跟着{}表示每一个参数,然后空格,然后"\;".{}之 ...
- -exec 与 xargs 的区别
实地在shell里执行下如下命令就知道区别了: $ find -type f -exec echo {} \; 很明显,exec是对每个找到的文件执行一次命令.从这里可以看出exec的缺点是每处理一个 ...
- linux find查找并拷贝 exec xargs区别
-exec 1.参数是一个一个传递的,传递一个参数执行一次rm 2.文件名有空格等特殊字符也能处理-xargs 1.一次将参数传给命令,可以使用-n控制参数个数 2.处理特殊 ...
- linux exec和xargs的区别
-exec 1.参数是一个一个传递的,传递一个参数执行一次,效率低 2.文件名有空格等特殊字符也能处理 -xargs 1.一次将参数传给命令,可以使用-n控制参数个数 ...
- -exec和|xargs
注意xargs会被空格割裂,所以遇到带有空格的文件名就不好办了,解决方法是使用-print0 例如:删除.目录下30天之前的.png文件 -type f -name rm 或者使用-exec:删除.目 ...
- test、exec、match区别
test.exec.match的简单区别 1.test test 返回 Boolean,查找对应的字符串中是否存在模式. var str = "1a1b1c"; var reg = ...
- js中, match和exec方法的区别
1. 来源分别为: string.match(reg) 和 RegExp.exec(str): 2. 区别 > 现有 字符串s1 和 正则对象 r1. 目标: 抽出s1中的所有电话号码 ...
随机推荐
- pycharm下设置自己的模板
在File---settings---File and Code Templates---Python script 脚本里添加: #!usr/bin/env python #-*- coding:u ...
- [Canvas]碰撞球 增加小球间碰撞检测
请点此下载代码并用Chrome或是Firefox打开index.html 图例: 代码: <!DOCTYPE html> <html lang="utf-8"&g ...
- 面试小结之Elasticsearch篇
https://www.cnblogs.com/luckcs/articles/7052932.html
- 解决Android Studio提示gradle project sync failed报错的解决方法
运行的时候报错,提示:gradle project sync failed 1.打开AS,切换到project目录结构依次进入目录app->gradle->gradle-wrapper.p ...
- javascript实现金额大写转换函数
function transform(tranvalue) { try { var i = 1; var dw2 = new Array("", "万", &q ...
- 微信小程序 - 自定义弹窗组件
2019-01-06:简化了一些代码,以及增加了可用性. // 弹窗配置 dialogConfig: { // 弹窗 dialogvisible: false, options: { // 显示关闭按 ...
- MDX Step by Step 读书笔记(九) - Working with Time 处理时间
开篇介绍 这一章节主要用到的 MDX 函数: PeriodsToDate( [Level , [Member]] ) - 从指定级别的范围内,返回与指定成员同一级别,从第一个期间开始到指定成员结束的期 ...
- canvas绘制图片
canvas保存为data:image扩展功能的实现 [已知]canvas提供了toDataURL的接口,可以方便的将canvas画布转化成base64编码的image.目前支持的最好的是png格式, ...
- innosetup完整脚本
#define MyAppName "Somarto"#define MyAppVersion "1.0.0"#define MyAppPublisher &q ...
- postman发送get请求
在地址栏里输入请求url(用到拼接方式):http://127.0.0.1:8081/getuser?userid=1 选择“GET”方式, 点击“send”得到json数据如图 分类: post ...