查找文件并执行的shell命令
来由
经常我们需要找到某类文件, 并对进行处理。 例如找到.svn文件夹, 然后删除掉。 如果不使用shell,你可以选择手动删除, 前提是没有几个此类文件, 但是svn信息文件很多, 不能采用手动删除, 或者逐个命令删除。
由此引入shell来解决此问题。
方法1 find –exec 选项
NAME
find - search for files in a directory hierarchySYNOPSIS
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]DESCRIPTION
This manual page documents the GNU version of find. GNU find searches the
directory tree rooted at each given file name by evaluating the given expres‐
sion from left to right, according to the rules of precedence (see section
OPERATORS), until the outcome is known (the left hand side is false for and
operations, true for or), at which point find moves on to the next file name.If you are using find in an environment where security is important (for exam‐
ple if you are using it to search directories that are writable by other
users), you should read the "Security Considerations" chapter of the findutils
documentation, which is called Finding Files and comes with findutils. That
document also includes a lot more detail and discussion than this manual page,
so you may find it a more useful source of information.
有效命令:
find ./testtime/ -name .svn -exec rm -rf {}\;;
-exec command ;
Execute command; true if 0 status is returned. All following arguments
to find are taken to be arguments to the command until an argument con‐
sisting of `;' is encountered. The string `{}' is replaced by the cur‐
rent file name being processed everywhere it occurs in the arguments to
the command, not just in arguments where it is alone, as in some ver‐
sions of find. Both of these constructions might need to be escaped
(with a `\') or quoted to protect them from expansion by the shell.
See the EXAMPLES section for examples of the use of the -exec option.
The specified command is run once for each matched file. The command
is executed in the starting directory. There are unavoidable security
problems surrounding use of the -exec action; you should use the
-execdir option instead.-exec command {} +
This variant of the -exec action runs the specified command on the
selected files, but the command line is built by appending each
selected file name at the end; the total number of invocations of the
command will be much less than the number of matched files. The com‐
mand line is built in much the same way that xargs builds its command
lines. Only one instance of `{}' is allowed within the command. The
command is executed in the starting directory.
方法2 find xargs组合
xargs 含义, 从xargs参数构造命令,并执行, 执行命令的入参为标准输入。
NAME
xargs - build and execute command lines from standard inputSYNOPSIS
xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d
delimiter] [--delimiter delimiter] [-I replace-str] [-i[replace-str]]
[--replace[=replace-str]] [-l[max-lines]] [-L max-lines] [--max-lines[=max-
lines]] [-n max-args] [--max-args=max-args] [-s max-chars] [--max-chars=max-
chars] [-P max-procs] [--max-procs=max-procs] [--interactive] [--verbose]
[--exit] [--no-run-if-empty] [--arg-file=file] [--show-limits] [--version]
[--help] [command [initial-arguments]]DESCRIPTION
This manual page documents the GNU version of xargs. xargs reads items from
the standard input, delimited by blanks (which can be protected with double or
single quotes or a backslash) or newlines, and executes the command (default
is /bin/echo) one or more times with any initial-arguments followed by items
read from standard input. Blank lines on the standard input are ignored.Because Unix filenames can contain blanks and newlines, this default behaviour
is often problematic; filenames containing blanks and/or newlines are incor‐
rectly processed by xargs. In these situations it is better to use the -0 op‐
tion, which prevents such problems. When using this option you will need to
ensure that the program which produces the input for xargs also uses a null
character as a separator. If that program is GNU find for example, the
-print0 option does this for you.If any invocation of the command exits with a status of 255, xargs will stop
immediately without reading any further input. An error message is issued on
stderr when this happens.
有效命令:
find -name .svn | xargs rm –rf
方法3 find –delete选项
比前面两个更有效率。
find /tmp -depth -name core -type f -delete
Find files named core in or below the directory /tmp and delete them, but more
efficiently than in the previous example (because we avoid the need to use
fork(2) and exec(2) to launch rm and we don't need the extra xargs process).
查找文件并执行的shell命令的更多相关文章
- Java利用 ganymed-ssh2-build.jar来上传文件到linux以及下载linux文件以及执行linux shell命令
package api; import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.IOExcepti ...
- Android 执行 adb shell 命令
Android 执行Adb shell 命令大多需要root权限,Android自带的Runtime. getRuntime().exec()容易出错,在网上找到了一个执行adb shell命令的类 ...
- recovery 升级过程执行自定义shell命令
有时候我们需要,在升级的过程中,执行一些shell命令,来完成我们的一些需求,利用升级过程,进行一些特殊化的操作,思路如下: 第一: 把我们需要执行的命令,写成一个test.sh脚本,然后在recov ...
- shell脚本--php执行普通shell命令
这里只演示一些普通的shell命令,一些需要root用户权限执行的命令,请参考:php以root权限执行shell命令 php执行shell命令,可以使用下面几个函数: string system ( ...
- 【Devops】【docker】【CI/CD】关于jenkins构建成功后一步,执行的shell命令详解+jenkins容器运行宿主机shell命令的实现方法
1.展示这段shell命令 +详解 #================================================================================= ...
- 统计行数、文件夹个数、文件个数的相关shell命令
极客君最近做项目,刚好遇到需要统计一些sql文件数量的问题,用到一些实用的shell命令,记录下来,以后万一还能用上呢? 如果在终端不打开文件看到一共多少行,则可以使用wc命令来实现: wc -l [ ...
- 在Jenkins中配置执行远程shell命令
1.想要 远程登录到linux服务器并执行相应的shell脚本,需要在jenkins上安装插件enkins SSH plugin 2. 安装了这个插件后,进入系统的配置管理中配置 SSH remote ...
- 在docker中执行linux shell命令
在docker中执行shell命令,需要在命令前增加sh -c,例如: docker run ubuntu sh -c 'cat /data/a.txt > b.txt' 否则,指令无法被正常解 ...
- [Python2.x] 利用commands模块执行Linux shell命令
用Python写运维脚本时,经常需要执行linux shell的命令,Python中的commands模块专门用于调用Linux shell命令,并返回状态和结果,下面是commands模块的3个主要 ...
随机推荐
- virtual box ubuntu卡在开机光标
创建虚拟机的时候选择之前保存的虚拟机盘vdi文件,打开的时候卡在光标.原来是因为虚拟机是64位的,但是新建的时候只有32位的ubuntu可以选择就选择的32位. 解决办法: 在bios设置里,打开cp ...
- 11. 求奇数分之一序列前N项和
求奇数分之一序列前N项和 #include <stdio.h> int main() { int denominator, i, n; double item, sum; while (s ...
- [ PHP+jQuery ] ajax 多级联动菜单的应用:电商网站的用户地址选择功能 ( 二 ) - 仿亚马逊下拉面板
/** jQuery version: 1.8.3 Author: 小dee Date: 2014.11.8 */ 接上一篇博客. 实现带缓存的仿亚马逊下拉面板 效果图: 图1 初始 图2 点击省份 ...
- Windows下的Objective-C集成开发环境(IDE)
Windows下的Objective-C集成开发环境(IDE) 分类: Objective-C2012-04-20 21:54 26631人阅读 评论(42) 收藏 举报 windowside编译器c ...
- Python实用工具包Scrapy安装教程
对于想用每个想用Python开发网络爬虫的开发者来说,Scrapy无疑是一个极好的开源工具.今天安装之后觉得Scrapy的安装确实不易啊.所以在此博文一篇,往后来着少走弯路. 废话不多说了,如果 ...
- MTU-TCP/IP协议栈-linux kernel-TCP丢包重传-UDP高性能-AI-
http://view.inews.qq.com/a/20161025A0766200窄带时代的QQQQ是窄带时代极具代表性的产品,在那个网络传输效率比较低的年代,大家还记得Google的首页吗?Go ...
- jdk1.7
http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-windows-i586.exe?AuthParam=1450748157_ ...
- 实例讲述PHP面向对象的特性;;;php中const与define的使用区别
php中const与define的使用区别 1.const:类成员变量定义,一旦定义且不能改变其值. define:定义全局常量,在任何地方都可以访问.2.define:不能在类中定义,而const可 ...
- asr,tts,vsr
http://max.book118.com/html/2014/0814/9432056.shtm ASR技术的基础主要是信号处理和概率模型. 信号处理技术 语音信号处理 谱分析 基于时间的 ...
- CDH介绍
本文引用自:Cloudera 系列2:CDH介绍http://www.aboutyun.com/thread-18379-1-1.html(出处: about云开发) CDH提供: 灵活性-存储任何类 ...