Unix Find & Xargs detailed

Source: Internet
Author: User
Tags uppercase letter valid

Xargs:

 

Find-type f-name "*.UNL" | Xargs awk-f "|" ' {if ($1== "221") {print $ > ' log/' FILENAME} '

 

When you use the-e x E c option of the f i n d command to process a matching file, the Find command passes all matching files to E x e C for execution. However, some systems have a limit on the length of the command that can be passed to E x e C , so that an overflow error occurs after the f i n d command runs for a few minutes. the error message is usually "parameter column too Long" or "parameter column overflow". This is where the use of the X a RG s command is, especially with the f i n d command.

The F i n d command passes the matched file to the X a RG S command, and the x a RG s command gets only a subset of the files at a time instead of all, unlike the-e x E c option. This allows it to first process a portion of the file that was first fetched, then the next batch, and so on. In some systems, the use of the-e x E c option initiates a corresponding process for processing each matching file, not all of the matching files are executed once as parameters, so that in some cases there are too many processes and degraded system performance, so the efficiency is not high;

with the X a RG s command, there is only one process . In addition, when using the X a RG s command, whether to get all the parameters at once, or to get the parameters in batches, and the number of each fetch parameter will be determined according to the command's options and the corresponding tunable parameters in the system kernel.

Take a look at how the X a RG s command is used with the f i n d command, and gives some examples.
The following example finds every normal file in the system, and then uses the X a RG S command to test what type of file they belong to
#find. -type F-print | Xargs file
./.kde/autostart/autorun.desktop:utf-8 Unicode 中文版 Text
./.kde/autostart/.directory:iso-8859 text\
......
Find the Memory information dump file (core dump) throughout the system and save the results to the/tmp/core.log file:
$ find/-name "core"-print | Xargs echo "" >/tmp/core.log
The above execution is too slow, I changed to find in the current directory
#find. -name "file*"-print | Xargs echo "" >/temp/core.log
# Cat/temp/core.log
./file6
In the current directory, look for files with read, write, and execute permissions for all users, and reclaim the appropriate write permissions:
# ls-l
DRWXRWXRWX 2 Sam adm 4096 October 20:14 File6
-RWXRWXRWX 2 Sam Adm 0 October 01:01 http3.conf
-RWXRWXRWX 2 Sam Adm 0 October 01:01 httpd.conf
# Find. -perm-7-print | Xargs chmod o-w
# ls-l
Drwxrwxr-x 2 Sam adm 4096 October 20:14 File6
-rwxrwxr-x 2 Sam Adm 0 October 01:01 http3.conf
-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.conf
Use the G r e P command to search for the word hostname in all common files:
# Find. -type F-print | Xargs grep "hostname"
./httpd1.conf:# different IP addresses or hostnames and has them handled by the
./httpd1.conf:# virtualhost:if you want to maintain multiple domains/hostnames
On your
Use the G r e P command to search all normal files in the current directory for the word hostnames:
# Find. -name \*-type F-print | Xargs grep "Hostnames"
./httpd1.conf:# different IP addresses or hostnames and has them handled by the
./httpd1.conf:# virtualhost:if you want to maintain multiple domains/hostnames
On your
Note that in the above example, \ is used to cancel the special meaning of * in the S H e L in the f i n d command.

The 3.F i n d command with the use of E x e C and x a RG s can enable the user to execute almost all commands against the matched file.
Here are some examples of find some common parameters, useful to the time to check on the line, like the previous posts above, have used some of the parameters, you can also use man or view other posts in the Forum have find command manual
A. Using the name option
The file name option is the most commonly used option for the f i n d command, either by using this option alone or with other options.
You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks.
Regardless of the current path, if you want to find the file name in your own root directory $ H O M e matches *. T x t file, using ~ as ' p a t h n a m e parameter, tilde ~ represents your $ h O m e directory.
$ find ~-name "*.txt"-print
You want to find all the ' * ' in the current directory and subdirectories. T x t ' file, can be used:
$ find. -name "*.txt"-print
You want the current directory and subdirectories to find file names that begin with an uppercase letter, which can be used:
$ find. -name "[a-z]*"-print
To find files with a file name beginning with H o s T in the/e T C directory, you can use:
$ find/etc-name "host*"-print
To find files in the $ H O M directory, you can use:
$ find ~-name "*"-print or find. -print
To get the system running at a high load, start looking for all the files from the root directory.
$ find/-name "*"-print
If you want to find the file name in the current directory starting with two lowercase letters followed by two numbers, and finally. T x t file, the following command will be able to return the name of a X 3 7. T x t files:
$find. -name "[A-z][a-z][0--9][0--9].txt"-print
B. Using the PERM option
Follow the file permission mode with the-P e r m option.
To find files by file permission mode. It is best to use the octal permission notation.
For example, in the current directory to find files with permission bit 7 5 5 files, that is, the file owner can read, write, execute, other users can read, execute files, can be used:
$ find. -perm 755-print
There is also a way of expression: in front of the octal number to add a bar-, the expression is matched, such as 007 is equivalent to 777,-006 equivalent to 666
# ls-l
-rwxrwxr-x 2 Sam Adm 0 October 01:01 http3.conf
-rw-rw-rw-1 Sam Adm 34890 October 00:57 httpd1.conf
-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.conf
Drw-rw-rw-2 Gem Group 4096 October 19:48 Sam
-rw-rw-rw-1 root root 2792 October 20:19 Temp
# Find. -perm 006
# Find. -perm-006
./sam
./httpd1.conf
./temp
-perm mode: File license exactly matches mode
-perm +mode: File License section complies with mode
-perm-mode: File license fully complies with mode
C. Ignore a directory
If you want to ignore a directory when you are looking for a file, because you know that the directory does not have the file you are looking for, you can use the-P r u n e option to indicate the directories that need to be ignored. Be careful when using the-P r u n e option, because if you are using the-D e p t option at the same time, then the-P r u n e option will be ignored by the f i n d command.
If you want to find the file in the/a P s directory, but do not want to find it in the/a p p s/b i n directory, you can use:
$ find/apps-path "/apps/bin"-prune-o-print
Add:
How to avoid a file directory when finding files using Find
For example, to find all files in the/usr/sam directory that are not within the DIR1 subdirectory
Find/usr/sam-path "/usr/sam/dir1"-prune-o-print
Find [-path ...] [Expression] In the path list is followed by expressions
-path "/usr/sam"-prune-o-print is a shorthand expression for-path "/usr/sam"-a-prune-o-print, evaluated sequentially,-A and-O are short-circuit evaluated, with Shell's && and | | Similarly, if-path "/usr/sam" is true, then the evaluation-prune,-prune returns True, and the logical expression is true; otherwise, the-prune is not evaluated, and the logical expression is false. If the-path "/usr/sam"-a-prune is false, the-print is evaluated, the-print returns True, or the logical expression is true, otherwise no value-print, or the logical expression is true.
This combination of expressions can be written in pseudo-code
If-path "/usr/sam" Then
-prune
Else
-print
Avoid multiple folders
Find/usr/sam \ (-path/usr/sam/dir1-o-path/usr/sam/file1 \)-prune-o-print
Parentheses represent the combination of expressions.
\ denotes a reference, which instructs the shell not to give a special explanation of the characters that follow, leaving the Find command to explain its meaning.
Find a certain file,-name and other options after-O
#find/usr/sam \ (-path/usr/sam/dir1-o-path/usr/sam/file1 \)-prune-o-name "Temp"-print


4. Use the user and Nouser options
Find files by file owner, such as in the H O M e directory to find files belonging to the main Sam file, you can use:
$ find ~-user Sam-print
Find files in the/E T C directory where the file belongs to U-C p:
$ find/etc-user Uucp-print
In order to find files that are already deleted from the master account, you can use the-N o u S e r option. This will enable you to find files that have no valid accounts in the/E t c/p a s W d file. When using the-N o u S e r option, you do not have to give the user name; the f i n d command can do the work for you.
For example, to find all such files in the/h o m e directory, you can use:
$ find/home-nouser-print
5. Use Group and Nogroup options
Just like the U S e r and N o u S e r options, the f i n d command has the same option for the user group to which the file belongs, in order to find files belonging to the Gem User group in the/a P s directory, you can use:
$ find/apps-group Gem-print
To find all files that do not have a valid owning user group, you can use the N o g r o u p option. The following f i n d command looks for such a file from the root directory of the file system
$ find/-nogroup-print
6, according to change time or access time, etc. find files
If you want to find the file by changing the time, you can use the M t i m e,atime or CTime option. If the system suddenly does not have free space, it is possible that the length of a file grows rapidly during this period, you can use the M t i m e option to find such a file.
Use a minus sign-to limit the time to change the file within the current n days, and use the Plus + to limit the change time before the current n days of the file.
To find files that change within 5th of the system root directory, you can use:
$ find/-mtime-5-print
In order to find files that change time before 3rd in the/V a r/a d m directory, you can use:
$ find/var/adm-mtime +3-print
7. Find newer or older files than a file
You can use the-n e w e r option if you want to find all files that have changed to a new but older file than the other. The general form of it is:
Newest_file_name! Oldest_file_name
Among them,. is a logical non-symbol.
Look for files that change time than file Sam new but older than file temp:
Example: There are two of files
-rw-r--r--1 Sam Adm 0 October 01:07 Fiel
-rw-rw-rw-1 Sam Adm 34890 October 00:57 httpd1.conf
-rwxrwxr-x 2 Sam Adm 0 October 01:01 httpd.conf
Drw-rw-rw-2 Gem Group 4096 October 19:48 Sam
-rw-rw-rw-1 root root 2792 October 20:19 Temp
# Find-newer Httpd1.conf! -newer Temp-ls
1077669 0-rwxrwxr-x 2 Sam Adm 0 October to 01:01./httpd.conf
1077671 4-rw-rw-rw-1 root root 2792 October to 20:19./temp
1077673 0-rw-r--r--1 Sam Adm 0 October to 01:07./fiel
Look for a new file that changed in time than the temp file:
$ find. -newer Temp-print
8. Use the Type option
Find all the directories in the/e T-C directory, which can be used:
$ find/etc-type D-print
To find all types of files except directories in the current directory, you can use:
$ find. ! -type D-print
Find all of the symbolic link files in the/E T C directory, which can be used:
$ find/etc-type L-print
9. Use the SIZE option
Files can be found by file length, and the length of the file referred to here can be measured either in blocks (b l o C k) or in bytes. The length of the measured file in bytes is expressed as n C, and the length of the block measurement file is only represented by a number.
When looking up files by file length, this is generally the size of the file in bytes, and it is easier to convert by using blocks to measure the file system.
In the current directory, look for files with a file length greater than 1 m bytes:
$ find. -size +1000000c-print
Look for files with a file length of exactly 1 0 0 bytes in the/h o m e/a p a c h e directory:
$ find/home/apache-size 100c-print
In the current directory, look for files with a length of more than 1 0 blocks (a piece equals 5 1 2 bytes):
$ find. -size +10-print
10. Using the depth option
When using the f i n d command, you might want to match all the files before looking in subdirectories. Use the D e p t h option to enable the F i n d command to do so. One reason for this is that when you back up the file system to tape using the f i n command, you want to back up all the files first, and then back up the files in the subdirectories.
In the following example, the f i n d command starts at the root of the file system and looks for a name C O N. F I L E's file.
It will first match all the files and then go to the subdirectory to find them.
$ find/-name "CON. FILE "-depth-print
11. Using the Mount option
To find files in the current file system (without entering other file systems), you can use the M o u n T option of the f i n d command.
From the current directory, look for files that are located in the file system with the file name ending in x C:
$ find. -name "*. XC "-mount-print

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.