find.-name"[A-Z]*&"/> find.-name"[A-Z]*&">

Home  >  Article  >  System Tutorial  >  Options and parameters available for the find command in Linux

Options and parameters available for the find command in Linux

WBOY
WBOYforward
2024-01-03 10:55:19982browse

Use name option:

The file name option is the most commonly used option for the find command. This option is either used alone or together with other options. You can use a filename pattern to match files, remember to enclose the filename pattern in quotes. No matter what the current path is, if you want to find a file with a file name matching *.log in your root directory $HOME, use ~ as the 'pathname' parameter, and the tilde ~ represents the current user's home directory.

Find the .log file in the home directory of the currently logged in user:

> find ~ -name "*.log"

Find files starting with an uppercase letter in the current directory:

> find . -name "[A-Z]*"

Find files starting with sys in the /etc directory:

> find /etc -name "sys*"

Query the picture .png in the current directory that starts with a 3-digit number starting with a capital letter

> find . -name "[A-Z]*[0-9][0-9][0-9].png"

Use perm option:

Use the -perm option according to the file permission mode and search for files according to the file permission mode. It is best to use octal notation for permissions. If you want to find a file with the file permission bit 755 in the current directory, that is, the file owner can read, write, and execute, and other users can read and execute, you can use


> find . -perm 755 

find -perm -mode , indicates that the 1 converted into binary in mode must match the file permission bit. For example, if mode=644, then converted into binary is 110 100 100, and the value of the file being searched is The permission bit can also be converted into a binary number. The 1 part of the two bits must match exactly, and the 0 part does not matter. For example, if the permission of the file being searched is 111 111 111 converted into a binary number, then this will be matched, but if it is 100 100 100, then it will not be matched. So the function of this '-' boils down to matching files with more sufficient permissions than mode (I can't find any words to describe it)

Ignore a directory:

If you want to ignore a directory when searching for files because you know that the file you are looking for does not exist in that directory, you can use the -prune option to indicate the directory that needs to be ignored. Be careful when using the -prune option, because if you also use the -depth option, the -prune option will be ignored by the find command. If you want to find files in the current directory but not in the ./bin directory, you can use


> find . -path "./bin" -prune -o -print

Avoid multiple folders:

> find . \( -path "./bin" -o -path "./doc" \) -prune -o -print

Parentheses indicate the combination of expressions. \ represents a reference, which instructs the shell not to make special interpretations of the following characters, but to leave the find command to interpret their meaning.

To search for a certain file, add -name and other options after -o:

> find . \( -path "./bin" -o -path "./doc" \) -prune -o-name "*.txt" -print

<span style="background-color: initial;">user nouser</span> and <span style="background-color: initial;">group nogroup</span>

Search the current directory for files whose owner is root

> find . -user root 

Find deleted files in the current directory:

> find . -nouser

Search the current directory for files whose group is root:

> find . -group root 

Find files deleted by the group to which the current directory belongs:

> find . -nogroup

Find files by time

Find files changed within 7 days:

> find . -mtime -7

Find files that were changed before 7 days:

> find . mtime +7

Find files that are newer or older than a certain file

Find files whose change time is newer than file a.log but older than file b.log:

> find . -newer a.log ! -newer b.log

Find files whose change time is newer than the a.log file:

> find . -newer a.log

type option

Search for all directories in the /etc directory:

> find /etc -type d

Find all symbolic link files in the /etc directory:

> find /etc -type l

size parameter

You can search files according to their length. The file length referred to here can be measured in blocks or bytes. The expression form of file length measured in bytes is N c; the file length measured in blocks can only be expressed by numbers. When searching for files by file length, this file length expressed in bytes is generally used to view the size of the file system, because it is easier to convert using blocks to measure.

Search for files with a file length greater than 100M bytes in the current directory :

> find . -size +100M

Use depth option:

在使用find命令时,可能希望先匹配所有的文件,再在子目录中查找。使用depth选项就可以使find命令这样做。这样做的一个原因就是,当在使用find命令向磁带上备份文件系统时,希望首先备份所有的文件,其次再备份子目录中的文件。

先输出子内容,再输出上层目录内容,直到最顶层:

> find test -depth! -empty

test/test1/test2/test3

test/test1/test2

test/test1

test

先输出顶层目录,再输出下面的各层子目录内容,直到最低层:

> find test ! -empty

test

test/test1

test/test1/test2

test/test1/test2/test3

mount选项:

在当前的文件系统中查找文件(不进入其他文件系统),可以使用find命令的mount选项。

从当前目录开始查找位于本文件系统中文件名以.txt结尾的文件:

> find . -mount -name "*.txt"-print

The above is the detailed content of Options and parameters available for the find command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete