是否有一个等同于zsh的文件类型globbing的bash?

时间:2010-10-26 05:20:37

标签: bash glob

在zsh中,您可以使用文件类型断言限定globs,例如*(/)只匹配目录,*(.)只有普通文件,有没有办法在bash中做同样的事情而不求助于查找?

2 个答案:

答案 0 :(得分:3)

你可以尝试

ls -ltrd */ #match directories using -d and the slash "/"

echo */

for dir in */
do
  ...
done

如果你需要递归,你有Bash 4 +

$ shopt -s globstar
$ for dir in **/*/; do echo $dir; done

答案 1 :(得分:0)

我认为没有办法直接执行此操作,但不要忘记您可以使用test选项-d-f来确定名称是否指的是目录或文件。

for a in *; do
  if [ -d "$a" ]; then
    echo Directory: $a
  elif [ -f "$a" ]; then
    echo File: $a
  fi
done