find只查当前目录 和 -exec和xargs区别
1.find默认查找当前目录和子目录,通过maxdepth限制只查当前目录:
find . -maxdepth 1 -type f -name "*.php"
2.
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 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 区别
find . -name "*.txt" -exec rm {} \;find . -name "*.txt" | xargs rm {} -exec 1 ...
- 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法
几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...
- linux中exec和xargs命令的区别和优劣分析
find的exec及ok命令 exec命令的格式为: exec command {} \; exec后面跟着的是操作命令,然后跟着{}表示每一个参数,然后空格,然后"\;".{}之 ...
- MySQL 笔记整理(19) --为什么我只查一行的语句,也执行这么慢?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 19) --为什么我只查一行的语句,也执行这么慢? 需要说明一下,如果M ...
- [Linux] 终端设置只显示当前目录及终端美化
1.只显示当前目录 vim ~/.bashrc 找到位置: if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($d ...
- find命令之exec和xargs
exec: find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了. exec解释: -exec 参数后面跟的是c ...
- find命令应用exec及xargs
find最普通的用法是查找文件,然后要对文件进行处理就需要用到参数-exec. 先看下manpage中介绍: -exec command ; Execute command; status is re ...
- -exec 与 xargs 的区别
实地在shell里执行下如下命令就知道区别了: $ find -type f -exec echo {} \; 很明显,exec是对每个找到的文件执行一次命令.从这里可以看出exec的缺点是每处理一个 ...
随机推荐
- 检查c# 内存泄漏
c# 内存泄漏检查心得 系统环境 windows 7 x64 检查工具:ANTS Memory Profiler 7 或者 .NET Memory Profiler 4.0 开发的软件为winform ...
- php基础04:字符串函数
<?php //1.strlen(),strlen() 函数返回字符串的长度,以字符计. echo strlen("hello world"); echo "< ...
- Response.Redirect("x.aspx);跳转后session为null的解决方法
通常我们做登陆的时候都是登录成功后为管理员保存一些信息,一般都会写类似下面的代码 if(登录成功) { Session["xx"] = "user"; Resp ...
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- [MetaHook] Load TGA texture to OpenGL
This function load a *.tga texture file and convert to OpenGL pixel format, uncompress only. #pragma ...
- 信息安全系统设计基础实验四:外设驱动程序设计 20135211李行之&20135216刘蔚然
北京电子科技学院(BESTI) 实 验 报 告 封面 课程:信息安全系统设计基础 班级:1352 ...
- Jenkins进阶系列之——14配置Jenkins用户和权限
今天给大家说说使用Jenkins专有用户数据库的配置,和一些常用的权限配置. 配置用户注册 在已运行的Jenkins主页中,点击左侧的系统管理—>Configure Global Securit ...
- IOS判断app在appstore是否有可用的更新
iTunes可以提供app的版本信息,主要通过appid获取,如 http://itunes.apple.com/lookup?id=946449501,使用时只需要到iTunes查找自己的appid ...
- 第四十二课:基于CSS的动画引擎
由于低版本浏览器不支持css3 animation,因此我们需要根据浏览器来选择不同的动画引擎.如果浏览器支持css3 animation,那么就使用此动画引擎,如果不支持,就使用javascript ...
- AngularJS - 路由入门
我们有很多方法让一个视图随着用户的操作进行变化. 但是,只是单单一个视图就要满足所有的需求会让代码变得非常复杂. 也许我们可以使用ng-include来引用各种模板,但这只限于部分场景. 于是我们可以 ...