linux - 如何在 Linux 中遍历目录?

我正在 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/

相关文章:

python - 'setdefault' dict 方法的用例

python - Conda 是否取代了对 virtualenv 的需求?

python - 如何在 Python 中获取当前执行文件的路径?

linux - 如何在 bash 中在一行中运行多个后台命令?

linux - SVN check out 文件夹的内容,而不是文件夹本身

python - 在 chrome 中运行 Selenium WebDriver python 绑定

linux - 如何在 Linux 中创建给定大小的文件?

linux - libtool 的 .la 文件有什么用?

python - 如何检查我的 python 对象是否为数字?

python - 如何获得 Python 类的 parent ?