如何编写一个 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.
最佳答案
find . -maxdepth 1 -type d \( ! -name . \) -exec bash -c "cd '{}' && pwd" \;
\( !-name .\)
避免在当前目录中执行命令。
关于bash - 如何进入每个目录并执行命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7470165/