简体   繁体   中英

How does “*.txt” regex work with ls command?

I have a file name

atxt

and I am running this command

ls *.txt

Ideally it should match . with 'a' and * with zero character, but it is returning files like

a.txt, b.txt

Why is it that here ls is treating . as literal dot, rater than any character of regex.

It's called file globbing , not regular expression .

Although both support wildcards like "?", "*", they have different schemes.

For example, "a*" in glob matches any filename that begins with "a", but in regex it matches any string that has 0 or more of letter "a". Another difference is wildcard "?" and "*" in regex must have a preceding element, while it's unnecessary in globbing.

As for your last question, a dot "." has not special meaning in globbing, it's always a literal dot. To match exactly one unknown character in globbing, one could use "?".

Its not REGEX, its wildcard , where * means any character(s), before .txt that is why you are getting all the files with extension .txt

Files and directories - WildCard

When specifying file names (or paths) in CP/M, DOS, Microsoft Windows and Unix-like operating systems, the asterisk character ("*") substitutes for zero or more characters. In Unix-like operating systems, the question mark ("?") substitutes for exactly one character

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM