How to Use the find command in Linux

Source: Internet
Author: User


In Linux, the common form of the find command is [html] Find pathname-options [-print-exec-OK].
The parameters are described as follows: pathname: directory path searched by the find command. Www.2cto.com-print: find command to output matching files to standard output. -Exec: find: Execute the shell Command provided by this parameter on the matching file. The subsequent command is in the form of 'coommand '{}\;, note that {} + space + \;-OK: serves the same purpose as-exec, but executes the shell command given by this parameter in a safer mode, before executing each command, a prompt is displayed, asking the user to determine whether to execute the command. 1. name option this option is used to search by file name. During the search process, you need to use quotation marks to mark the file name. That is to say, you can use the pattern string to match the file name, for example, in the current directory, to find a file whose name starts with an uppercase letter, run the following command: [html] find. /-name "[A-Z] *"-print 2, perm option www.2cto.com this option looks for files by File Permission mode and uses the-perm option. For example, if you want to find a file with the File Permission of 644 in the current directory, you can use: find. /-perm 644-print [html] find. /-perm 644-print 3. The nouser and user options are used to search for files based on the file owner. The corresponding user name can be provided in the command. For example, search for files belonging to xinyu in the current directory. [Html] find. /-user xinyu-print to find the files of Users whose accounts have been deleted, you can use the-nouser option. This option does not use user name parameters, such as www.2cto.com [html] find. /-nouser-print corresponds to the other two options used for group owner search: group and nogroup, which are the same as user and nouser. 4. The mtime option searches for files based on the file change time. When we perform some data backup, we often need to delete the backup several days ago, this option is useful when only backups are left in the last few days, or we want to delete files from a few months ago. This option can be used to search for files earlier than N days, or files earlier than N days, with the + and-signs, these two symbols to limit, needless to say, you need to know how many days the-number represents, and the + number represents how many days ago. [Html] find. /-mtime-6-print file find within 6 days. /-mtime + 6 print files 6 days ago. The newer option sometimes does not specify the number of days before or within the specified number of days, we want to find which files are newer than a specified file, or which files are older than a specified file. For example, we want to find files that are newer than "mod_hash.c. [Html] find./-newer mod_hash.c-print www.2cto.com can be used! -Newer indicates that a file is not updated. 6. The type option has many different file types in Linux, such as: d: indicates the directory. C: character device file. B: indicates the block device file. P: indicates the MPs queue file. L: indicates the symbolic connection file. And s network files. For example, if we want to find all the symbolic connection files in the current directory, we can use [html] find. /-type l-print: find all directory files [html] find. /-type d-print 7. The size option can be found at the first glance. This option is used to find the file based on the file size. However, it should be noted that this option can be used to search for files based on blocks or bytes. A block is calculated by 512 bytes. The default value is block. If you want to search by byte, you only need to add c to the end, such as [html] find. /-size + 10-print: searches for files with more than 10 blocks in the current directory. Fiind. /-size 1024c-print: search for a 1024-byte file in the current directory www.2cto.com 8. Before introducing the prune option, familiarize yourself with the usage of the other two options-path, -a or-o. If the prune option is not used, the path option can be used to specify the directory under which the prune option is located. For example, [html] find./-path 'apache'-name *. conf indicates to find the configuration file with the suffix conf in the apache directory under the current directory. -O indicates or, that is, the return result of the command on the left will be truthful. If it is a match, the subsequent Command Option calculation will be performed. For example,-path./apache-prune-o-print indicates that if the directory on the Left does not match the./apache directory,-print is called for output. -A indicates the meaning of "and", and the difference between them is as follows: www.2cto.com [html] find. -path. /apache-prune-o-print display division. /find all files out of the apache directory. -path. /apache-prune-a-print only displays. /apache directory this option is used to ignore a directory during the search process. This option is more important than the previous options. This option is just like an option that determines us, followed by-o to indicate selection. If the directory to be searched is ignored, it will not be output.
When using the prune option, use-path to specify the path to be ignored. Also, pay attention to the following issues:-path to specify the path, such as ignoring the apache directory under the current directory: -path. /apache-prune-o-print,-o can indicate whether the previous one matches. When-path is used,./apache/cannot be used, but./apache. 9. depth option this option is used to control the action of the find. After this option is added, the find command matches all the files and then searches for the files in the subdirectory. 10. mount Option this option indicates that the find command only searches for files in the current file system and does not enter other file systems. We know that in Linux, you can mount a file system to a directory point of the current file. The options www.2cto.com 11, exec, and OK can be used to perform some operations on the matched files after the find command matches some files. In this case, you can use the-exec option, for example, we want to use the ls-l command to list the matched files: [html] find. /-type l-exec ls-l {}\; pay attention to the format following this exec option, as we delete the log file one week ago. [Html] find. /logs-type f-mtime + 7-exec rm {} \; The OK option is the same as the exec option, which can be considered as the safe mode of exec. If the OK option is used, before the operation, you will be prompted whether to do so. In actual application, the exec option is still used in many cases. Note the space between {} And \; again.

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.