Linux Find---xargs and find----exec in conjunction with

Source: Internet
Author: User
Tags uppercase letter

Example: Delete/home/raven, including all files named Abc.txt in subdirectories:

Find/home/raven-name Abc.txt | Xargs RM-RF

If Xargs is not used, then:

Find/home/raven-name abc.txt-exec RM-FV {};

Preface: About the Find command

Because find has powerful features, it has a lot of options, most of which are worth taking the time to look at. Even if the system contains a network file system (NFS), the Find command works equally well in the file system, and you only have the appropriate permissions.

When running a very resource-intensive find command, many people tend to put it in the background because it can take a long time to traverse a large file system (this refers to a file system with more than 30G bytes).

One, find command format

1. The general form of the Find command is;

Find Pathname-options [-print-exec-ok ...]

2, the parameters of the Find command;

The directory path that the Pathname:find command looks for. For example, use. To represent the current directory, and/to represent the system root directory.
The-print:find command outputs the matched file to standard output.
The-exec:find command executes the shell command given by the parameter to the matching file. The corresponding command is in the form of ' command ' {};, note the space between {} and;
-ok: The same as-exec, except that the shell command given by the parameter is executed in a more secure mode, prompting the user to determine whether to execute before executing each command.

3. Find command Options


Find files by file name.

Follow the file permissions to find the file.

Use this option to make the Find command not look in the currently specified directory, and if you use the-depth option at the same time,-prune will be ignored by the Find command.

Locate the file according to the owner of the file.

Locate the file according to the group to which it belongs.

-mtime-n +n
The file changes time to find the file,-n means that the file change time is now less than n days, + n means that the file change time is now N days ago. The Find command also has the-atime and-ctime options, but they both and the-m time option.

Finds a file that does not have a valid owning group, that is, the group to which the file belongs does not exist in/etc/groups.

Find a file without a valid owner, that is, the owner of the file does not exist in the/etc/passwd.
-newer file1! File2

Look for changes that are newer than the file file1 but file2 older than the file.

Find a file of a certain type, such as:

B-block device files.
C-character device file.
P-Pipeline file.
L-Symbolic link file.
F-Normal file.

-size N:[c] finds files with a file length of n blocks, with C indicating the length of the file in bytes.
-depth: When looking for a file, first find the file in the current directory, and then look in its subdirectories.
-fstype: Find files located in a file system of a certain type, these file system types can usually be found in the configuration file/etc/fstab, which contains information about the file system in this system.

-mount: Does not cross the file system mount point when locating files.
-follow: If the find command encounters a symbolic link file, it tracks to the file that the link points to.
-cpio: Use the cpio command for matching files to back up these files to the tape device.
In addition, the following three differences:

-amin N
Find the last n minutes of files accessed in the system

-atime N
Find the last n*24 hour Access file in the system

-cmin N
Find files in the last n minutes of the system that have been changed file status

-ctime N
Find files that have changed file status in the last n*24 hours of the system

-mmin N
Find files that have changed file data in the last N minutes of the system

-mtime N
Find files that have changed file data in the last n*24 hours of the system

4. Use exec or OK to execute shell commands

When using find, just write the desired action in a file, you can use the exec to match the Find lookup, very convenient

In some operating systems, only the-EXEC option is allowed to execute commands such as L s or ls-l. Most users use this option to find old files and delete them. It is recommended that you take a look at the LS command before you actually execute the RM command to delete files, confirming that they are the files you want to delete.

The EXEC option is followed by the command or script that you want to execute, followed by a pair of {}, a space and one, and finally a semicolon. In order to use the EXEC option, you must use the Print option at the same time. If you verify the Find command, you will see that the command outputs only the relative path and file name from the current path.

For example, to list the matching files with the Ls-l command, you can place the Ls-l command in the-exec option of the Find command

Find. -type f-exec ls-l {};

-rw-r--r--1 root root 34928 2003-02-25./conf/httpd.conf
-rw-r--r--1 root root 12959 2003-02-25./conf/magic
-rw-r--r--1 root root 2003-02-25./conf.d/readme
In the example above, the Find command matches all the normal files in the current directory and lists them using the Ls-l command in the-exec option.
In the/logs directory, look for files that change time before 5th and delete them:

$ find Logs-type f-mtime +5-exec rm {};
Remember: Before the shell can delete files in any way, you should look at the appropriate files before you must be careful! You can use the Safe mode of the-EXEC option when using a command such as MV or RM. It will prompt you before you work on each file that is matched to it.

In the following example, the Find command finds all filenames in the current directory. Log end, change files over the 5th, and delete them, but give a hint before deleting them.

$ find. -name "*.conf"-mtime +5-ok rm {};
< RM .... /conf/httpd.conf >? N
Press the Y key to delete the file and press N to not delete it.

Any form of command can be used in the-EXEC option.

In the following example we use the grep command. The find command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then executes the grep command to see if there is a SAM user in these files.

Find/etc-name "passwd*"-exec grep "Sam" {};

Ii. examples of the Find command;

1. Find all files under the current user's home directory:

Here are two ways to use

$ find $HOME-print
$ find ~-print

2, let the current directory of the file owner has read, write permissions, and the file belongs to the group of users and other users have Read permission files;

$ find. -type f-perm 644-exec ls-l {};

3, in order to find all the files in the system file length of 0 ordinary files, and list their full path;

$ find/-type f-size 0-exec ls-l {};

4. Look for common files in the/var/logs directory that were changed before 7th, and ask them before deleting them;

$ find/var/logs-type f-mtime +7-ok rm {};

5, in order to find all the files belonging to the root group in the system;

$find. -group root-exec ls-l {};
-rw-r--r--1 root root 595 October 01:09./fie1

6. The Find command will delete the Admin.log file that contains the digital suffix since the access time in the directory was 7th.

This command checks only three digits, so the suffix of the corresponding file should not exceed 999. Build several admin.log* files before using this command

$ find. -name "admin.log[0-9][0-9][0-9]"-atime-7-ok
RM {};
< RM .... /admin.log001 >? N
< RM .... /admin.log002 >? N
< RM .... /admin.log042 >? N
< RM .... /admin.log942 >? N

7, in order to find all the directories in the current file system and sorting;

$ find. -type D | Sort

8, in order to find all the RMT tape devices in the system;

$ find/dev/rmt-print

Third, Xargs

Xargs-build and execute command lines from standard input

When a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have a limit on the length of the command that can be passed to exec so that an overflow error occurs after the Find command runs for a few minutes. The error message is usually "parameter column too Long" or "parameter column overflow". This is where the Xargs command is used, especially with the Find command.

The find command passes the matched file to the Xargs command, and the Xargs command takes only a subset of the files at a time instead of all, unlike the-exec 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-EXEC 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 will be too many processes and degraded system performance, so the efficiency is not high;

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

Take a look at how the Xargs command is used with the Find command, and give some examples.

The following example finds every normal file in the system, and then uses the Xargs 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.logcat/temp/core.log

In the current directory, look for files with read, write, and execute permissions for all users, and reclaim the appropriate write permissions:


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-wls-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 grep 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 grep command to search for the word hostnames in all normal files in the current directory:

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 example above, \ is used to cancel the special meaning of the * in the shell in the Find command.

The Find command, with exec and Xargs, allows the user to execute almost all commands against the matching file.

Iv. parameters for the Find command

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

1. Use the name option

The file name option is the most common option for the Find command, either used alone or in conjunction with other options.

You can use a file name pattern to match files, remembering to enclose the filename pattern in quotation marks.

No matter what the current path is, if you want to find the file name in your root $home that matches *.txt, use ~ as the ' pathname ' parameter, and the tilde ~ represents your $home directory.

$ find ~-name ".txt"-print
To find all the '. txt ' files in the current directory and subdirectories
, you can use:

$ 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 the file name beginning with host in the/etc directory, you can use:

$ find/etc-name "host*"-print
To find files in the $home 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 with two lowercase letters, followed by two digits, and finally the. txt file, the following command will be able to return a file named Ax37.txt:

$find. -name "[A-z][a-z][0--9][0--9].txt"-print

2. With PERM option

Follow the file permission mode with the-perm 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 file permission bit 755 file, 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


-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 006find. -perm-006

-perm mode: File license exactly matches mode

-perm +mode: File License section complies with mode

-perm-mode: File license fully complies with mode

3. Ignore a directory

If you want to ignore a directory when you're looking for a file, because you know that directory doesn't have the file you're looking for, you can use the-prune option to indicate which directories you want to ignore. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option is ignored by the Find command.

If you want to find the file under the/apps directory but do not want to find it in the/apps/bin directory, you can use:

$ find/apps-path "/apps/bin"-prune-o-print

4. How to avoid a file directory when finding files with 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-path "/usr/sam"-a-prune-o
-print shorthand expressions are 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
Avoid multiple folders

Find/usr/sam (-path/usr/sam/dir1-o-path/usr/sam/file1)-prune-o-print
Parentheses represent the combination of expressions.

Represents 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

5. Use the user and Nouser options

Find files by file owner, such as files in the $home directory where the file belongs to Sam, you can use:

$ find ~-user Sam-print
Look for files in the/etc directory that belong to the main UUCP:

$ find/etc-user Uucp-print
In order to find files that are already deleted from the master account, you can use the-nouser option. This will enable you to find files that are not valid accounts in the/etc/passwd file. When using the-nouser option, you do not have to give the user name; the Find command can do the work for you.

For example, to find all such files in the/home directory, you can use:

$ find/home-nouser-print

6. Use Group and Nogroup options

Just like the user and Nouser options, the Find command has the same options for the group of users that the file belongs to, in order to find files belonging to the Gem User group in the/apps directory, you can use:

$ find/apps-group Gem-print
To find all files that do not have a valid group of users, you can use the Nogroup option. The following find command looks for such files from the root directory of the file system

$ find/-nogroup-print

7, according to change time or access time, etc. find files

You can use the Mtime,atime or CTime option if you want to find the file by changing the time. 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 Mtime 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/var/adm directory, you can use:

$ find/var/adm-mtime +3-print
Fire Away, fire away! You shoot me off but I won ' t fall

Linux Find---xargs and find----exec in conjunction with

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