Usage of the find command in shell

Source: Internet
Author: User

Key points for using the find command in shell linux the find command is an important way to find files, and other bash commands can be used to achieve powerful operation results. Let's take a look at the basic components of the find command: find pathname-option [-print-exec-OK...] class Memory attribute parameters ~ Pathname: Specify the query root path of the find command, such as '/' and. You can also specify the search path in the-path parameter. -Option: 1. File Operation:-name is enclosed by quotation marks ("). It is applicable to simple regular expressions (it should be changed to shell metacharacters, the same below ). -Type: Specifies the file type B-block device file. D-directory. C-character device file. P-MPs queue file. L-Symbolic Link file. F-common file. -Size n: Specifies the file size. If the value is greater than +, the value-Indicates smaller than the value. The common unit is c (bytes), k (KB), and M (MB). 2. User Permissions: -perm user permission, which is expressed by a commonly used three-digit number, for example, 644. If the number + indicates at least one specified access permission, -indicates that all specified access permissions must be included.-user/-nouser: query by file owner/query by file owner-group/-nogroup: query by file owner/search no valid rent file. 3. Time options: -atime/-amin n: files recently accessed (accessed). The former indicates that the Unit is Day (24 h), and the latter indicates minute (minute ); if + is before the number, it indicates that n segments are before, and-indicates the last n segments.. -Ctime/-cmin n: recently changed file-mtime/-mmin n: recently modified file-newer file! Otherfile searches for new files whose change time is later than file, plus non (!) Search for files older than otherfile (note that the logical symbols include! (Not),-a (and), and-o (or) are short-circuit values in the command. Simply put, if the following parameters do not affect logical judgment, this parameter is not followed by a value. In the find command, logical symbols are usually added before the option.) 4. File Path:-follow: a symbolic link file is encountered, the file-mount point pointed to by the link does not span the mount point of the file system, that is, it is queried in the current file system and does not enter other file systems (such as the mounted windows System) -path indicates the file path, which can be queried in the path and can meet the requirements of simple regular expressions (metacharacters ). For example, find '. /var/www 'index.html 'file in the subdirectory: $ find. -path '. /var/www * '-name 'index.html'-print. /var/www/learn/study_smarty/templates/index. tpl. /var/www/learn/study_smarty/docs/index. php. /var/www/learn/index. php. /var/www/index.html (the result shows that the parameters in the-path option must be fuzzy query conditions, in my understanding, the file name found by the-path option is actually a string with a path, while the-path option is a path character that satisfies the previous character in the result string .) -Prune indicates the directory to be ignored. Combined with the-path option, for example, find the 'index.html 'file in the current directory and ignore the'/www/bin' directory in the path: $ find. -path '. /www/bin'-prune-o-name 'index.html '-print-path ". /www/bin "-prune-o-name 'index.html '-print is-path". /www/bin "-a-prune-o-name 'index.html '-print short expression. -A and-o are short-circuited values in order, which is similar to shell & | if-path ". if/www/bin is true, evaluate-prune,-prune returns true, and the logical expression "and" is true; otherwise, do not seek a value-prune, and the logical expression is false. If-path "/usr/sam"-a-prune is false, evaluate-name. -Name: returns true. The "or" logical expression is true. Otherwise, the Value-name, "or" logical expression is not required. (Therefore, other query conditions must be placed after-o! The-path option is usually placed at the beginning, and-prune and-o must be used together) to avoid multiple files: $ find/usr \ (-path/usr/dir1-o-path/usr/dir2 \) -prune-o-name "index.html"-print '\ (' and '\)' indicate escape, and shell no longer gives special explanations on parentheses. Here, the brackets after escape indicate combination. (Note: The path cannot be appended !) -Depth first matches all files and then searches for them in the subdirectory. That is, the query by traversing the breadth. Note that the-prune option is invalid when the-depth option is added. 5. Execute the query:-exec command {}\; execute the command on the query result file, and {} contains the query result. -OK command {}\; has the same operation as-exec. The difference is that when you execute the command, an interactive prompt indicating whether to execute the command is relatively safe. When you use the-exec option of the find command to process matched files, the find command passes all matching files to exec for execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error will occur after the find command runs for several minutes. The error message is usually "the parameter column is too long" or "parameter column overflow ". In some systems, the-exec option is used to initiate a corresponding process for processing each matching file, rather than executing all the matching files as parameters once; in this way, in some cases, too many processes may occur and the system performance may decline, resulting in low efficiency. Xargs is used with pipe to perform operations on Matched files. The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this. The xargs command has only one process. For example, search for 'admin': $ find in a common file in the current path. -type f-print | xargs grep 'admin' for a parameter command, xargs implicitly transmits the matching file. For example, in the preceding grep files, the result is implicitly transmitted to files. For multi-parameter commands, such as cp file dir, you must use the-I option of xargs. For example, copy the price of the preceding query to '/usr': $ find. -type f-print | xargs-I cp {}/usr/after the-I option is added, xargs passes the matching result {}, this facilitates the use of multiple parameter commands.

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.