8 Shell Command's find

Source: Internet
Author: User

The Find command, like a CD, is often used. But perhaps most of the time as long as the one or two parameters are enough to use. Or, it's a little too much. But when we take the initiative to look at the handbook of Find, we will find that the original more useful features are not used.

This article is a simple introduction to find using your own use. Not guaranteed to be comprehensive, just list the options I've benefited a lot from using.

Find dir [option] Specifies the current directory for a lookup.

option can be omitted, and if omitted, find recursively lists all files in the current directory.

There are a number of commonly used option, only a common part is listed:

-NAME Specifies the file name, you can use wildcards, and if you use wildcards, you need to expand them with double quotes.

-TYPE Specifies the type of file, optional types include normal file f directory file D symbol file L character device C block device B etc.

-size can be followed by a +-n +n represents a file that exceeds n specified size, and-n indicates a size less than n specified.

-newer file lists the current directory and subdirectories, new to the specified file

-mtime +-n is similar to the size option, +n indicates that the modification time is older than the specified n*24 disappears.-N indicates that the modification time is new to n*24 disappears.

-ctime +-n Ibid, but that is the time of the change in the state of the file, note that find does not have the so-called Search by creation time

-atime +-n Ibid, the lookup benchmark is the time of the visit.

-maxdepth maximum directory depth. By specifying this option, you can avoid find endless traversal of subdirectories.

-depth This option means to process a directory and then traverse subdirectories.

-and is used for conditions, you can specify multiple conditions, and the returned result is a file that satisfies all conditions

-or Ibid, but return is one of the conditions of satisfaction.

! Put it in front of the option, and you'll see his usage later. Used to specify a file that does not meet the criteria.

-exec command [option] {} \; The non-interactive execution command, which will be executed for all files found by find. Note the spaces between {} and \.

-OK command [option] {] \; Executes commands interactively, executes the command for the file found by find, but prompts for confirmation.

-delete the files that are found are deleted.

The above should be able to cope with most of the scene. There is another reason why you can use find. I accidentally found it. When there are more files in a directory. You might want to do this. Command: ls "*.txt" | Xargs Cat

A command that appears to have no problem, but an error occurs. The reason is that there are too many files. This command is not available, and find will come in handy at this time. There must be something different about find.

Let's look at the application of find in practice.

1. Locate the normal non-sh suffix file for the current directory and perform the cat

2. List non-sh and txt suffix files

There are two possible ways to do this. Note that there must be a space between the options, or you will get an error. Notice that there are spaces between the parentheses and the options, otherwise you will get an error.

The effect of parentheses may not be obvious now. I could not think of a concrete example at the moment. In short, if there is no parenthesis, there is a scene, multiple conditions to filter and use the-EXEC option,- The command after the EXEC option executes only the file that is filtered by the last specified option. Well, I've had this problem. But I can't figure out the scene. Ashamed.

3. list files larger than a certain size for the current file

It is important to note that when no unit is specified, the number behind the size is a block of 512byte. So don't take it for granted that it's a byte.


The general use of find is summed up so much. There must be a lot of incomplete, fortunately the option of find is not difficult to understand, so you can write a statement against this option.

8 Shell Command's find

Related Article

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.