Linux file name matching-wildcard use

Source: Internet
Author: User

Background: During Linux use, it is often necessary to find files, and it is not clear how the wildcard pattern and regular expressions in a command are distinguished. It is necessary to study it well.

1 Literacy 1.1-pass wildcards regular expression

When using command lines, there is a lot of time to find the files you need, such as LS find. S h e l L provides a complete set of string pattern matching rules, or meta-characters, when S-H-L encounters the above characters, it treats them as special characters rather than ordinary characters in the file name, so that the user can use them to match the corresponding file names, which I understand can be called wildcards.

wildcard characters and regular expressions are different, simple: wildcard is used to match, the regular expression is used for matching strings;

In the text Filter tool, the regular expression is used , such as awk,sed, etc., is for the content of the file.

wildcard characters are used in filenames , such as finding FIND,LS,CP, and so on.

Second, the shell handles wildcard and regular expressions differently, "" is typically a wildcard character (the shell itself extracts the processing), and "Inside" is usually a regular expression (the shell will pass the data to other commands for processing).

2 wildcard characters Detail test data
Touch a a6.log abc.log ac.txt b c C5.txt X.log a
"*"

Represents any number of characters

Example: Querying for files ending with ". Log" ll *.log
“?”

Represents any single character

Example: Query only A, B, CLL?

“[]”

A character that represents "[" and "]", such as [0-9] can represent any number between 0-9, [a-za-z] can represent any one letter between A-Z and a-Z, and the letters are case-sensitive.

Example: Querying only the letter file ll [a-za-z]

Example: Query a file with a ". Log" ending and a ". Log" with only two characters before the second character is the number ll? [0-9].log

^

Indicates that the match result is reversed, and note that the wildcard must be used in []

Example: Querying a file that does not end with ". txt" ll *[^txt]*

“{}”

Represents multiple files that are enclosed in parentheses

Example: Query '. Log ' and '. txt ' end of file ll {*.log,*.txt}

Note: "." This conforms to the relatively special, if the matching conditions are added to the match then the query result file contains the band "." of the file

For example the preceding "^" example, if I query this way "ll *". [^txt]*, then the result is different.

Delete operation
For example: Delete A, B, C, and a file ending in. txt rm-f {[Abc],*.txt}

Of course, since you can query, of course, you can also use wildcard matching to move files, if you need to move some types of files in a folder with a large number of files then the efficiency of using wildcard matching is obvious; the use of wildcards is more than just these, and the availability of them can be studied more.

3 Example
*   matches any string in the file name, including an empty string.? Matches any single character in the file name. [...]   Matches any of the characters contained in []. [!...]   Match [] Non-exclamation! The character after. Same as ^ 's effect

Such as:

All strings starting with 5* 5

All strings ending in 5

A. A string with a second-to-last character of 5

[0-9] all characters with numbers

[1] or 2

[!0-9] is not a numeric character

ls/etc/[!a-n]*.conf lists files in the/etc/directory that do not start with the letters A through n and end with. conf

ls/etc/[a-n]*.conf lists files in the/etc/directory that begin with the letters A through n and end with. conf

ls/bin/[ck]* lists filenames that begin with C or K

Linux file name matching-wildcard use

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.