Shell Basics II: Find tips, use of Find and Xargs

Source: Internet
Author: User
Tags safe mode

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-e x E C 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 L S command to confirm that they are the files you want to delete before actually executing the r m command to delete the files.

The E x E C option is followed by the command or script that you want to execute, followed by a pair of {}, a space and a \, and finally a semicolon.

To use the E x E C option, you must use the P r i n t option at the same time. If you verify the f i n d command, you will see that the command outputs only the relative path and file name from the current path.



For example, to list the matched files with the Ls-l command, you can place the Ls-l command in the-e x E c option of the f i n d command

    1. # Find. -type f-exec ls-l {} \;

    2. -rw-r--r--1 root root 34928 2003-02-25./conf/httpd.conf

    3. -rw-r--r--1 root root 12959 2003-02-25./conf/magic

    4. -rw-r--r--1 root root 2003-02-25./conf.d/readme

Copy Code


In the above example, the f i n d command matches all normal files in the current directory and lists them in the-e x E C option using the Ls-l command.

In the/L o G S directory, look for files that change time before 5th and delete them:

    1. $ find Logs-type f-mtime +5-exec rm {} \;

Copy Code


Remember, before you delete files in any way in the S H e l L, you should look at the appropriate files first, be careful! When using commands such as M V or R m, the safe mode of the-e x E C option can be used. It will prompt you before you work on each file that is matched to it.

In the following example, the f i n d command finds all filenames in the current directory. L O g end, change the files over 5th and delete them, but give a hint before deleting them.

    1. $ find. -name "*.conf"-mtime +5-ok rm {} \;

    2. < RM .... /conf/httpd.conf >? N

Copy Code


Press the Y key to delete the file and press N to not delete it.


Any form of command can be used in the-e x E c option.

In the following example we use the G r e P command. The f i n d command first matches all files named "passwd*", such as passwd, Passwd.old, Passwd.bak, and then
Line grep command to see if there is a SAM user in these files.

    1. # find/etc-name "passwd*"-exec grep "Sam" {} \;

    2. Sam:x:501:501::/usr/sam:/bin/bash

Copy Code


Find all the files under the current user's home directory, which are available in two ways:

    1. $ find $HOME-print

    2. $ find ~-print

Copy Code



In order for files in the current directory to have read, write permissions, and files belonging to the group that the file belongs to and other users who have read permissions, you can use:

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

Copy Code



In order to find all normal files with file lengths of 0 in the system and list their full paths, you can use:

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

Copy Code



Find common files in the/var/logs directory that were changed before 7th, and ask them before deleting them:

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

Copy Code



In order to find all files belonging to the root group in the system, you can use:

    1. $find. -group root-exec ls-l {} \;

    2. -rw-r--r--1 root root 595 October 01:09./fie1

Copy Code




The following find command deletes the Admin.log file that contains the numeric suffix since the access time in the directory has been 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

    1. $ find. -name "admin.log[0-9][0-9][0-9]"-atime-7-ok

    2. RM {} \;

    3. < RM .... /admin.log001 >? N

    4. < RM .... /admin.log002 >? N

    5. < RM .... /admin.log042 >? N

    6. < RM .... /admin.log942 >? N

Copy Code



In order to find and sort all the directories in the current file system, you can use:

    1. $ find. -type D |sort

Copy Code



In order to find all the R m T tape devices in the system, you can use:

    1. $ find/dev/rmt-print

Copy Code


    1. The original book is:

    2. In order to find and sort all the directories in the current file system, you can use:

    3. $ find. -type D-loacl-mount |sort

    4. has been corrected to:

    5. $ find. -type D |sort

Copy Code

Shell Basics II: Find tips, use of Find and Xargs


Xargs


When using the-e x E c option of the f i n d command to process a matching file, the f i n d 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\

......

Copy Code



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

Copy Code


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

Copy Code



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

Copy Code



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

Copy Code



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

Copy Code


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.


Shell Basics II: Find tips, use of Find and Xargs

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.