8. find in shell commands and find in shell commands

Source: Internet
Author: User

8. find in shell commands and find in shell commands

The find command, which is commonly used like cd. however, most of the time, one or two parameters are enough. or, barely enough. however, when we take the initiative to look at the find manual, we will find that the more practical functions are not used.

This article briefly introduces find in combination with your own usage. It is not guaranteed to be comprehensive. It only lists the options that I have benefited a lot from in use.

Find dir [option] specifies the current directory for search.

Option can be omitted. If it is omitted, find recursively lists all files in the current directory.

There are many commonly used options, and only some of them are listed:

-Name: Specifies the file name. Wildcards can be used. If Wildcards are used, they must be expanded using double quotation marks.

-Type: Specifies the file type. The optional types include general file f directory file d symbol file l character Device c Block Device B.

-Size followed by +-N plus + N indicates that the size of the object exceeds the specified size of N, and-N indicates that the size is smaller than the specified size of N.

-Newer file: list the current directory and Its subdirectories.

-The mtime +-N option is similar to the size Option. + N indicates that the modification time is earlier than the specified N * 24.-N indicates that the modification time disappears from N * 24.

-Ctime +-N is the same as above, but it indicates the time when the file state changes. Note that the "find" method does not support searching by creation time.

-Atime +-N is the same as above. The benchmark is the access time.

-Maxdepth: specifies the maximum directory depth. This option can avoid the endless traversal of subdirectories by find.

-Depth indicates that a directory is processed and then the subdirectory is traversed.

-And is used for conditions. You can specify multiple conditions. The returned results are files that meet all conditions.

-Or is the same as above, but the returned result is one of the conditions.

! Put it in front of the option, and you can see its usage later. It is used to specify files that do not meet the conditions.

-Exec command [option] {}\; non-interactive command execution. Execute command for all files found by find. Note the spaces between {} And.

-OK command [option] {] \; Interactive command execution: Execute command on the file found by find, but will prompt whether to confirm.

-Delete: delete all files found.

The above should be able to cope with most of the scenarios. there is another reason for using find. I discovered it by mistake. when there are many files in a directory. you may want to execute the command like this: ls "*. txt "| xargs cat

A command that seems to have no problem, but an error occurs. the reason is that there are too many files. you cannot use this command. In this case, find will also be used. certainly, find must be different.

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

1. Find common files not suffixed with sh in the current directory and execute cat

2. List Files with non-sh and txt suffixes

Two possible methods are listed above. Note! There must be spaces between the brackets and the options. Otherwise, an error will be reported. Note that there must be spaces between the parentheses and the options. Otherwise, an error will be reported.

The effect of parentheses may not be obvious now. I couldn't think of a specific example at the moment. in short, if no parentheses exist, there is a scenario where multiple conditions are used for filtering and the-exec option is used, the command after the-exec option only executes the file filtered out by the last specified option. well, I have encountered this problem. but I cannot figure out the scenario. shame.

3. List the files larger than a certain size added to the current file

Note that when no unit is specified, the unit of the number following the size is a block of bytes. Therefore, do not consider it as a byte.


The general usage of find is summarized so much. There must be many incomplete ones. Fortunately, it is not difficult to understand the find option. Therefore, you can write a statement against this option.

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.