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