Home >Operation and Maintenance >Linux Operation and Maintenance >Why does find in Linux not search the specified subdirectory?

Why does find in Linux not search the specified subdirectory?

WBOY
WBOYOriginal
2022-07-12 15:30:354689browse

In Linux, you can use the "-prune" parameter of the find command to make find not search the specified subdirectory when searching for files. The find command is used to find files in the specified directory. When the parameter is set to "-prune " can be filtered, as long as the path parameter to be ignored must immediately follow the search path, otherwise the parameter will not work. The syntax is "find search path-path subdirectory path not to be found-prune...".

Why does find in Linux not search the specified subdirectory?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to find in Linux find without searching the specified subdirectory

When Linux find is searching, sometimes it is necessary to ignore certain directories and not search. You can use the -prune parameter to filter, but it must It should be noted that the path parameter to be ignored must immediately follow the searched path, otherwise this parameter will not work.

The example is as follows:

The following is a specified search for all files in the /home/carryf directory, but the path to /home/carryf/astetc will be ignored:

find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -print

If you search by file name, it will be:

find /home/carryf -path "/home/carryf/astetc" -prune -o -type f -name "cdr_*.conf" -print

What to do if you want to ignore more than two paths?

find /home/carryf −path"/home/carryf/astetc"−o−path"/home/carryf/etc" -prune -o -type f  -print
find /home/carryf −path"/home/carryf/astetc"−o−path"/home/carryf/etc" -prune -o -type f  -name "cdr_*.conf" -print

Note that there are spaces before and after.

To find the content of a file, the following statement can solve the problem of directories with spaces:

find ./ -name "MySQL*" -print0  |xargs -0 grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

If the directory does not have spaces, it can be executed in the following form:

find ./ -name "mysql*"  |xargs  grep "SELECT lead_id FROM vicidial_list where vendor_lead_code"

Extended knowledge

The Linux find command is used to find files in the specified directory. Any string preceding the parameter will be treated as the name of the directory to be searched. If you use this command without setting any parameters, the find command will search for subdirectories and files in the current directory. And all found subdirectories and files will be displayed.

Syntax

find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

Parameter description:

find determines path and expression according to the following rules. The first one on the command line - (), the part before ! is path. , followed by expression. If path is an empty string, the current path is used. If expression is an empty string, -print is used as the default expression. There are as many as twenty or thirty options that can be used in

expression. Only the most commonly used ones are introduced here.

-mount, -xdev: Only check files in the same file system as the specified directory, avoid listing files in other file systems

-amin n: In the past n minutes Read

-anewer file: A file that was read later than file file

-atime n: A file that was read in the past n days

-cmin n: Modified in the past n minutes

-cnewer file: File newer than file file

-ctime n: File created in the past n days

-mtime n: Files modified in the past n days

-empty: Empty files -gid n or -group name: gid is n or group name is name

- ipath p, -path p: The file whose path name matches p, ipath will ignore the case

-name name, -iname name: The file whose file name matches name. iname will ignore case

-size n: The file size is n units, b represents a block of 512 bytes, c represents the number of characters, k represents kilo bytes, and w is two bytes.

-type c: The file type is c.

Recommended learning: Linux video tutorial

The above is the detailed content of Why does find in Linux not search the specified subdirectory?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn