我正在 Linux 上用 bash 编写脚本,需要遍历给定目录中的所有子目录名称。如何遍历这些目录(并跳过常规文件)?
例如:
给定目录是 /tmp/
它有以下子目录:/tmp/A,/tmp/B,/tmp/C
我想检索 A、B、C。
最佳答案
到目前为止,所有答案都使用 find
,所以这里只有一个 shell。在您的情况下不需要外部工具:
for dir in /tmp/*/ # list directories in the form "/tmp/dirname/"
do
dir=${dir%*/} # remove the trailing "/"
echo "${dir##*/}" # print everything after the final "/"
done
https://stackoverflow.com/questions/2107945/