在zsh中,您可以使用文件类型断言来限定通配符,例如
*(/)
只匹配目录,*(.)
只匹配普通文件。在bash中有没有一种方法可以做到这一点,而不必求助于find命令?你可以尝试
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
我认为没有直接实现这个的方法,但不要忘记你可以使用test选项-d
和-f
来确定名称是指目录还是文件。
for a in *; do
if [ -d "$a" ]; then
echo Directory: $a
elif [ -f "$a" ]; then
echo File: $a
fi
done
fooBar */
的bash函数。如果我把fooBar
变成一个脚本,我可以使用find . -maxdepth 1 -type d -print0 | xargs -0 fooBar
命令,这比在for循环中构建数组要容易得多。 - tobyodavies