Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Linux find and xargs     - Linux supports serial output method (Linux)

- Windows and Ubuntu dual system, repair of two ways UEFI boot (Linux)

- Vi editor Introduction (Linux)

- Android SDK Manager can not update the domestic solutions (Linux)

- After installing minimize RHEL / CentOS 7 need to do some things (Linux)

- Spring use Cache (Programming)

- To use slay kill user processes (Linux)

- Transfer files and permissions from Windows to Linux system by Samba (Linux)

- Nginx start, stop, smooth start, smooth upgrade (Server)

- Build the first ASP.NET 5 Web project in Mac OS X Yosemite 10.10.3 (Server)

- CentOS 6.5 Telnet SecureCRT use management tools (Linux)

- Linux environment variable configuration and save places (Linux)

- Use IP address spoofing Intrusion Prevention Firewall (Linux)

- Python kills corresponding process according to pid (Programming)

- Ubuntu 15.04 and CentOS 7 to deploy .NET, Mono and DNX (Server)

- Linux startup and logon security settings (Linux)

- JavaScript prototype and prototype chain and project combat (Programming)

- Eclipse distributed management using GitHub project development (Linux)

- Docker in the development and practice of IFTTT (Server)

- Talk about Java EE Learning (Programming)

  Linux find and xargs
  Add Date : 2018-11-21      
  Find work orders

    Work find command is as follows: traversing down along the file hierarchy, matching qualified file and perform the appropriate action.

find command extremely powerful because it allows you to press the file name, file type, users can even find the file timestamp. Use the find command, you can not only find with any combination of these properties files, it can also perform file operations found.

[Note: find the version used herein, the GNU version, so some of the details may be different from other versions of find. ]

The basic format

    Before we begin, let's look at the basic structure of the find command:

find start_directory test options criteria_to_match action_to_perform_on_results

start_directory: path to find the command you are looking for, you can specify multiple paths.

# Find. -print
# Find / / etc / usr "* .c"
    The above command, find in the current directory ( "." Indicates) find any documents.

-print print out matching specified file name (path). When using -print, '\ n' as a delimiters for the file.

-print0 specified with '\ 0' as the delimiter to print the name of each file matching. When the filename contains whitespace or line breaks, it is very useful.

    If the user does not have the appropriate permissions, use the find time, will generate a lot of error messages: Permission denied

We can redirect the error message to ensure clear results:

# Find / etc -name "* .sh" 2> / dev / null
According to the file name or regular expression matching the search

    Options -name parameter specifies the file name must match string. We can use a wildcard as a parameter. note,

find there is an option -iname (ignoring case), and the effect of this option -name similar, but the matching filenames when ignoring case.

Note: You need to escape the wildcard to make sure it passes the command to find and are not to be construed shell.

# Find. -name "* .sh"
 If you want to match one of the plurality of conditions may be employed OR two conditions:

# Find. \ (-name "* .sh" -o -name "* .txt" \) -print
    Option -path parameter can use wildcard characters to match the file path or file -name always given file name to match. -path path to the file is as a whole match.

    Options -regex parameters and -path similar, but -regex is based on regular expressions to match the file path, This is a match on the whole path, not a search. Similar, -iregex ignoring case for regular expression matching.

Search-based directory depth: maxdepth, mindepth, ignore a directory

    When using the find command will traverse all subdirectories. We can use some depth parameter to limit the find command to traverse depth. -maxdepth and -mindepth is such parameters.

    In most cases, we only need to search in the current directory, no need to continue to look down. In this case, we use the depth argument to restrict find command down to find the depth. If only allowed to find looks in the current directory, the depth can be set to 1; when needed down the levels, the depth can be set to 2; otherwise once analogy.

    We can specify the maximum depth by -maxdepth parameters. Similarly, we can also specify a minimum depth using -mindepth parameter setting minimum depth.

    -maxdepth and -mindepth should appear as the first option. If the option as an after, it may affect the efficiency of the find, because it had to make some unnecessary tests.

     When using the find command will traverse all subdirectories. Use find to find the time, and sometimes want to ignore certain directories, may be used to filter -prune parameters, but must pay attention to ignore the path parameters must be followed by the search path after Otherwise, this parameter can not work.

The following are specified search / home directory of all files carryf /, but ignores the / home / carryf / astetc path:

find / home / carryf -path "/ home / carryf / astetc" -prune -o -type f -print

If you search by file name was:

find / home / carryf -path "/ home / carryf / astetc" -prune -o -type f -name "cdr _ *. conf" -print

What happens if you want to ignore more than two path processing?

find / home / carryf \ (-path "/ home / carryf / astetc" -o -path "/ home / carryf / etc" \) -prune -o -type f -print

find / home / carryf \ (-path "/ home / carryf / astetc" -o -path "/ home / carryf / etc" \) -prune -o -type f -name "* .conf" -print

Note \ (and \) front and back spaces.

Based on the file type search

    UNIX-like systems are deemed to file everything. Files of different types, such as ordinary files, directories, character devices, block devices, symbolic links, sockets, and FIFO and the like.

    -type option to search for a file type filter. The following is the type you can specify the type:

Only list all the directories:
$ Find. -type D -print
List only ordinary file:
$ Find. -type F -print

Time-based search

    UNIX / Linux file system, each file there are three timestamps (timestamp), as shown below.

Access time (-atime, access time): Last user access time of the file.

Modified (-mtime, modify time): the contents of the file was last modified.

Change time (-ctime, change time): file metadata (metadata, such as file permissions or ownership) changes in the last time.

-atime, -mtime, -ctime can find as an option, they are given as an integer value, unit of measurement is "Heaven." These integer values usually with the - or +; - represents less than, greater than + represents.

Print out in the last seven days of all files being accessed:
$ Find. -type F -atime -7 -print
Print out in just seven days before being visited all files:
$ Find. -type F -atime 7 -print
Print out more than seven days to be visited all the files:
$ Find. -type F -atime +7 -print
-atime, -mtime, -ctime which unit of measurement is "Heaven." There are other time parameters based on a "minute" as a unit of measure. they are, respectively:

-amin (access time)

-mmin (Modified)

-cmin (change time)

Based on the file size of the search

    -size options can be filtered based on file size, so you can search based on file size:

$ Find. -type F -size + 2k
# File larger than 2KB
$ Find. -type F -size -2k
# Less than 2KB files
$ Find. -type F -size 2k
# Equal to 2KB files Note: You may be rounded up, -size 7k, may find the 6.8k file
In addition to k, you can also use other units File size:

b block (512 bytes)

c bytes

w word (2 bytes)

k kilobytes

M megabytes

G Gigabytes


Search based on file permissions and ownership

    find command

-perm -mode

              All of the permission bits mode are set for the file. Symbolic modes are

              accepted in this form, and this is usually the way in which would want to use

              them. You must specify 'u', 'g' or 'o' if you use a symbolic mode. See the

              EXAMPLES section for some illustrative examples.

-perm / mode

              Any of the permission bits mode are set for the file. Symbolic modes are

              accepted in this form. You must specify 'u', 'g' or 'o' if you use a sym-

              bolic mode. See the EXAMPLES section for some illustrative examples. If no

              permission bits in mode are set, this test matches any file (the idea here is

              to be consistent with the behaviour of -perm -000).

-perm + mode

              Deprecated, old way of searching for files with any of the permission bits in

              mode set. You should use -perm / mode instead. Trying to use the '+' syntax

              with symbolic modes will yield surprising results. For example, '+ u + x' is a

              valid symbolic mode (equivalent to + u, + x, i.e. 0111) and will therefore not

              be evaluated as -perm + mode but instead as the exact mode specifier -perm

              mode and so it matches files with exact permissions 0111 instead of files

              with any execute bit set. If you found this paragraph confusing, you're not

              alone -. just use -perm / mode This form of the -perm test is deprecated

              because the POSIX specification requires the interpretation of a leading '+'

              as being part of a symbolic mode, and so we switched to using '/' instead.

find -perm detailed description of the parameters:
    MODE: exact match, the file permissions "exactly equal" MODE, and you must specify the permission bits exactly
    -MODE: File permissions to fully contain this MODE if eligible, "must all contain" all the appropriate permissions bits can be specified
    / MODE: Match any one condition is met, "optionally contain a specified permission bits" are in line with the document.

# Ls -l
total 8
-rw-r - r-- 1 root root 77 Mar 27 23:14 a.txt.
------- R--. 1 root root 0 Mar 27 23:15 b.txt
-rwxr-xr-x. 1 root root 32 Mar 27 23:14 first.sh
MODE: must exactly match exactly, only permissions to 644 will find
# Find. -perm 644
-MODE: Corresponding permission bits must be fully contained. rw-r - r--, you must include several permission bits to qualify
# Find. -perm -644
/ MODE: the corresponding permission bits, as long as any one can match MODE. rw-r - r--
# R -------- match
# -w ------- Match
# --- R ----- match
# ------ R-- match
# Rwxr-xr-x matches
# Find. -perm / 644
Run or find combined action

    find command can make use of the option -exec combination with other shell commands. Corresponding syntax is -exec command {} \;

Note that {\} and; spaces between. {} File represents find find, \ an escape ";" indicates the end of the Bank's instructions. Because ";" In the bash environment it is of special significance, and therefore need to escape.
    -ok: -exec and the role of the same, but in a more secure mode to execute shell commands given by the parameter before executing each command will prompt you to allow users to determine whether to execute.

Examples are as follows:

. # Find -type f -exec ls -l {} \;
    In this command, {} is a special string, used in conjunction with -exec option. For each matching file, {} will be replaced with the file name.

-exec combine multiple commands

    We can not directly use the -exec option after multiple commands. It only takes a single command, but we can play a little trick. Multiple commands in a shell script, and then use this script in the -exec.

Combining multiple search conditions

     At the same time we can specify multiple options, such as: find -type f -size 1M; we specify the -type, -size option. By default, if we specify a number of options, the default option is between the "and" condition combination.


              Force precedence. Since parentheses are special to the shell, you will nor-

              mally need to quote them. Many of the examples in this manual page use back-

              slashes for this purpose: '\ (... \)' instead of '(...)'.


expr1 expr2

              Two expressions in a row are taken to be joined with an implied "and"; expr2

              It is not evaluated if expr1 is false.


find supports three logical combination:

-a -and

-o -or

! -not

find xargs command based Friends

    Some commands can only be in the form of a command line argument to accept data, and can not accept data through stdin stream. In this case, we can not use the pipeline to which provided data only through the command-line parameters can provide.

    xargs is a very useful command, it is good at converting the input data into a standard command-line arguments. xargs can handle stdin and convert it to a specific command command line parameters.

    xargs command should immediately after the pipeline operator. It is the standard input stream as the primary source of data and uses stdin and by providing command line parameters to execute other commands. xargs command received from stdin data reformatting (xargs default is whitespace characters (spaces, TAB, newline) to split the record), then as a parameter to other commands.

For example: command | xargs

-d option: Specifies the argument separator
# Echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
-n option: Specifies the maximum number of parameters in each row n, we can divide any text from stdin into multiple lines.
Each row n arguments. Each parameter is a "" (space) separated string. Space is the default delimiter.
# Echo "splitXsplitXsplitXsplit" | xargs -d X -n 2
split split
split split
-I Option to specify a replacement string, the string when xargs extension will be replaced.
When used with xargs -I combination, for each parameter, the command will be executed once.
Equivalent to specifying a placeholder.
# Cat args.txt | xargs -I {} ./cecho.sh -p {} -l
-I Specifies the replacement string. For each command parameter, the string {} is replaced with the parameter to be read from stdin.

Why use xargs?

    When the file using find -exec command processing option to match, find the command will be passed along to all matching files to the exec execution. However, some systems can be passed to the exec command length is limited, so that after the find command to run a few minutes, there will be an overflow error. Error message is usually "parameter list is too long" or "parameter column overflow." This is useful where the xargs command, especially for use with the find command.

    find the command to match the file transfer to the xargs command, xargs command and each get only part of the file, but not all, unlike the -exec option that. So that it can be processed to obtain the first part of the file, and then the next batch, and so continue.

    In some systems, using the -exec option to process each file matching to initiate a corresponding process, not the entire file as an argument to match the first performance; so in some cases there will be too many processes, system performance degradation, so efficiency is not high; and use xargs command is only one process. In addition, when using the xargs command, whether it is a get all the parameters, or arguments made in batches, and each time get the number of parameters will be determined based on the options for the command and the corresponding kernel tunable parameters.

Take a look at how the xargs command used in conjunction with the find command, and gives some examples.

find -type f -print |. xargs file search system each ordinary file, then use the xargs command to test what kind of file they belong

find -type f -print |. xargs grep "hostname" Search hostname of the word in all common file using the grep command

find ./ -mtime +3 -print | xargs rm -f -r all things deleted three days before (find -ctime +3 -exec rm -rf {} \;.)

find ./ -size 0 | xargs rm -f & delete files with zero size files

find and xargs command in conjunction with exec allows users to perform almost all of the command files to match.

It looks pretty good, though. . .

[Root @ skype tmp] # ls
file 1.txt file 2.txt
. [Root @ skype tmp] # find -name '* .txt' | xargs rm
rm: can not remove `./file ': No such file or directory
rm: can not remove `1.txt ': No such file or directory
rm: can not remove `./file ': No such file or directory
rm: can not remove `2.txt ': No such file or directory
j_0065.gif tell me way?

    The reason is simple, xargs default is whitespace characters (spaces, TAB, newline) to split the recording, the file name is interpreted into two ./file 1.txt record ./file and 1.txt, unfortunately rm these two documents are not found. in order to solve such problems, the smart people came up with a way to let find out after printing a file name and then outputs a NULL character ( '\ 0') instead of a newline, then tell xargs also use the NULL character as a record delimiter. this is the origin of -print0 find and xargs -0 it.

If you save?

. [Root @ skype tmp] # find -name '* .txt' -print0 | xargs -0 rm


              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 cor-

              rectly interpreted by programs that process the find output. This option

              corresponds to the -0 option of xargs.
- Use py2exe to generate exe files Python script (Programming)
- Python 2.7.9 Installation on Linux CentOS 6.6 (Linux)
- Linux installed PCRE (Linux)
- How Oracle implements random reads from specific combinations (Database)
- How to display a dialog Bash Shell script (Programming)
- SSH keys using login and password to log prohibited practice (Linux)
- Spark SQL job of a lifetime (Server)
- Linux based serial programming (Programming)
- Ora-00439: feature not enabled: managed standby (Database)
- JavaScript Advanced Programming notes event capture and event bubbling (Programming)
- The principle Httpclient4.4 (execution request) (Programming)
- Service Discovery: Zookeeper vs etcd vs Consul (Server)
- grep command Series: grep command to search for multiple words (Linux)
- To_teach you three strategies to prevent the LAN IP address theft (Linux)
- PXE + Kickstart automatically install CentOS 6.5 (Linux)
- Guide: Trickle restrict application bandwidth usage (Linux)
- Object-oriented language Java some of the basic features (Programming)
- To install minimize RHEL / CentOS 7 (Linux)
- HttpClient4 usage upgrade from HttpClient3 (Programming)
- Running into the site-wide HTTPS (Server)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.