Find Common precautions

usenameoptions

The filename option is the most commonly used option for the find command, either alone or in combination with other options. Some file name pattern can be used to match files. Remember to enclose the file name pattern in quotes. If you want to find *.log files in your root directory, $HOME, regardless of the current path, use ~ as the ‘pathname’ parameter, with tilde ~ representing the current user’s HOME directory.

To find the home directory of the currently logged in user.logfile

> find ~ -name "*.log"
Copy the code

Find files in the current directory that begin with a capital letter

> find . -name "[A-Z]*"
Copy the code

To find the/etcDirectory tosysOpening file

> find /etc -name "sys*"
Copy the code

Query information in the current directory that starts with an uppercase letter and contains 3 digits.pngPictures of the

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

With perm options

Use the -perm option to search for files by file permission mode. It is best to use octal permission notation. For example, you can search for files whose permission bit is 755 in the current directory. That is, files whose owner can read, write, and execute files, and files that other users can read and execute files can be used

> find . -perm 755 
Copy the code

Find-perm-mode: the binary 1 of mode must match in the file’s permission bit. For example, if mode=644, the binary 1 must match in the file’s permission bit. The binary 1 of mode must match in the file’s permission bit. Zero doesn’t matter. For example, if the file being searched has permission to convert to binary 111, 111, 111 then this is matched for example, and if it’s 100, 100, 100 then it won’t match. So what this ‘-‘ does boils down to is matching files with more mode permissions.

Ignore a directory

If you want to ignore a directory while looking for files because you know it doesn’t have the file you’re looking for, use the -prune option to indicate the directory to ignore. 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 do not want to find files in the./bin directory, you can use

> find . -path "./bin" -prune -o -print
Copy the code

Avoid multiple folders

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

Parentheses represent combinations of expressions. \ indicates a reference, which instructs the shell to leave the following characters uninterpreted and leave the find command to interpret their meaning.

To find a certain file, options such as -name are added after -o

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

user nousergroup nogroup

Find the owner in the current directoryrootThe file

> find . -user root 
Copy the code

Find the deleted files that belong to the current directory

> find . -nouser
Copy the code

Find the owning group in the current directory isrootThe file

> find . -group root 
Copy the code

Search for files that have been deleted from the owning group of the current directory

> find . -nogroup
Copy the code

Find files by time

Find files that were changed within 7 days

> find . -mtime -7
Copy the code

Find files that were changed before 7 days ago

> find . mtime +7
Copy the code

Find a file that is newer or older than a file

Find change time ratio filesa.logNew but better than the fileb.logThe old file

> find . -newer a.log ! -newer b.log
Copy the code

Find changes in time thana.logFile New file

> find . -newer a.log
Copy the code

typeoptions

in/etcFind all directories under directory

> find /etc -type d
Copy the code

in/etcDirectory to find all symlink files

> find /etc -type l
Copy the code

sizeparameter

You can find a file by its length, which can be measured in blocks or bytes. The expression for measuring file length in bytes is N c. The length of a file in blocks can be expressed in numbers only. This type of file length is generally used when looking up files by file length, and when looking at the size of the file system, because it is easier to convert using blocks.

Find a file in the current directory whose length is greater than100MByte file

> find . -size +100M
Copy the code

Using depth

When using the find command, you might want to match all files before looking in subdirectories. Use the depth option to make the find command do this. One reason for this is that when backing up a file system to tape using the find command, you want to back up all the files first and then the files in the subdirectory.

The child content is output, then the upper directory content is output, all the way to the top level

> find test -depth  ! -empty
test/test1/test2/test3
test/test1/test2
test/test1
test
Copy the code

The top level directory is printed first, and then the contents of the subdirectories below are printed until the lowest level:

> find test   ! -empty
test
test/test1
test/test1/test2
test/test1/test2/test3
Copy the code

Mount options

To find a file on the current file system (without entering another file system), use the mount option of the find command.

Start from the current directory to find the name of the file located in this file system.txtClosing document

> find . -mount -name "*.txt"  -print
Copy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station

Linux Common Commands quick Guide PDF download