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语言可以在栈区 or 堆区 or 全局区 存放字符串,字符串不单单是存储在全局区的. //字符串与指针 #include<stdio.h> #include<stdlib.h> ...
- C语言 共用体
//共用体 union #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #includ ...
- AE笔记
我想做一个好玩的视频,需要用到AE,写点东西锻炼语言能力,视频教程后续录制到时放链接. 一.合成 拖进项目的视频要新建合成才能加以编辑,新建合成很简单,拖拽你的视频向下到一个类似相片的图标上面就新建成 ...
- WinForm编程数据视图之DataGridView浅析
学习C#语言的朋友们肯定或多或少地接触到了WinForm编程,在C#语言的可视化IDE中(如VS.NET中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...
- Log4Net写入到数据库配置过程中的一些小问题备忘
问题1: 在公司进行log4net写入服务器配置的时候,一切正常,但是在家里的机器上,就频繁出现这个问题: SQL Server 2008 报错:已成功与服务器建立连接,但是在登录前的握手期间发生错误 ...
- [CareerCup] 9.1 Climbing Staircase 爬楼梯
9.1 A child is running up a staircase with n steps, and can hop either 1 step, 2 steps, or 3 steps a ...
- 系统级I/O 第八周11.1~11.8
第十章 系统级I/O 输入输出I/O是在主存和外部设备(如磁盘,网络和终端)之间拷贝数据的过程.输入就是从I/O设备拷贝数据到主存,而输出就是从主存拷贝数据到I/O设备. 所有语言的运行时系统都提供执 ...
- 身份证号码自动生成程序(Python)
今天收到一个小需求:需要一个自动生成身份证号码的小程序.近期用python较多,因此打算用python实现. 需求细化: 1.身份证必须能够通过身份证校验程序. 2.通过查询,发现身份证号码是有国家标 ...
- 玩转数据库之 Group by Grouping
有的时候我们要从数据库里把数据组织成树结构再展现到页面上 像下面这样 今天我们用Group 和Grouping实现它,并总结一下它俩. 先看一下概念,再用代码一点一点去理解它们,最后我会给出完整的代码 ...
- Redis入门学习(一)——安装配置
最近马上要找实习了,听学长说他们公司里用了redis来解决缓存问题,在大三结束前来学习一下. 问:Redis是什么? 答:(官方)REmote DIctionary Server(Redis)是一个K ...