Find and xargs

Source: Internet
Author: User

Find and xargs
The common form of the find command is find pathname-options [-print-exec-OK]. Let's take a look at the command parameter: directory path searched by the pathname find command. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system. -The print find command outputs matching files to the standard output. -The exec find command executes the s h e l command given by this parameter on the matching file. The corresponding command is in the form of 'c o m-And' {}\;. Note the spaces between {} and. -OK is the same as-e x e c, except that the s h e l command given by this parameter is executed in a safer mode. Before executing each command, A prompt will be given to the user to determine whether to execute. 1.1 find Command Options

-Name

Search for files by file name.

-Perm

Find the file according to the file permission.

-Prune

This option can be used to make the f I n d command not in the specified directory. If the-d e p t h option is used at the same time, the-p r u n e option is ignored by the f I n d command.

-User

Search for files by file owner.

-Group

Find the file according to the file group.

-Mtime-n + n

Find the file according to the file change time.-n indicates that the file change time is less than n days from now, and + n indicates that the file change time is earlier than n days from now.

-Newer file1! File2

Search for files whose change time is newer than file f I l e 1 but older than file f I l e 2.

-Nogroup

Find the file with no valid group, that is, the group to which the file belongs does not exist in/e t c/g r o u p s.

-Nouser

Find a file without a valid owner, that is, the owner of the file does not exist in/e t c/p a s w d.

-Type

Find a type of file, such:

 

B-block device files.

 

D-directory.

 

C-character device file.

 

P-MPs queue file.

 

L-Symbolic Link file.

 

F-common file.

-Size n [c]

Search for files with a length of n blocks. If a file contains c, the file length is measured in bytes.

-Depth

When searching for a file, first find the file in the current directory and then find it in its subdirectory.

-Fstype

Search for files in a certain type of file system. These file system types can usually be found in the configuration file/e t c/f s t a B, this configuration file contains information about the file system in the system.

-Mount

The file search does not span the m o u n t point of the file system.

-Follow

If the fi n d command encounters a symbolic link file, it traces the file to which the link points.

-Cpio

Use the c p I o command for matching files to back up these files to the tape device.

1.1.1 mtime: list the files in the current directory within one day. find. -mtime-1-print: list the files found 3 days ago in the current directory. -mtime + 3-print 1.1.2 newer: Check the files in the current directory that are newer than orclw.mmon_00003.trc but earlier than orclw.rcbg_6676.trc. Find.-newer orcl1_mmon_1_3.trc! -Newer orcl1_rcbg_6676.trc-exec ls-l {}\; $ touch 1.txt $ touch 2.txt $ find. -newer 1.txt .. /2.txt 1.1.3 size: Check the file with the size of exactly one block $ find. -size 1-print. /1.txt: Check the file with exactly 2 bytes (one English letter occupies two bytes by default) $ find. -size 2c-print. /1.txt: find the file exceeding the block size. -size + 1-print 1.1.4 depth first searches for all files, and then searches for $ find in the subdirectory. -name "awr *"-depth-print find: warning: you have specified the-depth option after a non-option Argument-name, but options are not positional (-depth affects tests specified before it as well as those specified after it ). please specify options before other arguments .. /awrrpt_rac_45_53.html 1.1.5 run exec or OK to view common files in the current directory (excluding directories, but recursively searching all files in subdirectories) find. -type f-exec ls-l {}\; Delete common files in the log directory five days ago (subdirectories will not be deleted) find log-type f-mtime + 5-exec rm {}\; delete all common files under the current directory log and Its subdirectories (subdirectories will not be deleted) find log -Type f-exec rm-rf {}\; OK operation needs to be confirmed $ find. -type f-OK rm-rf {}\; <rm .... /B>? Y <rm.../a>? Y to view the specified content in the specified file. First, match all files named "passwd *", such as passwd, passwd. old, and passwd. bak. Then run the grep command to check whether an admin user exists in these files. Find/etc/-name "passwd *"-exec grep "admin" {}\; admin: x: 903: 503:/home/admin: /bin/bash: View error messages in ORACLE logs [oracle @ r7 trace] $ find. -name "orcl *"-exec grep "ORA-" {}\; 1.2 xarg F I n d command to pass the matching file to x a rg s command, the "x a rg s" command obtains only a part of files, not all of them at a time, unlike the "-e x e c" option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. In some systems, using the-e x e c option will initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in some cases, there may be too many processes and the system performance may degrade, so the efficiency is not high. However, the command x a rg s has only one process. In addition, when using the x a rg s command, whether to obtain all parameters at a time or obtain parameters in batches, and the number of parameters obtained each time is determined based on the options of the command and the corresponding adjustable parameters in the system kernel. The following example finds the memory information dumping file (core dump) in the entire system, and then saves the result to/tmp/core. in the log file: $ find. -name "core"-print | xargs echo "">/tmp/core. in the example of log, search for all files with read, write, and execution permissions under the/a p s/a u d I t directory, and revoke the write permissions: $ find/apps/audit-perm-7-print | xargs chmod o-w in the following example, we use the g r e p command to search for the word d e v I c e in all common files: $ find/-type f-print | xargs grep "device" in the following example, we use the g r e p command to search all common files in the current directory for the word d B O: $ find. -name * \-type f- Print | xargs grep "DBO" NOTE: In the above example, \ is used to cancel the special meaning of * in s h e l of the f I n d command.

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.