bash - 如何进入每个目录并执行命令?

标签 bash shell unix find

如何编写一个 bash 脚本遍历 parent_directory 中的每个目录并在每个目录执行一个命令

目录结构如下:

parent_directory (name could be anything - doesnt follow a pattern)

  • 001 (directory names follow this pattern)
    • 0001.txt (filenames follow this pattern)
    • 0002.txt
    • 0003.txt
  • 002
    • 0001.txt
    • 0002.txt
    • 0003.txt
    • 0004.txt
  • 003
    • 0001.txt

the number of directories is unknown.

最佳答案

answer发布者 Todd帮助了我。

find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd" \;

\( !-name .\) 避免在当前目录中执行命令。

关于bash - 如何进入每个目录并执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470165/

相关文章:

bash - 将 CSV 文件拆分成更小的文件但保留标题?

bash - 如何在 Bash 脚本中复制文件而不覆盖任何文件

shell - "bash -eu"之间的差异。和 "bash -e"

shell-mode emacs 奇怪地显示 npm install 字符

unix - curl | tar - gzip : stdin: not in gzip format

linux - 交互式和非交互式 shell 中的环境变量

linux - 从 Linux Shell 递归解压?

bash - 在每一行中搜索一个模式并将其附加到该行的末尾

c - 如何将测试用例添加到 ltp

linux - 使用 bash 脚本使用 valgrind 检查内存