如果glob */
只匹配目录,那么从逻辑上讲,extglob !(*/)
应匹配非目录;但这不起作用。这是一个错误还是我错过了什么?这适用于任何shell吗?
*/
有效$ 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//
!(*/)
$ cd /tmp; shopt -s extglob; ls -ld !(*/)
/bin/ls: cannot access !(*/): No such file or directory
答案 0 :(得分:2)
在Bash中,!()
(例如*
,?
,*()
和@()
)仅适用于一个路径组件。因此,!(anything containing a / slash)
无效。
如果切换到zsh,则可以使用*(^/)
匹配所有非目录,或*(.)
匹配所有普通文件。
答案 1 :(得分:0)
已经给出了具体问题的答案;我不确定你是否真的想要另一个解决方案,或者你只是想分析行为,但列出当前文件夹中所有非目录的一种方法是使用find:
find . ! -type d -maxdepth 1