前一阵子,与一个程序员交流,问一个linux的问题,如果使用ls命令仅仅显示当前
目录不包括.的文件.
我以前常用的做法是ls加grep过滤的方式,我想这个也是许多linux用户常见的操作模式.
ls -l | grep -v '\.'
在他看来linux没有windows灵活,windows下仅仅dir *.就可以了.而且ls的命令参数如此
之多,竟然无法实现.
我当时给他的答案是windows下关于*的理解与linux不一样.出于好奇,我还是看了ls的文档,
我发现执行ls --ignore=*.* -l 可以做到.
后来我有看了bash shell编程的文档,发现在打开extglob模式下(缺省是打开的),ls也可以实现,而且更加灵活.
shopt -u extglob #关闭
shopt -s extglob #打开
ls -l !(*.*)
ls -l -d !(*.*)
把bash shell手册摘录如下:
If the extglob shell option is enabled using the shopt builtin, several extended
pattern matching operators are recognized. In the following description, a pattern-
list is a list of one or more patterns separated by a |. Composite patterns may be
formed using one or more of the following sub-patterns:
?(pattern-list) Matches zero or one occurrence of the given patterns
*(pattern-list) Matches zero or more occurrences of the given patterns
+(pattern-list) Matches one or more occurrences of the given patterns
@(pattern-list) Matches exactly one of the given patterns
!(pattern-list) Matches anything except one of the given patterns
目录不包括.的文件.
我以前常用的做法是ls加grep过滤的方式,我想这个也是许多linux用户常见的操作模式.
ls -l | grep -v '\.'
在他看来linux没有windows灵活,windows下仅仅dir *.就可以了.而且ls的命令参数如此
之多,竟然无法实现.
我当时给他的答案是windows下关于*的理解与linux不一样.出于好奇,我还是看了ls的文档,
我发现执行ls --ignore=*.* -l 可以做到.
后来我有看了bash shell编程的文档,发现在打开extglob模式下(缺省是打开的),ls也可以实现,而且更加灵活.
shopt -u extglob #关闭
shopt -s extglob #打开
ls -l !(*.*)
ls -l -d !(*.*)
把bash shell手册摘录如下:
If the extglob shell option is enabled using the shopt builtin, several extended
pattern matching operators are recognized. In the following description, a pattern-
list is a list of one or more patterns separated by a |. Composite patterns may be
formed using one or more of the following sub-patterns:
?(pattern-list) Matches zero or one occurrence of the given patterns
*(pattern-list) Matches zero or more occurrences of the given patterns
+(pattern-list) Matches one or more occurrences of the given patterns
@(pattern-list) Matches exactly one of the given patterns
!(pattern-list) Matches anything except one of the given patterns