Bash Bug?不能使用否定extglob!(* /)过滤目录?

时间:2012-01-02 00:07:45

标签: bash shell

如果glob */只匹配目录,那么从逻辑上讲,extglob !(*/)应匹配非目录;但这不起作用。这是一个错误还是我错过了什么?这适用于任何shell吗?

测试1以证明*/有效

$ cd /tmp; ls -ld */
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test1//
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test2//
drwxr-xr-x  2 seand users 4096 Jan  1 15:59 test3//

测试2显示!(*/)

的潜在错误
$ cd /tmp; shopt -s extglob; ls -ld !(*/)
/bin/ls: cannot access !(*/): No such file or directory

2 个答案:

答案 0 :(得分:2)

在Bash中,!()(例如*?*()@())仅适用于一个路径组件。因此,!(anything containing a / slash)无效。

如果切换到zsh,则可以使用*(^/)匹配所有非目录,或*(.)匹配所有普通文件。

答案 1 :(得分:0)

已经给出了具体问题的答案;我不确定你是否真的想要另一个解决方案,或者你只是想分析行为,但列出当前文件夹中所有非目录的一种方法是使用find:

find . ! -type d -maxdepth 1