简体   繁体   English

Linux与Unix文件通配符

[英]Linux vs. Unix file wildcards

I'm looking to get a list of files in a directory in Linux starting with a capital letter. 我希望在大写字母的Linux目录中获取一个文件列表。 In Unix, it's a simple 在Unix中,它很简单

ls [AZ]* ls [AZ] *

In Linux, though, I'm seeing matches that don't seem to be case-sensitive: 但是在Linux中,我看到的匹配似乎不区分大小写:

=> ls
A.txt  b.txt  B.txt  c.txt  C.txt

=> ls [A]*
A.txt

=> ls [AB]*
A.txt  B.txt

=> ls [ABC]*
A.txt  B.txt  C.txt


=> ls [AC]* A.txt b.txt B.txt c.txt C.txt => ls [AC] * A.txt b.txt B.txt c.txt C.txt

=> ls [b]*
b.txt


=> ls [ac]* A.txt b.txt B.txt c.txt => ls [ac] * A.txt b.txt B.txt c.txt

Running the same commands on the Unix side work as I'd expect. 在Unix端运行相同的命令就像我期望的那样。 Is this the way Linux has always behaved? 这是Linux一直表现的方式吗? It's easy enough to work around this with awk, so I'm not looking for a solution in that way, but I'm wondering if I just never noticed this before. 使用awk解决这个问题很容易,所以我不是那样寻找解决方案,但我想知道我以前是否从未注意到这一点。 Thanks in advance. 提前致谢。

The result depends on different shell options, particularly: nocasematch nocaseglob And also LC_COLLATE variable (used in sort, [-], etc.) 结果取决于不同的shell选项,特别是:nocasematch nocaseglob以及LC_COLLATE变量(用于sort,[ - ]等)

$ shopt extglob nocasematch nocaseglob
extglob         off
nocasematch     off
nocaseglob      off


$ printf "%s\n" a A b B c C | sort
a
A
b
B
c
C

So [AC] range contains b and c, but not a, also [ac] should contain A but not C. 所以[AC]范围包含b和c,但不是a,[ac]也应该包含A但不包含C.

$ printf "%s\n" a A b B c C | LC_COLLATE=C sort
A
B
C
a
b
c

gives a different result. 给出了不同的结果。

$ LC_COLLATE=C ls [A-C]*

Should return expected result, this syntax sets the variable only for the new process execution (ls), but not in current shell process. 应返回预期结果,此语法仅为新进程执行(ls)设置变量,但不在当前shell进程中设置。

EDIT: thanks to comment, previous command is wrong because the expansion is processed before the LC_COLLATE be set, the simplest is to split into two commands. 编辑:感谢评论,以前的命令是错误的,因为在设置LC_COLLATE之前处理扩展,最简单的是分成两个命令。

$ export LC_COLLATE=C
$ ls [A-C]*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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