Shell command to find and execute a file

Source: Internet
Author: User
Tags svn

Reason

Often we need to find some kind of file and deal with it. For example, locate the. svn folder, and then delete it. If you do not use the shell, you can choose to manually delete, provided that there are few such files, but the SVN information file is many, can not be deleted manually or by command.

The shell is introduced to solve this problem.

Method 1 Find–exec Options

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 F Ile name by evaluating the given  expres‐
       sion  from  left  to  right, according to the rules of precedence (see section
       OPERATORS), unt Il the outcome is known (the left hand side is  false  for  and
       ope Rations, true for or), at which point find moves on to the next file name.

If you are using the find in a environment where security is important (for exam‐
ple If you is using it to search directories that is 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 could find it a more useful source of information.

Valid commands:

Find./testtime/-name. svn-exec rm-rf {}\;;

-exec command;
Execute command;  True if 0 status is returned. All following arguments
To find is taken to being 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 was 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 sections for EXAMPLES of the use of the-exec option.
The specified command is a run once for each matched file. The command
is executed in the starting directory. There is 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 was built by appending each
Selected file name at the end; The total number of invocations of the
Command'll is much less than the number of matched files. The com‐
Mand Line was built in much the same-a-line, that Xargs-builds its command
Lines.  Only one instance of ' {} ' is allowed within the command. The
command is executed in the starting directory.

Method 2 Find Xargs combination

Xargs meaning, the command is constructed from the Xargs parameter and executed, and the input parameters of the execution command are standard inputs.

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 B e 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 Initia L-arguments followed  by  Items
       read from standard input.  Blank lines on the standard input is 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's better to use the-0 op‐
       tion, Which prevents such problems.   when using the This option you'll 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  Exa mple,  the
      -print0 option does.

If any invocation of the command exits with a status of 255, Xargs'll stop
Immediately without reading any further input. An error message was issued on
StdErr when this happens.

Valid commands:

Find-name. SVN | Xargs RM–RF

Method 3 Find–delete Options

Two more efficient than the previous one.

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 command to find and execute a file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.