Linux file lookup command find, xargs detailed

xiaoxiao2021-04-08  380

Foreword: About Find Commands

Since Find has powerful features, there are many options, most of whom are worth our time to find out. Even if the system contains the network file system (NFS), the Find command is equally valid in the file system, just you have the appropriate permissions.

When running a very resource-consuming Find command, many people tend to put it in the background, because it can spend a long time to traverse a large file system (herein refers to a file system over 30G bytes).

First, the general form of the find command is; Find pathname -Options [-print -exec -ok ...]

2, the parameters of the Find command; Pathname: The directory path found in the find command. For example, use. To represent the current directory, use / to represent the system root directory. -print: The Find command outputs the matching file to the standard output. -EXEC: The Find command executes the shell command given by the matching file. The form of the corresponding command is 'Command' {} /;, pay attention to the space between {} and /; -ok: and -exec the same, but only the shell command given in a more secure mode is performed. You will give a prompt before performing each command, allowing the user to determine if it is executed.

3, find command option

-name finds a file according to the file name. -Perm Find files in accordance with file permissions. -prune Use this option to make the find command in the currently specified directory, if you use the -Depth option at the same time, -prune will be ignored by the FIND command. -user Find files according to the owner of the file. -group Find files in accordance with the group belonging to the file. -mtime -n n Find files according to the change time of the file, - n indicates that the file change time is now N days, n indicates that the file change time is now N days. The Find command also has the -atime and -ctime option, but they are all with the -m Time option. -NOGROUP looks out that the file with a valid locally, that is, the group belonging to the file does not exist in / etc / groups. -nouser finds a file without the active home, that is, the owner of the file does not exist in / etc / passwd. -newer file1! file2 lookup change time than file File1 new but file file2 old files. -type finds a type of file, such as: B - block device file. D - directory. C - Character device file. P - pipe file. l - symbolic link file. f - normal file. -size n: [c] Find files with the file length N block, with C indicates that the file length is bytes. -Depth: When looking up a file, look for files in the current directory before looking for files in its subdirectory. -fstype: Find files in a certain type of file system, which typically can be found in configuration file / etc / fstabs that contain information about file systems in this system. -mount: Do not cross the file system Mount point when looking for files. -FOLLOW: If the Find command encounters a symbolic link file, track the file pointed to the link. -CPIO: Use the CPIO command to the match to back up these files into the tape device.

In addition, the differences below:

-Amin n Find the last N minute accessed file -AME N lookup system last N * 24 hours access file -cmin n Finding the last N minutes to change the file status file-CTIME N lookup system last N * 24-hour file-mmin n Finding the last N minutes of the final N minutes to change the file data - MTIME N lookup system The last N * 24 hours of change file data file data

4. When using EXEC or OK to execute the shell command to use Find, simply write the desired operation in a file, you can use the exec to match the Find lookup, which is convenient to allow only the -EXEC options in some operating systems. LS or LS -L commands. Most users use this option to find old files and delete them. It is recommended that before the RM command is really executed, it is best to take a look at the LS command, confirm that they are the files to be deleted.

After the exec option, follow the command or script you want to execute, then 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 find that the command only outputs the relative path and file name from the current path.

For example, in order to list the matched files with the ls -l command, the ls -l command can be placed in the -exec option.

# 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 180 2003-02-25 ./conf.d/readme

In the above example, the find command matches all normal files in the current directory and listed them using the ls -l command in the -exec option. Find changes in the / logs directory in the 5th file and delete them:

$ FIND LOGS --TYPE F-MTIME 5 - EXEC RM {} /;

Remember: Before you use any way in any way, you should first look at the corresponding file, be careful! When using, such as MV or RM commands, the security mode of the -EXEC option can be used. It will prompt you before performing each matching file.

In the following example, the find command looks for all file names in the current directory to end the file, change the time over 5 or more, and delete them, just give a prompt before deleting.

$ FIND. -NAME "* .conf" -mtime 5 -ok rm {} /; ? n

Press the Y key to delete the file, press the n to not delete it.

Any form of commands can be used in the -exec option.

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

# Find / etc -name "passwd *" -exec grep "sam" {} /; sam: x: 501: 501 :: / usr / sam: / bin / bash

Second, the example of find command; 1. Find all files under the current user's home directory: Both methods can be used

$ FIND $ HOME -PRINT $ FIND ~ -PRINT

2, let the files in the current directory have read, write permissions, and the user of the files and other users with read permissions;

$ FIND. -TYPE F -PERM 644 - EXEC LS -L {} /;

3, in order to find ordinary files with all the files in the system, they list their full path; $ find / -type f -size 0 -EXEC ls -l {} /;

4. Find the / var / logs directory to change the normal file before the 7th, and ask them before deleting; $ find / var / logs -type f -mtime 7 -ok rm {} /; 5, in order to find All files belonging to the root group; $ find. -Group root -exec ls -l {} /; - rw-r - r - 1 root root 595 October 31 01:09./fie1

6, the Find command will delete the access time in the directory since the 7th, containing the digital suffixed admin.log file. This command only checks three digits, so the suffix of the corresponding file should not exceed 999. First build a few additional admin.log * files to use the following command

$ FIND. -NAME "admin.log [0-9] [0-9] [0-9]" -time -7 -okrm {} /; ? n ? n ? n ? n

7. In order to find all directories in the current file system and sort; $ find. -Type d | sort

8, in order to find all RMT tape devices in the system; $ FIND / DEV / RMT -PRINT

Third, Xargsxargs - Build and Execute Command Lines from Standard Input

When using the -EXEC option to use the FIND command, the Find command passes all the matched files to Exec execution. However, some systems have restrictions on the length of the command that can be passed to the exec, so that after the Find command runs a few minutes, an overflow error occurs. Error information is usually "parameter column too long" or "parameter column overflow". This is where the xargs command is located, especially with the Find command.

The Find command passes the matching file to the xargs command, and the Xargs command takes only a part of the files instead of all, not all like the -exec option. This will deal with the first part of the file, then the next batch, and continue.

In some systems, using the -exec option initiates a corresponding process for each matching file, not all of the matched files as a parameter; this will have too many processes in some cases, the system Performance drops, so the efficiency is not high;

And using Xargs commands only one process. In addition, when using the Xargs command, all parameters are acquired, or batch, and the number of acquisition parameters will be determined based on the options for this command and the corresponding adjustable parameters in the system kernel.

Take a look at how the xargs command is used with a FIND command and give some examples.

The following example looks for every normal file in the system, then use the Xargs command to test which type of files they belong to.

#find. -type f -print | xargs file./.kde/autostart/autorun.desktop: UTF-8 Unicode English Text./.kde/autostart/.directory: ISO-8859 Text / ...

Find the memory information dump file (Core Dump) throughout the system, then save the results in the /tmp/core.log file:

$ FIND / -NAME "Core" -print | xargs echo "> /TMP/core.log

This is too slow, I changed to the current directory.

#find. -name "file *" -print | xargs echo "> /temp/core.log#file6 Find all users with read, write, and execution permission in the current directory And recover corresponding write rights:

# ls -ldrwxrwxrwx 2 Sam ADM 4096 October 30 20:14 File6-RWXRWXRWX 2 Sam ADM 0 October 31 01:01 http3.conf-rwxrwxrwx 2 Sam ADM 0 October 31 01:01 httpd.conf # Find. -perm -7 -Print | xargs chmod ow # ls -ldrwxrwxr-x 2 Sam ADM 4096 October 30 20:14 File6-RWXRWXR-X 2 Sam ADM 0 October 31 01:01 http3.conf-rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd.conf

Use the grep command to search for Hostname in all normal files:

. # Find -type f -print | xargs grep "hostname" ./ httpd1.conf: # different IP addresses or hostnames and have them handled by the./httpd1.conf:# VirtualHost: If you want to maintain multiple domains / hostnameson Your

Search for Hostnames in all normal files in the current directory with a grep command:

# Find. -Name / * -type f -print | xargs grep "hostnames" ./ httpd1.conf: # Different IP addresss or hostnames and have theme handled by the./httpd1.conf:# VirtualHost: if you want to maintain Multiple Domains / Hostnameson Your

Note that in the above example, / is used to cancel the special meaning in the SHELL in the Find command.

The Find command works with EXEC and Xargs to enable the user to perform almost all commands to match the file.

Fourth, the parameter of the FIND command is the example of some common parameters. It is useful to check it. You can use some of the parameters in the previous post, you can also use the man or view the forum. Post Office Manual with Find

1. Using the Name Option File Name option is the most commonly used option for the Find command, or use this option separately or with other options.

You can use some filename mode to match the file, remember to cause the file name mode by quotation marks.

No matter what the current path is, if you want to find the file name in your own root directory $ HOME, use it ~ As the 'PathName' parameter, the wavy ~ represents your $ home directory.

$ FIND ~ -NAME "* .txt" -print

Want to find all the '* .txt' files in the current directory and subdirectory, you can use:

$ FIND. -NAME "* .txt" -print

The desired current directory and subdirectory lookup file names start with a capital letter, you can use:

$ FIND. -NAME "[a-z] *" -print

Want to find file names starting with HOST in the / etc directory, you can use:

$ FIND / etc -name "Host *" -print wants to find files in the $ home directory, you can use:

$ FIND ~ -NAME "*" -print or find. -print

To make the system high load operation, start looking for all files from the root directory.

$ FIND / -NAME "*" -print

If you want to find the file name at the current directory to start with two lowercase letters, follow the two numbers, the final is the .txt file, the following command can return the file named ax37.txt:

$ FIND. -NAME "[A-Z] [A-Z] [0--9] [0--9] .txt" -print

2. Use the Perm option to use the -perm option according to the file permission mode to find the file by file permission mode. It is best to use an octal permission representation.

If you find a file with a file permission bit 755 in the current directory, that is, the file owner can read, write, execute, other users can read, execute the file, which can be used:

$ FIND. -PERM 755 -PRINT

There is also an expression method: add a horizontal bar in front of the octal number -, indicating that it matches, such as -007 is equivalent to 777, -006 equivalent to 666

# ls -l-rwxrwr-x 2 Sam ADM 0 October 31 01:01 http3.conf-rw-rw-rw- 1 Sam ADM 34890 October 31 00:57 httpd1.conf-rwxrwxr-x 2 Sam ADM 0 10 Month 31 01:01 httpd.confdrw-rw-rw- 2 Gem Group 4096 October 26 19:48 SAM-RW-RW-RW- 1 root root 2792 October 31 20:19 Temp # Find. -Perm 006 # find . -perm -006./sam./httpd1.conf./temp

-Perm mode: File license is just in line with MODE

-perm mode: File license part complies with MODE

-Perm -Mode: File License Full compliance with MODE

3, Ignore a directory if you want to ignore a directory when you find a file, because you know the file you want to find in that directory, you can use the -prune option to indicate the directory you need 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 files in / apps directory, you don't want to find in / apps / bin directory, you can use:

$ FIND / Apps -Path "/ Apps / Bin" -prune -o -print

4. How to avoid a file directory when using the find lookup file

For example, find all files that are not within Dir1 subdirectory in the / usr / sam directory.

Find / usr / sam -path "/ usr / sam / dir1" -prune -o -print

Find [-path ..] [expression] The expression in the path list is the expression

-path "/ usr / sam" -prune -o -print is -path "/ usr / sam" -A -PRune -O-print's short-write expression, -A and -O are short-circuited And shell's && || If the -path "/ usr / sam" is true, the value -Prune, -prune returns true, and the logical expression is true; otherwise no value -Prune, and logical expressions Fake. If the -path "/ usr / sam" -a -prune is a fake, the value -Print, -print returns true, or logical expressions are true; otherwise, no value -Print, or logical expression is true. This expression combination can be written as a pseudo code

if -path "/ usr / sam" Then -PruneElse -Print

Avoid multiple folders

Find / usr / sam / (-path / usr / sam / dir1 -path / usr / sam / file1 /) -prune -o -print

Parentheses indicate the binding of the expression.

/ Recommend the reference, that is, indicating that the shell is not a special interpretation, and leaves the find command to explain its meaning.

Find a certain confirmation file, -name and other options are added after -O

#find / usr / sam / (- path / usr / sam / dir1 -path / usr / sam / file1 /) -prune -print "TEMP" -Print

5, use user and Nouser options

Press the file owner to find files, such as finding the file owner for SAM in the $ HOME directory, you can use:

$ FIND ~-USER SAM -PRINT

Find file owner for UUCP in / etc directory:

$ FIND / ETC-IUSER uucp -print

In order to find the file that the owner has been deleted, you can use the -nouser option. This will find files that are not valid accounts in the / etc / passwd file. When using the -nouser option, you don't have to give the username; the Find command can do your work with your work.

For example, I hope 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 user group belonging to the file is also the same option. In order to find files belonging to the GEM user group in / apps directory, you can use:

$ find / apps -group gem -print

To find all the files that are not valid for the user group, you can use the NOGROUP option. The following Find command finds such a file from the root of the file system.

$ FIND / -NOGROUP-PRINT

7, find the file according to the change time or access time

If you want to find a file according to the change time, you can use the Mtime, Atime, or CTIME option. If the system suddenly has no space, it is very likely that the length of a file is growing rapidly during this period. At this time, you can use the Mtime option to find such a file.

Use the minus-token-to limit the files within the distance from Today's N days, and use the plus number to qualify the file before the time N days.

I hope to find changes within 5 days in the root directory, you can use:

$ FIND / -MTIME -5 -PRINT

In order to find the file before the / var / adm directory, you can use:

$ FIND / VAR / ADM -MTIME 3 -PRINT8, find all files than a file new or old file, if you want to find changes to all files than a file, you can use the -neewer option. Its general form is:

NEWEST_FILE_NAME! Oldest_file_name

among them,! Is logical non-symbol.

Finding changes to the file SAM new but file TEMP old file:

Example: There are two files

-rw-r - r - 1 SAM ADM 0 October 31 01:07 FIEL-RW-RW-RW- 1 SAM ADM 34890 October 31 00:57 httpd1.conf-rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd.confdrw-rw-rw- 2 Gem Group 4096 October 26 19:48 SAM-RW-RW-RW- 1 root root 2792 October 31 20:19 Temp # find -newer httpd1.conf! - NEWER TEMP -LS1077669 0 -RWXRWXR-X 2 SAM ADM 0 October 31 01:01 ./httpd.conf1077671 4 -RW-RW-RW- 1 root root 2792 October 31 20:19 ./TEMP1077673 0 -RW-R --R - 1 Sam ADM 0 October 31 01:07 ./fiel

Find a new file than the Temp file:

$ FIND. -NEWER TEMP -PRINT

9, use the TYPE option

Find all directories in the / etc directory, you can use:

$ FIND / ETC -TYPE D -PRINT

Look for all types of files other than the directory in the current directory, you can use:

$ FIND.! --TYPE D -PRINT

Find all symbolic link files in / etc directory, you can use

$ FIND / ETC -TYPE L -PRINT

10, use the size option

You can find a file according to the length of the file, which can be measured with block (block) or bytes. The expression form of the byte metering file is n c; only the number is represented by the number of block meter files.

When looking for files in accordance with the file length, this generally uses this byte represented file length, and the size of the file system is viewed because the block is used to measure more easily. Find a file with a file length greater than 1 m bytes in the current directory:

$ FIND. -SIZE 1000000C -PRINT

Find file lengths in the / home / apache directory to be 100 bytes of files:

$ FIND / HOME / APACHE -SIZE 100C -PRINT

Look for files with more than 10 pieces in the current directory (a piece equal to 512 bytes):

$ FIND. -SIZE 10 -PRINT

11. Use the depth option When you use the find command, you may want to match all files first, then look for in the subdirectory. Use the depth option to make the find command. One reason for this is that when you back up the file system on the tape with the Find command, you want to back up all files first, follow the files in the subdirectory.

In the following example, the Find command starts from the root directory of the file system to find a file called Con.File.

It will first match all files and then enter the subdirectory.

$ FIND / -NAME "Con.File" -Depth -print

12, use the mount option

Find files (do not enter other file systems) in the current file system, you can use the mount option of the Find command. Start searching from the current directory in this file system in the file name with XC:

$ FIND. -NAME "* .xc" -Mount -Print

Transfer from: http://www.4aiur.com/html/program/shell/2006/0516/209.html

转载请注明原文地址:https://www.9cbs.com/read-132658.html

New Post(0)