博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
查找文件并执行的shell命令
阅读量:4477 次
发布时间:2019-06-08

本文共 4524 字,大约阅读时间需要 15 分钟。

来由

经常我们需要找到某类文件, 并对进行处理。 例如找到.svn文件夹, 然后删除掉。 如果不使用shell,你可以选择手动删除, 前提是没有几个此类文件, 但是svn信息文件很多, 不能采用手动删除, 或者逐个命令删除。

由此引入shell来解决此问题。

 

方法1 find –exec 选项

NAME

       find - search for files in a directory hierarchy

SYNOPSIS

       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 input

SYNOPSIS

       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).

转载于:https://www.cnblogs.com/lightsong/p/4774664.html

你可能感兴趣的文章
NameNode 与 SecondaryNameNode 的工作机制
查看>>
Code obfuscation
查看>>
node.js系列(实例):原生node.js实现接收前台post请求提交数据
查看>>
用python实现矩阵转置
查看>>
linux 小技巧(磁盘空间搜索)
查看>>
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>
tomcat使用spring-loaded实现应用热部署
查看>>
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
连接不上sql server服务器的解决方案
查看>>
2013年终总结
查看>>
Start to study Introduction to Algorithms
查看>>
正则表达式
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
SparkStreaming入门及例子
查看>>
Web应用增加struts2支持
查看>>
java程序——凯撒加密
查看>>
Windows Store App之数据存储
查看>>