Unix-find command details

Source: Internet
Author: User
Tags uppercase letter
Document directory
  • 1.1.1 find command form
  • 1.1.2 find Command Parameters
  • 1.1.3 example of the find command

Because find has powerful functions, there are many options, and most of them are worth the time to understand. Even if the system contains a Network File System (NFS), the find command is equally valid in the file system, and you only have the corresponding permissions.

When running a find command that consumes a lot of resources, many people tend to put it in the background for execution, because it may take a long time to traverse a large file system (this is a file system with more than 30 GB bytes ).

1.1.1 find command form

Find pathname-options [-print-Exec-OK]

1.1.2 find Command Parameters

Find Command Options:

Parameters

Description

Pathname

Find command to find the directory path. For example, use "." To represent the current directory, and use "/" to represent the root directory of the system.

-Print

The find command outputs matching files to the standard output.

-Exec

The find command executes the s h e l command given by this parameter on the matching file. The corresponding command is in the form of 'command' {}\;. Note the space between {} And.

-OK

The role of-exec is the same as that of-exec, but the shell command given by this parameter is executed in a safer mode. A prompt is displayed before each command is executed, let the user determine whether to execute.

-Name

Search for files by file name.

-Perm

Find the file according to the file permission.

-Prune

This option can make the find command not to be searched in the specified directory. If the-depth option is used at the same time,-prune will be ignored by the find command.

-User

Search for files by file owner.

-Group

Find the file according to the file group.

-Mtime

-Mtime-N + n

Find the file according to the file change time.-N indicates that the file change time is less than N days from now, and + N indicates that the file change time is earlier than N days from now. The find command also has the-atime and-ctime options, but they both have the-mtime options.

-Nogroup

Find the file with no valid group, that is, the group to which the file belongs does not exist in/etc/groups.

-Nouser

Find a file without a valid owner, that is, the owner of the file does not exist in/etc/passwd.

-Newer

-Newer file1! File2

Find the file whose modification time is newer than file1 but earlier than file2.

-Type

Search for a type of file.

B-block device files.

D-directory.

C-character device file.

P-MPs queue file.

L-Symbolic Link file.

F-common file.

-Size N [c]

Search for files with a length of N blocks. If a file contains C, the file length is measured in bytes.

-Depth

When searching for a file, first find the file in the current directory and then find it in its subdirectory.

-Fstype

Search for files in a certain type of file system. These file system types can be found in the configuration file/etc/fstab, this configuration file contains information about the file system in the system.

-Mount

When searching for a file, it does not span the mount point of the file system.

-Follow

If the find command encounters a symbolic link file, it will trace the file to which the link points.

-Cpio

Use the cpio command to back up the files to the tape device.

 

1.1.3 example of the find command 1.1.3.1 name Option

The file name option is the most common option for the find command. You can either use this option independently or use it with other options.

You can use a certain file name pattern to match the file. Remember to use quotation marks to cause the file name pattern.

No matter what the current path is, if you want to find a file with a file name *. txt in your root directory $ home, use ~ As the pathname parameter, the Tilde ~ Represents your $ home directory.

$ Find ~ -Name "*. txt"-print

 

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

$ Find.-Name "*. txt"-print

 

To search for a file whose name starts with an uppercase letter in the current directory and subdirectory, you can use:

$ Find.-Name "[A-Z] *"-print

 

To search for a file whose file name starts with host in the/etc directory, use:

$ Find/etc-name "Host *"-print

 

To search for files in the $ home directory, you can use:

$ Find ~ -Name "*"-print or find.-Print

 

To make the system run at a high load, search for all the files from the root directory.

$ Find/-name "*"-print

 

If you want to find the file name starting with two lower-case letters in the current directory, followed by two numbers, and finally a file named *. txt, the following command can return the file named ax37.txt:

$ Find.-Name "commana-z?a-z=%0--9%%0--9%.txt"-print

 

1.1.3.2 perm options

Use the-Perm option in File Permission mode.

Find the file in File Permission mode. It is best to use the octal permission notation.

 

For example, in the current directory, find a file with a permission of 755, that is, the file owner can read, write, and execute the file. Other users can read and execute the file. You can use:

$ Find.-Perm 755-print

 

There is also a way of expression: Add a horizontal bar before the octal number to indicate that all matches. For example,-007 is equivalent to 777, and-006 is equivalent to 666.

# Ls-l

-Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 http3.conf

-RW-1 Sam ADM 34890 00:57 httpd1.conf

-Rwxrwxr-x 2 Sam ADM 0 October 31 01:01 httpd. conf

Drw-RW-2 gem group 4096 October 26 19:48 Sam

-RW-1 Root 2792 October 31 20:19 temp

 

# Find.-Perm 006

# Find.-Perm-006

./SAM

./Httpd1.conf

./Temp

 

1.1.3.3 prune Option

If you want to ignore a directory when searching for a file because you know that there is no file in the directory, you can use the-prune option to specify the directory to be ignored. Be careful when using the-prune option, because if you use the-depth option at the same time, the-prune option will be ignored by the find command.

 

If you want to search for files in the/apps directory, but do not want to search for files in the/apps/bin directory, you can use:

$ Find/apps-path "/apps/bin"-prune-o-print

 

-In the perm option, there is also an expression for my parsing: Add a horizontal bar before the octal number to indicate that all matches. For example,-007 is equivalent to 777, and-006 is equivalent to 666.

 

How can I avoid a file directory when searching for files using find?

 

For example, you need to find all files not in the dir1 subdirectory under the/usr/SAM directory.

$ Find/usr/SAM-path "/usr/SAM/dir1"-prune-o-print

 

Find [path...] [expression] After the path list is an expression

-Path "/usr/SAM"-prune-o-print is-path "/usr/SAM"-a-prune-o-print short expressions are evaluated in order, -both A and-O are short-circuit values, which are similar to shell's & |. If-path "/usr/SAM" is true, evaluate-Prune,-prune returns true, and the logical expression is true; otherwise, do not seek a value-Prune, and the logical expression is false. If-path "/usr/SAM"-a-Prune is false, evaluate-print,-print
Returns true, or the logical expression is true. Otherwise, no value-print is required, or the logical expression is true.

The special expression combination can be written

If-path "/usr/SAM" then

-Prune

Else

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.